1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-05 23:29:58 +00:00

AK: Add CircularBuffer::flush_to_stream

In a similar fashion to what have been done with `fill_from_stream`,
this new method allows to write CircularBuffer's data to a Stream
without additional copies.
This commit is contained in:
Lucas CHOLLET 2023-04-30 20:56:20 -04:00 committed by Andreas Kling
parent 2a91245a96
commit 48b000a36c
2 changed files with 18 additions and 0 deletions

View File

@ -206,6 +206,23 @@ ErrorOr<size_t> CircularBuffer::fill_from_stream(Stream& stream)
return bytes.size();
}
ErrorOr<size_t> CircularBuffer::flush_to_stream(Stream& stream)
{
auto next_span = next_read_span();
if (next_span.size() == 0)
return 0;
auto written_bytes = TRY(stream.write_some(next_span));
m_used_space -= written_bytes;
m_reading_head += written_bytes;
if (m_reading_head >= capacity())
m_reading_head -= capacity();
return written_bytes;
}
ErrorOr<size_t> CircularBuffer::copy_from_seekback(size_t distance, size_t length)
{
if (distance > m_seekback_limit)

View File

@ -28,6 +28,7 @@ public:
Bytes read(Bytes bytes);
ErrorOr<void> discard(size_t discarded_bytes);
ErrorOr<size_t> fill_from_stream(Stream&);
ErrorOr<size_t> flush_to_stream(Stream&);
/// Compared to `read()`, this starts reading from an offset that is `distance` bytes
/// before the current write pointer and allows for reading already-read data.