25#ifndef JAU_BYTE_STREAM_HPP_
26#define JAU_BYTE_STREAM_HPP_
131 explicit operator bool() const noexcept {
return !
fail(); }
224 [[nodiscard]] virtual
size_t read(
void* out,
size_t length) noexcept = 0;
231 [[nodiscard]]
bool read(uint8_t& out) noexcept;
243 [[nodiscard]] virtual
size_t peek(
void* out,
size_t length,
size_t peek_offset) noexcept = 0;
250 [[nodiscard]]
bool peek(uint8_t& out) noexcept;
257 [[nodiscard]]
size_t discard(
size_t N) noexcept;
263 virtual
std::
string id() const noexcept {
return ""; }
270 virtual uint64_t
tellg() const noexcept = 0;
292 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
294 [[nodiscard]]
size_t peek(
void*,
size_t,
size_t)
noexcept override;
296 bool available(
size_t n)
noexcept override;
310 : m_source(in, in + length), m_offset(0) {}
317 : m_source(
std::move(in)), m_offset(0) {}
324 : m_source(in.begin(), in.end()), m_offset(0) {}
326 bool is_open() const noexcept
override {
return true; }
328 void close() noexcept override;
332 uint64_t
tellg() const noexcept
override {
return m_offset; }
336 uint64_t
content_size() const noexcept
override {
return m_source.size(); }
338 std::string
to_string() const noexcept override;
360 bool m_has_content_length;
361 uint64_t m_content_size;
362 uint64_t m_bytes_consumed;
363 uint64_t get_available()
const noexcept {
return m_has_content_length ? m_content_size - m_bytes_consumed : 0; }
366 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
367 [[nodiscard]]
size_t peek(
void*,
size_t,
size_t)
noexcept override;
368 bool available(
size_t n)
noexcept override;
370 bool is_open() const noexcept
override {
return 0 <= m_fd; }
372 std::string
id() const noexcept
override {
return stats.path(); }
379 int fd() const noexcept {
return m_fd; }
416 void close() noexcept override;
420 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
424 uint64_t
content_size() const noexcept
override {
return m_content_size; }
426 std::string
to_string() const noexcept override;
451 bool available(
size_t n)
noexcept override;
468 [[nodiscard]]
size_t read(
void* out,
size_t length)
noexcept override;
470 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override;
474 std::string
id() const noexcept
override {
return m_url; }
487 bool is_open() const noexcept override;
489 void close() noexcept override;
493 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
495 bool has_content_size() const noexcept
override {
return m_stream_resp->has_content_length; }
497 uint64_t
content_size() const noexcept
override {
return m_stream_resp->content_length; }
499 std::string
to_string() const noexcept override;
502 uint64_t get_available() const noexcept {
return m_stream_resp->has_content_length ? m_stream_resp->content_length - m_bytes_consumed : 0; }
503 std::string to_string_int() const noexcept;
505 const
std::
string m_url;
510 uint64_t m_bytes_consumed;
545 bool available(
size_t n)
noexcept override;
562 [[nodiscard]]
size_t read(
void* out,
size_t length)
noexcept override;
564 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override;
568 std::string
id() const noexcept
override {
return m_id; }
581 bool is_open() const noexcept override;
583 void close() noexcept override;
587 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
591 uint64_t
content_size() const noexcept
override {
return m_content_size; }
602 m_buffer.interruptReader();
632 [[nodiscard]]
bool write(uint8_t in[],
size_t length)
noexcept {
633 return write(in, length, m_timeout);
641 m_content_size = size;
642 m_has_content_length =
true;
656 std::string
to_string() const noexcept override;
659 uint64_t get_available() const noexcept {
return m_has_content_length ? m_content_size - m_bytes_consumed : 0; }
660 std::string to_string_int() const noexcept;
662 const
std::
string m_id;
669 uint64_t m_bytes_consumed;
680 [[nodiscard]]
size_t read(
void*,
size_t)
noexcept override;
682 [[nodiscard]]
size_t peek(
void* out,
size_t length,
size_t peek_offset)
noexcept override {
683 return m_parent.peek(out, length, peek_offset);
687 return m_parent.available(n);
693 std::string
id() const noexcept
override {
return m_parent.id(); }
701 : m_parent(parent), m_bytes_consumed(0), m_buffer(buffer), m_rec_offset(0), m_is_recording(
false) {};
707 bool is_open() const noexcept
override {
return m_parent.is_open(); }
709 void close() noexcept override;
713 uint64_t
tellg() const noexcept
override {
return m_bytes_consumed; }
717 uint64_t
content_size() const noexcept
override {
return m_parent.content_size(); }
725 void start_recording() noexcept;
733 void stop_recording() noexcept;
741 void clear_recording() noexcept;
753 std::string
to_string() const noexcept override;
757 uint64_t m_bytes_consumed;
759 uint64_t m_rec_offset;
797 [[nodiscard]] virtual
size_t write(const
void* in,
size_t length) noexcept = 0;
804 [[nodiscard]]
bool write(const uint8_t& in) noexcept;
810 virtual
std::
string id() const noexcept {
return ""; }
817 virtual uint64_t
tellp() const noexcept = 0;
837 bool is_open() const noexcept
override {
return 0 <= m_fd; }
839 [[nodiscard]]
size_t write(
const void*,
size_t)
noexcept override;
841 std::string
id() const noexcept
override {
return stats.path(); }
848 int fd() const noexcept {
return m_fd; }
893 void close() noexcept override;
897 uint64_t
tellp() const noexcept
override {
return m_bytes_consumed; }
899 std::string
to_string() const noexcept override;
902 uint64_t m_bytes_consumed;
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
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(std::string id_name, const jau::fraction_i64 &timeout) noexcept
Construct a ringbuffer backed externally provisioned byte input stream.
void close() noexcept override
Close the stream if supported by the underlying mechanism.
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.
ByteInStream_File(const std::string &path) noexcept
Construct a stream based byte input stream from filesystem path.
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
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
io::secure_vector< uint8_t > & get_recording() noexcept
Returns the reference of the recording buffer given by user.
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
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.
ByteInStream_SecMemory(const std::string &in)
Construct a secure memory source that reads from a string.
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
ByteInStream_URL(std::string url, const jau::fraction_i64 &timeout) noexcept
Construct a ringbuffer backed Http byte input stream.
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.
void close() noexcept override
Close the stream if supported by the underlying mechanism.
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(const std::string &path, const jau::fs::fmode_t mode=jau::fs::fmode_t::def_file_prot) noexcept
Construct a stream based byte output stream from filesystem path, either an existing or new file.
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
virtual bool is_open() const noexcept=0
Checks if the stream has an associated file.
virtual std::string to_string() const noexcept=0
virtual uint64_t tellp() const noexcept=0
Returns the output position indicator.
virtual size_t write(const void *in, size_t length) noexcept=0
Write to the data sink.
virtual void close() noexcept=0
Close the stream if supported by the underlying mechanism.
~ByteOutStream() noexcept override=default
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
ordered_atomic< bool, std::memory_order_relaxed > relaxed_atomic_bool
Relaxed non-SC atomic integral scalar boolean.
ordered_atomic< uint64_t, std::memory_order_relaxed > relaxed_atomic_uint64
Relaxed non-SC atomic integral scalar uint64_t.
#define JAU_MAKE_BITFIELD_ENUM_STRING(type,...)
constexpr E & write(E &store, const E bits, bool set) noexcept
If set==true, sets the bits in store, i.e.
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 ...
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
jau::ordered_atomic< io_result_t, std::memory_order_relaxed > relaxed_atomic_io_result_t
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...
io_result_t
I/O operation result value.
std::shared_ptr< AsyncStreamResponse > AsyncStreamResponseRef
jau::ringbuffer< uint8_t, size_t > ByteRingbuffer
std::vector< T, jau::callocator_sec< T > > secure_vector
@ 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.
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.