2022-01-14 00:14:23 +00: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 14:47:44 +00: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-14 00:14:23 +00:00
|
|
|
public:
|
2022-12-07 15:22:14 +00:00
|
|
|
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(Bytes bytes);
|
|
|
|
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(ReadonlyBytes bytes);
|
2022-01-14 00:14:23 +00:00
|
|
|
|
2022-12-07 15:22:14 +00:00
|
|
|
virtual bool is_eof() const override;
|
|
|
|
virtual bool is_open() const override;
|
|
|
|
virtual void close() override;
|
|
|
|
virtual ErrorOr<void> truncate(off_t) override;
|
|
|
|
virtual ErrorOr<Bytes> read(Bytes bytes) override;
|
2022-11-30 09:03:19 +00:00
|
|
|
|
2022-12-07 15:22:14 +00:00
|
|
|
virtual ErrorOr<off_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;
|
2022-01-14 00:14:23 +00:00
|
|
|
|
2022-12-07 15:22:14 +00:00
|
|
|
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override;
|
|
|
|
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override;
|
2022-01-14 00:14:23 +00:00
|
|
|
|
2022-12-07 15:22:14 +00:00
|
|
|
Bytes bytes();
|
|
|
|
ReadonlyBytes bytes() const;
|
|
|
|
size_t offset() const;
|
|
|
|
size_t remaining() const;
|
2022-11-30 09:03:19 +00:00
|
|
|
|
2022-01-14 00:14:23 +00:00
|
|
|
private:
|
2022-12-07 15:22:14 +00:00
|
|
|
explicit FixedMemoryStream(Bytes bytes);
|
|
|
|
explicit FixedMemoryStream(ReadonlyBytes bytes);
|
|
|
|
|
2022-01-14 00:14:23 +00:00
|
|
|
Bytes m_bytes;
|
|
|
|
size_t m_offset { 0 };
|
2022-11-30 09:03:19 +00:00
|
|
|
bool m_writing_enabled { true };
|
2022-01-14 00:14:23 +00:00
|
|
|
};
|
|
|
|
|
2022-12-07 16:43:31 +00:00
|
|
|
/// 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<Bytes> read(Bytes) override;
|
|
|
|
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
|
|
|
|
virtual ErrorOr<void> 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;
|
|
|
|
|
|
|
|
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<ReadonlyBytes> next_read_range();
|
|
|
|
ErrorOr<Bytes> next_write_range();
|
|
|
|
void cleanup_unused_chunks();
|
|
|
|
|
|
|
|
Vector<Chunk> m_chunks;
|
|
|
|
size_t m_read_offset = 0;
|
|
|
|
size_t m_write_offset = 0;
|
|
|
|
};
|
|
|
|
|
2022-01-14 00:14:23 +00:00
|
|
|
}
|