mirror of
https://github.com/RGBCube/serenity
synced 2025-05-20 13:55:08 +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:
parent
2a91245a96
commit
48b000a36c
2 changed files with 18 additions and 0 deletions
|
@ -206,6 +206,23 @@ ErrorOr<size_t> CircularBuffer::fill_from_stream(Stream& stream)
|
||||||
return bytes.size();
|
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)
|
ErrorOr<size_t> CircularBuffer::copy_from_seekback(size_t distance, size_t length)
|
||||||
{
|
{
|
||||||
if (distance > m_seekback_limit)
|
if (distance > m_seekback_limit)
|
||||||
|
|
|
@ -28,6 +28,7 @@ public:
|
||||||
Bytes read(Bytes bytes);
|
Bytes read(Bytes bytes);
|
||||||
ErrorOr<void> discard(size_t discarded_bytes);
|
ErrorOr<void> discard(size_t discarded_bytes);
|
||||||
ErrorOr<size_t> fill_from_stream(Stream&);
|
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
|
/// 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.
|
/// before the current write pointer and allows for reading already-read data.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue