/* * Copyright (c) 2021, kleines Filmröllchen . * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include namespace AK { /// A stream class that allows for reading/writing on a preallocated memory area /// using a single read/write head. class FixedMemoryStream final : public SeekableStream { public: static ErrorOr> construct(Bytes bytes); static ErrorOr> construct(ReadonlyBytes bytes); virtual bool is_eof() const override; virtual bool is_open() const override; virtual void close() override; virtual ErrorOr truncate(size_t) override; virtual ErrorOr read(Bytes bytes) override; virtual ErrorOr seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override; virtual ErrorOr write(ReadonlyBytes bytes) override; virtual ErrorOr write_entire_buffer(ReadonlyBytes bytes) override; Bytes bytes(); ReadonlyBytes bytes() const; size_t offset() const; size_t remaining() const; private: explicit FixedMemoryStream(Bytes bytes); explicit FixedMemoryStream(ReadonlyBytes bytes); Bytes m_bytes; size_t m_offset { 0 }; bool m_writing_enabled { true }; }; /// A stream class that allows for writing to an automatically allocating memory area /// and reading back the written data afterwards. class AllocatingMemoryStream final : public Stream { public: virtual ErrorOr read(Bytes) override; virtual ErrorOr write(ReadonlyBytes) override; virtual ErrorOr discard(size_t) override; virtual bool is_eof() const override; virtual bool is_open() const override; virtual void close() override; size_t used_buffer_size() const; ErrorOr> offset_of(ReadonlyBytes needle) const; private: // Note: We set the inline buffer capacity to zero to make moving chunks as efficient as possible. using Chunk = AK::Detail::ByteBuffer<0>; static constexpr size_t chunk_size = 4096; ErrorOr next_read_range(); ErrorOr next_write_range(); void cleanup_unused_chunks(); Vector m_chunks; size_t m_read_offset = 0; size_t m_write_offset = 0; }; } #if USING_AK_GLOBALLY using AK::AllocatingMemoryStream; using AK::FixedMemoryStream; #endif