1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:17:44 +00:00

LibCore: Add support for ReadonlyBytes to MemoryStream

This commit is contained in:
Tim Schumacher 2022-11-30 10:03:19 +01:00 committed by Sam Atkins
parent 38a882ddfa
commit d402f6cdb3
4 changed files with 22 additions and 6 deletions

View file

@ -22,6 +22,11 @@ public:
return adopt_nonnull_own_or_enomem<MemoryStream>(new (nothrow) MemoryStream(bytes));
}
static ErrorOr<NonnullOwnPtr<MemoryStream>> construct(ReadonlyBytes bytes)
{
return adopt_nonnull_own_or_enomem<MemoryStream>(new (nothrow) MemoryStream(bytes));
}
virtual bool is_eof() const override { return m_offset >= m_bytes.size(); }
virtual bool is_open() const override { return true; }
// FIXME: It doesn't make sense to close an memory stream. Therefore, we don't do anything here. Is that fine?
@ -67,6 +72,8 @@ public:
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
{
VERIFY(m_writing_enabled);
// FIXME: Can this not error?
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
m_offset += nwritten;
@ -81,7 +88,11 @@ public:
return true;
}
Bytes bytes() { return m_bytes; }
Bytes bytes()
{
VERIFY(m_writing_enabled);
return m_bytes;
}
ReadonlyBytes bytes() const { return m_bytes; }
size_t offset() const { return m_offset; }
size_t remaining() const { return m_bytes.size() - m_offset; }
@ -92,9 +103,16 @@ protected:
{
}
explicit MemoryStream(ReadonlyBytes bytes)
: m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
, m_writing_enabled(false)
{
}
private:
Bytes m_bytes;
size_t m_offset { 0 };
bool m_writing_enabled { true };
};
}