mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
AK: Re-add OutputMemoryStream for static buffers only.
This commit is contained in:
parent
f18e927827
commit
83d0803861
Notes:
sideshowbarker
2024-07-19 02:38:26 +09:00
Author: https://github.com/asynts Commit: https://github.com/SerenityOS/serenity/commit/83d0803861d Pull-request: https://github.com/SerenityOS/serenity/pull/3496
3 changed files with 66 additions and 4 deletions
|
@ -53,6 +53,7 @@ class InputMemoryStream;
|
|||
class DuplexMemoryStream;
|
||||
class OutputStream;
|
||||
class InputBitStream;
|
||||
class OutputMemoryStream;
|
||||
|
||||
template<size_t Capacity>
|
||||
class CircularDuplexStream;
|
||||
|
@ -153,6 +154,7 @@ using AK::LogStream;
|
|||
using AK::NonnullOwnPtr;
|
||||
using AK::NonnullRefPtr;
|
||||
using AK::Optional;
|
||||
using AK::OutputMemoryStream;
|
||||
using AK::OutputStream;
|
||||
using AK::OwnPtr;
|
||||
using AK::ReadonlyBytes;
|
||||
|
|
|
@ -35,7 +35,7 @@ namespace AK {
|
|||
|
||||
class InputMemoryStream final : public InputStream {
|
||||
public:
|
||||
InputMemoryStream(ReadonlyBytes bytes)
|
||||
explicit InputMemoryStream(ReadonlyBytes bytes)
|
||||
: m_bytes(bytes)
|
||||
{
|
||||
}
|
||||
|
@ -161,9 +161,45 @@ private:
|
|||
size_t m_offset { 0 };
|
||||
};
|
||||
|
||||
// All data written to this stream can be read from it. Reading and writing is done
|
||||
// using different offsets, meaning that it is not necessary to seek to the start
|
||||
// before reading; this behaviour differs from BufferStream.
|
||||
class OutputMemoryStream final : public OutputStream {
|
||||
public:
|
||||
explicit OutputMemoryStream(Bytes bytes)
|
||||
: m_bytes(bytes)
|
||||
{
|
||||
}
|
||||
|
||||
size_t write(ReadonlyBytes bytes) override
|
||||
{
|
||||
const auto nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
|
||||
m_offset += nwritten;
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
bool write_or_error(ReadonlyBytes bytes) override
|
||||
{
|
||||
if (remaining() < bytes.size()) {
|
||||
set_recoverable_error();
|
||||
return false;
|
||||
}
|
||||
|
||||
write(bytes);
|
||||
return true;
|
||||
}
|
||||
|
||||
ReadonlyBytes bytes() const { return { data(), size() }; }
|
||||
Bytes bytes() { return { data(), size() }; }
|
||||
|
||||
const u8* data() const { return m_bytes.data(); }
|
||||
u8* data() { return m_bytes.data(); }
|
||||
|
||||
size_t size() const { return m_offset; }
|
||||
size_t remaining() const { return m_bytes.size() - m_offset; }
|
||||
|
||||
private:
|
||||
size_t m_offset { 0 };
|
||||
Bytes m_bytes;
|
||||
};
|
||||
|
||||
class DuplexMemoryStream final : public DuplexStream {
|
||||
public:
|
||||
static constexpr size_t chunk_size = 4 * 1024;
|
||||
|
@ -321,3 +357,4 @@ private:
|
|||
using AK::DuplexMemoryStream;
|
||||
using AK::InputMemoryStream;
|
||||
using AK::InputStream;
|
||||
using AK::OutputMemoryStream;
|
||||
|
|
|
@ -171,4 +171,27 @@ TEST_CASE(write_endian_values)
|
|||
EXPECT(compare({ expected, sizeof(expected) }, stream.copy_into_contiguous_buffer()));
|
||||
}
|
||||
|
||||
TEST_CASE(new_output_memory_stream)
|
||||
{
|
||||
Array<u8, 16> buffer;
|
||||
OutputMemoryStream stream { buffer };
|
||||
|
||||
EXPECT_EQ(stream.size(), 0u);
|
||||
EXPECT_EQ(stream.remaining(), 16u);
|
||||
|
||||
stream << LittleEndian<u16>(0x12'87);
|
||||
|
||||
EXPECT_EQ(stream.size(), 2u);
|
||||
EXPECT_EQ(stream.remaining(), 14u);
|
||||
|
||||
stream << buffer;
|
||||
|
||||
EXPECT(stream.handle_recoverable_error());
|
||||
EXPECT_EQ(stream.size(), 2u);
|
||||
EXPECT_EQ(stream.remaining(), 14u);
|
||||
|
||||
EXPECT_EQ(stream.bytes().data(), buffer.data());
|
||||
EXPECT_EQ(stream.bytes().size(), 2u);
|
||||
}
|
||||
|
||||
TEST_MAIN(MemoryStream)
|
||||
|
|
Loading…
Reference in a new issue