diff --git a/AK/BufferedStream.h b/AK/BufferedStream.h index 4b454d15e9f..ab66e8c78ad 100644 --- a/AK/BufferedStream.h +++ b/AK/BufferedStream.h @@ -231,12 +231,11 @@ private: if (m_buffer.empty_space() == 0) return 0; - // TODO: Figure out if we can do direct writes in a comfortable way. - Array temporary_buffer; - auto const fillable_slice = temporary_buffer.span().trim(min(temporary_buffer.size(), m_buffer.empty_space())); size_t nread = 0; - do { - auto result = stream().read_some(fillable_slice); + + while (true) { + auto result = m_buffer.fill_from_stream(stream()); + if (result.is_error()) { if (!result.error().is_errno()) return result.release_error(); @@ -246,11 +245,11 @@ private: break; return result.release_error(); } - auto const filled_slice = result.value(); - VERIFY(m_buffer.write(filled_slice) == filled_slice.size()); - nread += filled_slice.size(); + + nread += result.value(); break; - } while (true); + } + return nread; } diff --git a/AK/CircularBuffer.cpp b/AK/CircularBuffer.cpp index 0c6363c9fed..6ffe7ede668 100644 --- a/AK/CircularBuffer.cpp +++ b/AK/CircularBuffer.cpp @@ -6,6 +6,7 @@ #include #include +#include namespace AK { @@ -189,4 +190,20 @@ ErrorOr CircularBuffer::discard(size_t discarding_size) return {}; } +ErrorOr CircularBuffer::fill_from_stream(Stream& stream) +{ + auto next_span = next_write_span(); + if (next_span.size() == 0) + return 0; + + auto bytes = TRY(stream.read_some(next_span)); + m_used_space += bytes.size(); + + m_seekback_limit += bytes.size(); + if (m_seekback_limit > capacity()) + m_seekback_limit = capacity(); + + return bytes.size(); +} + } diff --git a/AK/CircularBuffer.h b/AK/CircularBuffer.h index 98e9131d6cc..ffeb887319d 100644 --- a/AK/CircularBuffer.h +++ b/AK/CircularBuffer.h @@ -27,6 +27,7 @@ public: size_t write(ReadonlyBytes bytes); Bytes read(Bytes bytes); ErrorOr discard(size_t discarded_bytes); + ErrorOr fill_from_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.