25#ifndef JAU_BYTE_STREAM_HPP_
26#define JAU_BYTE_STREAM_HPP_
73 return static_cast<uint32_t
>(rhs);
76 return static_cast<iostate> ( ~number(rhs) );
103 return !( lhs == rhs );
120 : m_state(
iostate::goodbit ) {}
162 explicit operator bool() const noexcept {
return !
fail(); }
255 [[nodiscard]] virtual
size_t read(
void* out,
size_t length) noexcept = 0;
262 [[nodiscard]]
bool read(uint8_t& out) noexcept;
274 [[nodiscard]] virtual
size_t peek(
void* out,
size_t length,
size_t peek_offset) noexcept = 0;
281 [[nodiscard]]
bool peek(uint8_t& out) noexcept;
288 [[nodiscard]]
size_t discard(
size_t N) noexcept;
294 virtual
std::
string id() const noexcept {
return ""; }
301 virtual uint64_t
tellg() const noexcept = 0;
323 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
325 [[nodiscard]]
size_t peek(
void*,
size_t,
size_t)
noexcept override;
327 bool available(
size_t n)
noexcept override;
341 : m_source(in, in + length), m_offset(0) {}
348 : m_source(
std::move(in)), m_offset(0) {}
355 : m_source(in.begin(), in.end()), m_offset(0) {}
357 bool is_open() const noexcept
override {
return true; }
359 void close() noexcept override;
363 uint64_t
tellg() const noexcept
override {
return m_offset; }
367 uint64_t
content_size() const noexcept
override {
return m_source.size(); }
369 std::string
to_string() const noexcept override;
391 bool m_has_content_length;
392 uint64_t m_content_size;
393 uint64_t m_bytes_consumed;
394 uint64_t get_available()
const noexcept {
return m_has_content_length ? m_content_size - m_bytes_consumed : 0; }
397 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
398 [[nodiscard]]
size_t peek(
void*,
size_t,
size_t)
noexcept override;
399 bool available(
size_t n)
noexcept override;
401 bool is_open() const noexcept
override {
return 0 <= m_fd; }
403 std::string
id() const noexcept
override {
return stats.
path(); }
410 int fd() const noexcept {
return m_fd; }
447 void close() noexcept override;
451 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
455 uint64_t
content_size() const noexcept
override {
return m_content_size; }
457 std::string
to_string() const noexcept override;
482 bool available(
size_t n)
noexcept override;
499 [[nodiscard]]
size_t read(
void* out,
size_t length)
noexcept override;
501 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override;
505 std::string
id() const noexcept
override {
return m_url; }
518 bool is_open() const noexcept override;
520 void close() noexcept override;
524 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
528 uint64_t
content_size() const noexcept
override {
return m_content_size; }
530 std::string
to_string() const noexcept override;
533 uint64_t get_available() const noexcept {
return m_has_content_length ? m_content_size - m_bytes_consumed : 0; }
534 std::string to_string_int() const noexcept;
536 const
std::
string m_url;
539 jau::io::url_header_sync m_header_sync;
544 std::unique_ptr<
std::thread> m_url_thread;
545 uint64_t m_bytes_consumed;
580 bool available(
size_t n)
noexcept override;
597 [[nodiscard]]
size_t read(
void* out,
size_t length)
noexcept override;
599 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override;
603 std::string
id() const noexcept
override {
return m_id; }
616 bool is_open() const noexcept override;
618 void close() noexcept override;
622 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
626 uint64_t
content_size() const noexcept
override {
return m_content_size; }
637 m_buffer.interruptReader();
667 [[nodiscard]]
bool write(uint8_t in[],
size_t length)
noexcept {
668 return write(in, length, m_timeout);
676 m_content_size = size;
677 m_has_content_length =
true;
691 std::string
to_string() const noexcept override;
694 uint64_t get_available() const noexcept {
return m_has_content_length ? m_content_size - m_bytes_consumed : 0; }
695 std::string to_string_int() const noexcept;
697 const
std::
string m_id;
704 uint64_t m_bytes_consumed;
715 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
717 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override {
718 return m_parent.peek(out, length, peek_offset);
722 return m_parent.available(n);
728 std::string
id() const noexcept
override {
return m_parent.id(); }
736 : m_parent(parent), m_bytes_consumed(0), m_buffer(buffer), m_rec_offset(0), m_is_recording(
false) {};
742 bool is_open() const noexcept
override {
return m_parent.is_open(); }
744 void close() noexcept override;
748 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
752 uint64_t
content_size() const noexcept
override {
return m_parent.content_size(); }
760 void start_recording() noexcept;
768 void stop_recording() noexcept;
776 void clear_recording() noexcept;
788 std::string
to_string() const noexcept override;
792 uint64_t m_bytes_consumed;
794 uint64_t m_rec_offset;
832 [[nodiscard]] virtual
size_t write(const
void* in,
size_t length) noexcept = 0;
839 [[nodiscard]]
bool write(const uint8_t& in) noexcept;
845 virtual
std::
string id() const noexcept {
return ""; }
852 virtual uint64_t
tellp() const noexcept = 0;
872 bool is_open() const noexcept
override {
return 0 <= m_fd; }
874 [[nodiscard]]
size_t write(
const void*,
size_t)
noexcept override;
876 std::string
id() const noexcept
override {
return stats.
path(); }
883 int fd() const noexcept {
return m_fd; }
928 void close() noexcept override;
932 uint64_t
tellp() const noexcept
override {
return m_bytes_consumed; }
934 std::string
to_string() const noexcept override;
937 uint64_t m_bytes_consumed;
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
std::string path() const noexcept
Returns the unix path representation.
Ringbuffer-Based byte input stream with an externally provisioned data feed.
void set_content_size(const uint64_t size) noexcept
Set known content size, informal only.
ByteInStream_Feed & operator=(const ByteInStream_Feed &)=delete
uint64_t tellg() const noexcept override
Returns the input position indicator, similar to std::basic_istream.
void interruptReader() noexcept
Interrupt a potentially blocked reader.
bool has_content_size() const noexcept override
Returns true if implementation is aware of content_size(), otherwise false.
ByteInStream_Feed(const ByteInStream_Feed &)=delete
bool write(uint8_t in[], size_t length) noexcept
Write given bytes to the async ringbuffer.
uint64_t content_size() const noexcept override
Returns the content_size if known.
std::string id() const noexcept override
return the id of this data source
File based byte input stream, including named file descriptor.
uint64_t content_size() const noexcept override
Returns the content_size if known.
int fd() const noexcept
Returns the file descriptor if is_open(), otherwise -1 for no file descriptor.
ByteInStream_File(const ByteInStream_File &)=delete
std::string id() const noexcept override
return the id of this data source
bool is_open() const noexcept override
Checks if the stream has an associated file.
bool has_content_size() const noexcept override
Returns true if implementation is aware of content_size(), otherwise false.
uint64_t tellg() const noexcept override
Returns the input position indicator, similar to std::basic_istream.
ByteInStream_File & operator=(const ByteInStream_File &)=delete
Wrapped byte input stream with the capability to record the read byte stream at will.
size_t get_bytes_recorded() noexcept
uint64_t tellg() const noexcept override
Returns the input position indicator, similar to std::basic_istream.
bool available(size_t n) noexcept override
Return whether n bytes are available in the input stream, if has_content_size() or using an asynchron...
iostate rdstate() const noexcept override
Returns the current state flags.
std::string id() const noexcept override
return the id of this data source
bool is_open() const noexcept override
Checks if the stream has an associated file.
ByteInStream_Recorder(ByteInStream &parent, io::secure_vector< uint8_t > &buffer) noexcept
Construct a byte input stream wrapper using the given parent ByteInStream.
ByteInStream_Recorder & operator=(const ByteInStream_Recorder &)=delete
bool is_recording() noexcept
void clear(const iostate state=iostate::goodbit) noexcept override
Clears state flags by assignment to the given value.
size_t peek(void *out, size_t length, size_t peek_offset) noexcept override
Read from the source but do not modify the internal offset.
uint64_t get_recording_start_pos() noexcept
Returns the recording start position.
bool has_content_size() const noexcept override
Returns true if implementation is aware of content_size(), otherwise false.
uint64_t content_size() const noexcept override
Returns the content_size if known.
ByteInStream_Recorder(const ByteInStream_Recorder &)=delete
Secure Memory-Based byte input stream.
bool has_content_size() const noexcept override
Returns true if implementation is aware of content_size(), otherwise false.
uint64_t content_size() const noexcept override
Returns the content_size if known.
ByteInStream_SecMemory(io::secure_vector< uint8_t > in)
Construct a secure memory source that reads from a secure_vector.
uint64_t tellg() const noexcept override
Returns the input position indicator, similar to std::basic_istream.
ByteInStream_SecMemory(const std::vector< uint8_t > &in)
Construct a secure memory source that reads from a std::vector.
ByteInStream_SecMemory(const uint8_t in[], size_t length)
Construct a secure memory source that reads from a byte array.
bool is_open() const noexcept override
Checks if the stream has an associated file.
Ringbuffer-Based byte input stream with a URL connection provisioned data feed.
ByteInStream_URL(const ByteInStream_URL &)=delete
uint64_t tellg() const noexcept override
Returns the input position indicator, similar to std::basic_istream.
ByteInStream_URL & operator=(const ByteInStream_URL &)=delete
uint64_t content_size() const noexcept override
Returns the content_size if known.
bool has_content_size() const noexcept override
Returns true if implementation is aware of content_size(), otherwise false.
std::string id() const noexcept override
return the id of this data source
Abstract byte input stream object.
virtual void close() noexcept=0
Close the stream if supported by the underlying mechanism.
virtual bool has_content_size() const noexcept=0
Returns true if implementation is aware of content_size(), otherwise false.
virtual uint64_t content_size() const noexcept=0
Returns the content_size if known.
virtual size_t peek(void *out, size_t length, size_t peek_offset) noexcept=0
Read from the source but do not modify the internal offset.
virtual bool is_open() const noexcept=0
Checks if the stream has an associated file.
size_t discard(size_t N) noexcept
Discard the next N bytes of the data.
virtual uint64_t tellg() const noexcept=0
Returns the input position indicator, similar to std::basic_istream.
virtual std::string to_string() const noexcept=0
~ByteInStream() noexcept override=default
virtual size_t read(void *out, size_t length) noexcept=0
Read from the source.
virtual bool available(size_t n) noexcept=0
Return whether n bytes are available in the input stream, if has_content_size() or using an asynchron...
File based byte output stream, including named file descriptor.
ByteOutStream_File & operator=(const ByteOutStream_File &)=delete
std::string id() const noexcept override
return the id of this data source
int fd() const noexcept
Returns the file descriptor if is_open(), otherwise -1 for no file descriptor.
uint64_t tellp() const noexcept override
Returns the output position indicator.
bool is_open() const noexcept override
Checks if the stream has an associated file.
ByteOutStream_File(const ByteOutStream_File &)=delete
Abstract byte output stream object, to write data to a sink.
virtual uint64_t tellp() const noexcept=0
Returns the output position indicator.
~ByteOutStream() noexcept override=default
Supporting std::basic_ios's iostate functionality for all ByteInStream implementations.
bool fail() const noexcept
Checks if an error has occurred.
iostate_func & operator=(const iostate_func &o) noexcept=default
iostate_func & operator=(iostate_func &&o) noexcept=default
constexpr iostate rdstate_impl() const noexcept
iostate_func(iostate_func &&o) noexcept=default
void setstate(const iostate state) noexcept
Sets state flags, by keeping its previous bits.
bool good() const noexcept
Checks if no error nor eof() has occurred i.e.
bool bad() const noexcept
Checks if a non-recoverable error has occurred.
bool operator!() const noexcept
Checks if an error has occurred, synonym of fail().
virtual ~iostate_func() noexcept=default
bool eof() const noexcept
Checks if end-of-file has been reached.
bool timeout() const noexcept
Checks if a timeout (non-recoverable) has occurred.
iostate_func(const iostate_func &o) noexcept=default
virtual iostate rdstate() const noexcept
Returns the current state flags.
virtual void clear(const iostate state=iostate::goodbit) noexcept
Clears state flags by assignment to the given value.
constexpr void setstate_impl(iostate state) const noexcept
fmode_t
Generic file type and POSIX protection mode bits as used in file_stats, touch(), mkdir() etc.
@ def_file_prot
Default file protection bit: Safe default: POSIX S_IRUSR | S_IWUSR | S_IRGRP or read_usr | write_usr ...
std::vector< T, jau::callocator_sec< T > > secure_vector
iostate
Mimic std::ios_base::iostate for state functionality, see iostate_func.
constexpr iostate operator^(const iostate lhs, const iostate rhs) noexcept
constexpr iostate operator|(const iostate lhs, const iostate rhs) noexcept
std::unique_ptr< ByteInStream > to_ByteInStream(const std::string &path_or_uri, jau::fraction_i64 timeout=20_s) noexcept
Parses the given path_or_uri, if it matches a supported protocol, see jau::io::uri::protocol_supporte...
constexpr iostate operator~(const iostate rhs) noexcept
async_io_result_t
Asynchronous I/O operation result value.
jau::ringbuffer< uint8_t, size_t > ByteRingbuffer
constexpr iostate operator&(const iostate lhs, const iostate rhs) noexcept
constexpr uint32_t number(const iostate rhs) noexcept
constexpr iostate & operator^=(iostate &lhs, const iostate rhs) noexcept
std::string to_string(const iostate mask) noexcept
jau::ordered_atomic< async_io_result_t, std::memory_order_relaxed > relaxed_atomic_async_io_result_t
constexpr iostate & operator|=(iostate &lhs, const iostate rhs) noexcept
constexpr iostate & operator&=(iostate &lhs, const iostate rhs) noexcept
constexpr bool operator!=(const iostate lhs, const iostate rhs) noexcept
constexpr bool operator==(const iostate lhs, const iostate rhs) noexcept
@ goodbit
No error occurred nor has EOS being reached.
@ none
No error occurred nor has EOS being reached.
@ failbit
Input or output operation failed (formatting or extraction error).
@ eofbit
An input operation reached the end of its stream.
@ timeout
Input or output operation failed due to timeout.
@ badbit
Irrecoverable stream error, including loss of integrity of the underlying stream or media.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.