diff options
author | Sven Gothel <[email protected]> | 2022-08-02 06:55:31 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-08-02 06:55:31 +0200 |
commit | b01d1571bd707e1c7ebacce3cd959ec3f6f8c710 (patch) | |
tree | 0de8b65bb825012c5e30930e4f7691868f8b0f1d | |
parent | 1a5e5b552ef6922d2f3a814862c2b13bbeb1b93b (diff) |
jau::io::read_stream(): Guarantee consumer_fn() is called with `is_final=true` once at the end, even if input stream has zero size
-rw-r--r-- | include/jau/io_util.hpp | 9 | ||||
-rw-r--r-- | src/io_util.cpp | 6 |
2 files changed, 12 insertions, 3 deletions
diff --git a/include/jau/io_util.hpp b/include/jau/io_util.hpp index 3d48a90..ad91a42 100644 --- a/include/jau/io_util.hpp +++ b/include/jau/io_util.hpp @@ -97,6 +97,9 @@ namespace jau::io { * * To abort streaming, user may return `false` from the given `consumer_func`. * + * It is guaranteed that consumer_fn() is called with `is_final=true` once at the end, + * even if `input_file` stream has zero size. + * * @param input_file input file name path, `-` denotes std::stdin. * @param buffer secure std::vector buffer, passed down to consumer_fn * @param consumer_fn StreamConsumerFunc consumer for each received heap of bytes, returning true to continue stream of false to abort. @@ -113,6 +116,9 @@ namespace jau::io { * * To abort streaming, user may return `false` from the given `consumer_func`. * + * It is guaranteed that consumer_fn() is called with `is_final=true` once at the end, + * even input stream has zero size. + * * @param in the input byte stream to read from * @param buffer secure std::vector buffer, passed down to consumer_fn * @param consumer_fn StreamConsumerFunc consumer for each received heap of bytes, returning true to continue stream of false to abort. @@ -127,6 +133,9 @@ namespace jau::io { * * To abort streaming, user may return `false` from the given `consumer_func`. * + * It is guaranteed that consumer_fn() is called with `is_final=true` once at the end, + * even if input stream has zero size. + * * Implementation reads one buffer ahead in respect to consumer_fn(). <br/> * If reading zero bytes on the next buffer, * it propagates the end-of-file (EOF) to the previous buffer which will be send via consumer_fn() next.<br/> diff --git a/src/io_util.cpp b/src/io_util.cpp index 724e51e..7b43520 100644 --- a/src/io_util.cpp +++ b/src/io_util.cpp @@ -61,8 +61,8 @@ uint64_t jau::io::read_stream(ByteInStream& in, secure_vector<uint8_t>& buffer, StreamConsumerFunc consumer_fn) noexcept { uint64_t total = 0; - bool has_more = !in.end_of_data(); - while( has_more ) { + bool has_more; + do { if( in.check_available(1) ) { // at least one byte to stream .. buffer.resize(buffer.capacity()); const uint64_t got = in.read(buffer.data(), buffer.capacity()); @@ -83,7 +83,7 @@ uint64_t jau::io::read_stream(ByteInStream& in, buffer.resize(0); consumer_fn(buffer, true); // forced final, zero size } - } + } while( has_more ); return total; } |