diff options
author | asynts <asynts@gmail.com> | 2020-09-01 11:55:04 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-01 17:25:26 +0200 |
commit | 3a2658951b6a8b36193dbdb29dae5e4b9afc8263 (patch) | |
tree | 93c8595ad64594f09f69c528fe1e2c9f82a9d994 | |
parent | b68a8730678c07d597eefd4d3a8b796412416a82 (diff) | |
download | serenity-3a2658951b6a8b36193dbdb29dae5e4b9afc8263.zip |
AK: Add DuplexMemoryStream::copy_into_contiguous_buffer.
-rw-r--r-- | AK/MemoryStream.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h index 96df59f3c9..dc8818698e 100644 --- a/AK/MemoryStream.h +++ b/AK/MemoryStream.h @@ -225,7 +225,7 @@ public: return {}; } - size_t read(Bytes bytes) override + size_t read_without_consuming(Bytes bytes) const { size_t nread = 0; while (bytes.size() - nread > 0 && m_write_offset - m_read_offset - nread > 0) { @@ -234,8 +234,14 @@ public: nread += chunk_bytes.copy_trimmed_to(bytes.slice(nread)); } - m_read_offset += nread; + return nread; + } + + size_t read(Bytes bytes) override + { + const auto nread = read_without_consuming(bytes); + m_read_offset += nread; try_discard_chunks(); return nread; @@ -272,6 +278,16 @@ public: return true; } + ByteBuffer copy_into_contiguous_buffer() const + { + auto buffer = ByteBuffer::create_uninitialized(remaining()); + + const auto nread = read_without_consuming(buffer); + ASSERT(nread == buffer.size()); + + return buffer; + } + size_t roffset() const { return m_read_offset; } size_t woffset() const { return m_write_offset; } |