mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-28 21:54:40 +03: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
Notes:
sideshowbarker
2024-07-17 05:18:58 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/48b000a36c Pull-request: https://github.com/SerenityOS/serenity/pull/18590 Reviewed-by: https://github.com/timschumi ✅
@ -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)
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user