diff --git a/AK/ConstrainedStream.cpp b/AK/ConstrainedStream.cpp index 3600366f11f..31d1f520d58 100644 --- a/AK/ConstrainedStream.cpp +++ b/AK/ConstrainedStream.cpp @@ -15,13 +15,6 @@ ConstrainedStream::ConstrainedStream(MaybeOwned stream, u64 limit) { } -ErrorOr ConstrainedStream::read_some(Bytes bytes) -{ - auto result = TRY(m_stream->read_some(bytes.trim(m_limit))); - m_limit -= result.size(); - return result; -} - ErrorOr ConstrainedStream::discard(size_t discarded_bytes) { if (discarded_bytes >= m_limit) diff --git a/AK/ConstrainedStream.h b/AK/ConstrainedStream.h index 7ef92b26382..a7cd0863241 100644 --- a/AK/ConstrainedStream.h +++ b/AK/ConstrainedStream.h @@ -16,7 +16,12 @@ class ConstrainedStream : public Stream { public: ConstrainedStream(MaybeOwned, u64 limit); - virtual ErrorOr read_some(Bytes) override; + virtual ErrorOr read_some(Bytes bytes) override + { + auto result = TRY(m_stream->read_some(bytes.trim(m_limit))); + m_limit -= result.size(); + return result; + } virtual ErrorOr discard(size_t discarded_bytes) override; virtual ErrorOr write_some(ReadonlyBytes) override; virtual bool is_eof() const override; diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index b6398d881e0..62c6d11cc0e 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -44,13 +44,6 @@ ErrorOr FixedMemoryStream::truncate(size_t) return Error::from_errno(EBADF); } -ErrorOr FixedMemoryStream::read_some(Bytes bytes) -{ - auto read = m_bytes.slice(m_offset).copy_trimmed_to(bytes); - m_offset += read; - return bytes.trim(read); -} - ErrorOr FixedMemoryStream::read_until_filled(AK::Bytes bytes) { if (remaining() < bytes.size()) diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h index fa8f9f1303f..e72d63ac8a8 100644 --- a/AK/MemoryStream.h +++ b/AK/MemoryStream.h @@ -30,7 +30,12 @@ public: virtual bool is_open() const override; virtual void close() override; virtual ErrorOr truncate(size_t) override; - virtual ErrorOr read_some(Bytes bytes) override; + virtual ErrorOr read_some(Bytes bytes) override + { + auto read = m_bytes.slice(m_offset).copy_trimmed_to(bytes); + m_offset += read; + return bytes.trim(read); + } virtual ErrorOr read_until_filled(Bytes bytes) override; virtual ErrorOr seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override; diff --git a/AK/Stream.cpp b/AK/Stream.cpp index fc043093ad1..7a870f78fd8 100644 --- a/AK/Stream.cpp +++ b/AK/Stream.cpp @@ -12,28 +12,6 @@ namespace AK { -ErrorOr Stream::read_until_filled(Bytes buffer) -{ - size_t nread = 0; - while (nread < buffer.size()) { - if (is_eof()) - return Error::from_string_literal("Reached end-of-file before filling the entire buffer"); - - auto result = read_some(buffer.slice(nread)); - if (result.is_error()) { - if (result.error().is_errno() && result.error().code() == EINTR) { - continue; - } - - return result.release_error(); - } - - nread += result.value().size(); - } - - return {}; -} - ErrorOr Stream::read_until_eof(size_t block_size) { return read_until_eof_impl(block_size); diff --git a/AK/Stream.h b/AK/Stream.h index 5fbe0bee433..5516975a58b 100644 --- a/AK/Stream.h +++ b/AK/Stream.h @@ -29,7 +29,27 @@ public: virtual ErrorOr read_some(Bytes) = 0; /// Tries to fill the entire buffer through reading. Returns whether the /// buffer was filled without an error. - virtual ErrorOr read_until_filled(Bytes); + virtual ErrorOr read_until_filled(Bytes buffer) + { + size_t nread = 0; + while (nread < buffer.size()) { + if (is_eof()) + return Error::from_string_literal("Reached end-of-file before filling the entire buffer"); + + auto result = read_some(buffer.slice(nread)); + if (result.is_error()) { + if (result.error().is_errno() && result.error().code() == EINTR) { + continue; + } + + return result.release_error(); + } + + nread += result.value().size(); + } + + return {}; + } /// Reads the stream until EOF, storing the contents into a ByteBuffer which /// is returned once EOF is encountered. The block size determines the size /// of newly allocated chunks while reading.