diff options
author | crupest <crupest@outlook.com> | 2024-05-12 00:19:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-05-12 00:19:16 +0800 |
commit | d861d799d2f614a6b58a8f0c0bc6a8911ad0dcfd (patch) | |
tree | adfe4096c07d7bf039b52d3d21d8ef742c8da837 /src/common/io/BufferStream.cpp | |
parent | dd62b63e1c577abe53d0f9cabe8e9a635c4c8fcf (diff) | |
download | cru-d861d799d2f614a6b58a8f0c0bc6a8911ad0dcfd.tar.gz cru-d861d799d2f614a6b58a8f0c0bc6a8911ad0dcfd.tar.bz2 cru-d861d799d2f614a6b58a8f0c0bc6a8911ad0dcfd.zip |
WORKING: fix Buffer and implement BufferStream::Read.
Diffstat (limited to 'src/common/io/BufferStream.cpp')
-rw-r--r-- | src/common/io/BufferStream.cpp | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/src/common/io/BufferStream.cpp b/src/common/io/BufferStream.cpp index d4780f7a..c08d04a5 100644 --- a/src/common/io/BufferStream.cpp +++ b/src/common/io/BufferStream.cpp @@ -1,5 +1,4 @@ #include "cru/common/io/BufferStream.h" -#include <memory> #include "cru/common/io/Stream.h" namespace cru::io { @@ -26,20 +25,39 @@ Index BufferStream::Seek(Index offset, SeekOrigin origin) { bool BufferStream::CanRead() { CheckClosed(); - return true; } + return true; +} Index BufferStream::Read(std::byte* buffer, Index offset, Index size) { std::unique_lock lock(mutex_); - Index written_size = 0; + condition_variable_.wait(lock, + [this] { return !buffer_list_.empty() || eof_; }); - if (eof_ && buffer_list_.empty()) { + if (eof_) { return 0; } + Index written_size = 0; + auto current_offset = offset; + auto rest_size = size; + while (!buffer_list_.empty()) { - + auto& stream_buffer = buffer_list_.front(); + auto this_written_size = + stream_buffer.PopFront(buffer + current_offset, rest_size); + if (stream_buffer.GetUsedSize() == 0) { + buffer_list_.pop_front(); + } + written_size += this_written_size; + rest_size -= this_written_size; + current_offset += this_written_size; + if (rest_size == 0) { + break; + } } + + return written_size; } } // namespace cru::io |