2022-01-13 19:14:23 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Error.h>
|
|
|
|
#include <AK/NonnullOwnPtr.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/Span.h>
|
|
|
|
#include <AK/TypedTransfer.h>
|
|
|
|
#include <LibCore/Stream.h>
|
|
|
|
|
|
|
|
namespace Core::Stream {
|
|
|
|
|
2022-12-07 09:47:44 -05:00
|
|
|
/// A stream class that allows for reading/writing on a preallocated memory area
|
|
|
|
/// using a single read/write head.
|
|
|
|
class FixedMemoryStream final : public SeekableStream {
|
2022-01-13 19:14:23 -05:00
|
|
|
public:
|
2022-12-07 09:47:44 -05:00
|
|
|
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(Bytes bytes)
|
2022-01-13 19:14:23 -05:00
|
|
|
{
|
2022-12-07 09:47:44 -05:00
|
|
|
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
|
2022-01-13 19:14:23 -05:00
|
|
|
}
|
|
|
|
|
2022-12-07 09:47:44 -05:00
|
|
|
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(ReadonlyBytes bytes)
|
2022-11-30 04:03:19 -05:00
|
|
|
{
|
2022-12-07 09:47:44 -05:00
|
|
|
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
|
2022-11-30 04:03:19 -05:00
|
|
|
}
|
|
|
|
|
2022-01-13 19:14:23 -05:00
|
|
|
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?
|
|
|
|
virtual void close() override { }
|
2022-02-03 14:21:51 -05:00
|
|
|
// FIXME: It doesn't make sense to truncate a memory stream. Therefore, we don't do anything here. Is that fine?
|
|
|
|
virtual ErrorOr<void> truncate(off_t) override { return Error::from_errno(ENOTSUP); }
|
2022-01-13 19:14:23 -05:00
|
|
|
|
2022-04-15 08:33:02 -04:00
|
|
|
virtual ErrorOr<Bytes> read(Bytes bytes) override
|
2022-01-13 19:14:23 -05:00
|
|
|
{
|
|
|
|
auto to_read = min(remaining(), bytes.size());
|
|
|
|
if (to_read == 0)
|
2022-04-15 08:33:02 -04:00
|
|
|
return Bytes {};
|
2022-01-13 19:14:23 -05:00
|
|
|
|
|
|
|
m_bytes.slice(m_offset, to_read).copy_to(bytes);
|
|
|
|
m_offset += to_read;
|
2022-04-15 08:33:02 -04:00
|
|
|
return bytes.trim(to_read);
|
2022-01-13 19:14:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ErrorOr<off_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override
|
|
|
|
{
|
|
|
|
switch (seek_mode) {
|
|
|
|
case SeekMode::SetPosition:
|
2022-10-12 11:07:15 -04:00
|
|
|
if (offset > static_cast<i64>(m_bytes.size()))
|
2022-07-11 13:57:32 -04:00
|
|
|
return Error::from_string_literal("Offset past the end of the stream memory");
|
2022-01-13 19:14:23 -05:00
|
|
|
|
|
|
|
m_offset = offset;
|
|
|
|
break;
|
|
|
|
case SeekMode::FromCurrentPosition:
|
2022-10-12 11:07:15 -04:00
|
|
|
if (offset + static_cast<i64>(m_offset) > static_cast<i64>(m_bytes.size()))
|
2022-07-11 13:57:32 -04:00
|
|
|
return Error::from_string_literal("Offset past the end of the stream memory");
|
2022-01-13 19:14:23 -05:00
|
|
|
|
|
|
|
m_offset += offset;
|
|
|
|
break;
|
|
|
|
case SeekMode::FromEndPosition:
|
2022-10-12 11:07:15 -04:00
|
|
|
if (offset > static_cast<i64>(m_bytes.size()))
|
2022-07-11 13:57:32 -04:00
|
|
|
return Error::from_string_literal("Offset past the start of the stream memory");
|
2022-01-13 19:14:23 -05:00
|
|
|
|
|
|
|
m_offset = m_bytes.size() - offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return static_cast<off_t>(m_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override
|
|
|
|
{
|
2022-11-30 04:03:19 -05:00
|
|
|
VERIFY(m_writing_enabled);
|
|
|
|
|
2022-01-13 19:14:23 -05:00
|
|
|
// FIXME: Can this not error?
|
2022-04-01 13:58:27 -04:00
|
|
|
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
|
2022-01-13 19:14:23 -05:00
|
|
|
m_offset += nwritten;
|
|
|
|
return nwritten;
|
|
|
|
}
|
2022-12-11 13:21:36 -05:00
|
|
|
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override
|
2022-01-13 19:14:23 -05:00
|
|
|
{
|
|
|
|
if (remaining() < bytes.size())
|
2022-12-11 13:21:36 -05:00
|
|
|
return Error::from_string_literal("Write of entire buffer ends past the memory area");
|
2022-01-13 19:14:23 -05:00
|
|
|
|
2022-12-11 13:21:36 -05:00
|
|
|
TRY(write(bytes));
|
|
|
|
return {};
|
2022-01-13 19:14:23 -05:00
|
|
|
}
|
|
|
|
|
2022-11-30 04:03:19 -05:00
|
|
|
Bytes bytes()
|
|
|
|
{
|
|
|
|
VERIFY(m_writing_enabled);
|
|
|
|
return m_bytes;
|
|
|
|
}
|
2022-01-13 19:14:23 -05:00
|
|
|
ReadonlyBytes bytes() const { return m_bytes; }
|
|
|
|
size_t offset() const { return m_offset; }
|
|
|
|
size_t remaining() const { return m_bytes.size() - m_offset; }
|
|
|
|
|
|
|
|
protected:
|
2022-12-07 09:47:44 -05:00
|
|
|
explicit FixedMemoryStream(Bytes bytes)
|
2022-01-13 19:14:23 -05:00
|
|
|
: m_bytes(bytes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-07 09:47:44 -05:00
|
|
|
explicit FixedMemoryStream(ReadonlyBytes bytes)
|
2022-11-30 04:03:19 -05:00
|
|
|
: m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
|
|
|
|
, m_writing_enabled(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-01-13 19:14:23 -05:00
|
|
|
private:
|
|
|
|
Bytes m_bytes;
|
|
|
|
size_t m_offset { 0 };
|
2022-11-30 04:03:19 -05:00
|
|
|
bool m_writing_enabled { true };
|
2022-01-13 19:14:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|