1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 18:07:35 +00:00

AK: Remove OutputMemoryStream for DuplexMemoryStream.

OutputMemoryStream was originally a proxy for DuplexMemoryStream that
did not expose any reading API.

Now I need to add another class that is like OutputMemoryStream but only
for static buffers. My first idea was to make OutputMemoryStream do that
too, but I think it's much better to have a distinct class for that.

I originally wanted to call that class FixedOutputMemoryStream but that
name is really cumbersome and it's a bit unintuitive because
InputMemoryStream is already reading from a fixed buffer.

So let's just use DuplexMemoryStream instead of OutputMemoryStream for
any dynamic stuff and create a new OutputMemoryStream for static
buffers.
This commit is contained in:
asynts 2020-09-15 11:48:54 +02:00 committed by Andreas Kling
parent c9a3a5b488
commit f18e927827
6 changed files with 10 additions and 28 deletions

View file

@ -53,7 +53,6 @@ class InputMemoryStream;
class DuplexMemoryStream; class DuplexMemoryStream;
class OutputStream; class OutputStream;
class InputBitStream; class InputBitStream;
class OutputMemoryStream;
template<size_t Capacity> template<size_t Capacity>
class CircularDuplexStream; class CircularDuplexStream;
@ -154,7 +153,6 @@ using AK::LogStream;
using AK::NonnullOwnPtr; using AK::NonnullOwnPtr;
using AK::NonnullRefPtr; using AK::NonnullRefPtr;
using AK::Optional; using AK::Optional;
using AK::OutputMemoryStream;
using AK::OutputStream; using AK::OutputStream;
using AK::OwnPtr; using AK::OwnPtr;
using AK::ReadonlyBytes; using AK::ReadonlyBytes;

View file

@ -185,7 +185,7 @@ public:
Optional<size_t> offset_of(ReadonlyBytes value) const Optional<size_t> offset_of(ReadonlyBytes value) const
{ {
if (value.size() > remaining()) if (value.size() > size())
return {}; return {};
// First, find which chunk we're in. // First, find which chunk we're in.
@ -288,7 +288,7 @@ public:
ByteBuffer copy_into_contiguous_buffer() const ByteBuffer copy_into_contiguous_buffer() const
{ {
auto buffer = ByteBuffer::create_uninitialized(remaining()); auto buffer = ByteBuffer::create_uninitialized(size());
const auto nread = read_without_consuming(buffer); const auto nread = read_without_consuming(buffer);
ASSERT(nread == buffer.size()); ASSERT(nread == buffer.size());
@ -299,7 +299,7 @@ public:
size_t roffset() const { return m_read_offset; } size_t roffset() const { return m_read_offset; }
size_t woffset() const { return m_write_offset; } size_t woffset() const { return m_write_offset; }
size_t remaining() const { return m_write_offset - m_read_offset; } size_t size() const { return m_write_offset - m_read_offset; }
private: private:
void try_discard_chunks() void try_discard_chunks()
@ -316,24 +316,8 @@ private:
size_t m_base_offset { 0 }; size_t m_base_offset { 0 };
}; };
class OutputMemoryStream final : public OutputStream {
public:
size_t write(ReadonlyBytes bytes) override { return m_stream.write(bytes); }
bool write_or_error(ReadonlyBytes bytes) override { return m_stream.write_or_error(bytes); }
ByteBuffer copy_into_contiguous_buffer() const { return m_stream.copy_into_contiguous_buffer(); }
Optional<size_t> offset_of(ReadonlyBytes value) const { return m_stream.offset_of(value); }
size_t size() const { return m_stream.woffset(); }
private:
DuplexMemoryStream m_stream;
};
} }
using AK::DuplexMemoryStream; using AK::DuplexMemoryStream;
using AK::InputMemoryStream; using AK::InputMemoryStream;
using AK::InputStream; using AK::InputStream;
using AK::OutputMemoryStream;

View file

@ -129,17 +129,17 @@ TEST_CASE(duplex_large_buffer)
Array<u8, 1024> one_kibibyte; Array<u8, 1024> one_kibibyte;
EXPECT_EQ(stream.remaining(), 0ul); EXPECT_EQ(stream.size(), 0ul);
for (size_t idx = 0; idx < 256; ++idx) for (size_t idx = 0; idx < 256; ++idx)
stream << one_kibibyte; stream << one_kibibyte;
EXPECT_EQ(stream.remaining(), 256 * 1024ul); EXPECT_EQ(stream.size(), 256 * 1024ul);
for (size_t idx = 0; idx < 128; ++idx) for (size_t idx = 0; idx < 128; ++idx)
stream >> one_kibibyte; stream >> one_kibibyte;
EXPECT_EQ(stream.remaining(), 128 * 1024ul); EXPECT_EQ(stream.size(), 128 * 1024ul);
for (size_t idx = 0; idx < 128; ++idx) for (size_t idx = 0; idx < 128; ++idx)
stream >> one_kibibyte; stream >> one_kibibyte;
@ -164,7 +164,7 @@ TEST_CASE(write_endian_values)
{ {
const u8 expected[] { 4, 3, 2, 1, 1, 2, 3, 4 }; const u8 expected[] { 4, 3, 2, 1, 1, 2, 3, 4 };
OutputMemoryStream stream; DuplexMemoryStream stream;
stream << LittleEndian<u32> { 0x01020304 } << BigEndian<u32> { 0x01020304 }; stream << LittleEndian<u32> { 0x01020304 } << BigEndian<u32> { 0x01020304 };
EXPECT_EQ(stream.size(), 8u); EXPECT_EQ(stream.size(), 8u);

View file

@ -307,7 +307,7 @@ Optional<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
{ {
InputMemoryStream memory_stream { bytes }; InputMemoryStream memory_stream { bytes };
DeflateDecompressor deflate_stream { memory_stream }; DeflateDecompressor deflate_stream { memory_stream };
OutputMemoryStream output_stream; DuplexMemoryStream output_stream;
u8 buffer[4096]; u8 buffer[4096];
while (!deflate_stream.has_any_error() && !deflate_stream.unreliable_eof()) { while (!deflate_stream.has_any_error() && !deflate_stream.unreliable_eof()) {

View file

@ -164,7 +164,7 @@ Optional<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
{ {
InputMemoryStream memory_stream { bytes }; InputMemoryStream memory_stream { bytes };
GzipDecompressor gzip_stream { memory_stream }; GzipDecompressor gzip_stream { memory_stream };
OutputMemoryStream output_stream; DuplexMemoryStream output_stream;
u8 buffer[4096]; u8 buffer[4096];
while (!gzip_stream.has_any_error() && !gzip_stream.unreliable_eof()) { while (!gzip_stream.has_any_error() && !gzip_stream.unreliable_eof()) {

View file

@ -1107,7 +1107,7 @@ void Execute::for_each_entry(RefPtr<Shell> shell, Function<IterationDecision(Ref
} while (action == Continue); } while (action == Continue);
if (!stream.eof()) { if (!stream.eof()) {
auto entry = ByteBuffer::create_uninitialized(stream.remaining()); auto entry = ByteBuffer::create_uninitialized(stream.size());
auto rc = stream.read_or_error(entry); auto rc = stream.read_or_error(entry);
ASSERT(rc); ASSERT(rc);
callback(create<StringValue>(String::copy(entry))); callback(create<StringValue>(String::copy(entry)));