diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp index 364e8da109..2d80aaaabb 100644 --- a/AK/CircularBuffer.cpp +++ b/AK/CircularBuffer.cpp @@ -206,6 +206,23 @@ ErrorOr CircularBuffer::fill_from_stream(Stream& stream) return bytes.size(); } +ErrorOr 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 CircularBuffer::copy_from_seekback(size_t distance, size_t length) { if (distance > m_seekback_limit) diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h index 89861d0215..6b8e7c4be9 100644 --- a/AK/CircularBuffer.h +++ b/AK/CircularBuffer.h @@ -28,6 +28,7 @@ public: Bytes read(Bytes bytes); ErrorOr discard(size_t discarded_bytes); ErrorOr fill_from_stream(Stream&); + ErrorOr 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.