1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:57:35 +00:00

AK: Move memory streams from LibCore

This commit is contained in:
Tim Schumacher 2023-01-25 20:19:05 +01:00 committed by Andrew Kaster
parent 11550f582b
commit 093cf428a3
46 changed files with 213 additions and 203 deletions

View file

@ -11,6 +11,7 @@
#include <AK/Format.h>
#include <AK/IntegralMath.h>
#include <AK/Math.h>
#include <AK/MemoryStream.h>
#include <AK/ScopeGuard.h>
#include <AK/StdLibExtras.h>
#include <AK/Try.h>
@ -20,7 +21,6 @@
#include <LibAudio/FlacTypes.h>
#include <LibAudio/LoaderError.h>
#include <LibAudio/Resampler.h>
#include <LibCore/MemoryStream.h>
#include <LibCore/Stream.h>
namespace Audio {
@ -42,7 +42,7 @@ Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(St
Result<NonnullOwnPtr<FlacLoaderPlugin>, LoaderError> FlacLoaderPlugin::create(Bytes buffer)
{
auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer));
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer));
auto loader = make<FlacLoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize());
@ -78,7 +78,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
// Receive the streaminfo block
auto streaminfo = TRY(next_meta_block(*bit_input));
FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
auto streaminfo_data_memory = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(streaminfo.data.bytes()));
auto streaminfo_data_memory = LOADER_TRY(FixedMemoryStream::construct(streaminfo.data.bytes()));
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*streaminfo_data_memory)));
// 11.10 METADATA_BLOCK_STREAMINFO
@ -149,7 +149,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
// 11.19. METADATA_BLOCK_PICTURE
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes()));
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
PictureData picture {};
@ -186,7 +186,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
// 11.13. METADATA_BLOCK_SEEKTABLE
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
{
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
auto memory_stream = LOADER_TRY(FixedMemoryStream::construct(block.data.bytes()));
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<AK::Stream>(*memory_stream)));
for (size_t i = 0; i < block.length / 18; ++i) {
// 11.14. SEEKPOINT

View file

@ -12,7 +12,6 @@
#include <AK/Error.h>
#include <AK/Span.h>
#include <AK/Types.h>
#include <LibCore/MemoryStream.h>
#include <LibCore/Stream.h>
namespace Audio {

View file

@ -31,7 +31,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Stri
Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Bytes buffer)
{
auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer));
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer));
auto loader = make<MP3LoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize());

View file

@ -9,8 +9,8 @@
#include "Loader.h"
#include "MP3Types.h"
#include <AK/BitStream.h>
#include <AK/MemoryStream.h>
#include <AK/Tuple.h>
#include <LibCore/MemoryStream.h>
#include <LibCore/Stream.h>
#include <LibDSP/MDCT.h>
@ -73,7 +73,7 @@ private:
AK::Optional<MP3::MP3Frame> m_current_frame;
u32 m_current_frame_read;
OwnPtr<BigEndianInputBitStream> m_bitstream;
Core::Stream::AllocatingMemoryStream m_bit_reservoir;
AllocatingMemoryStream m_bit_reservoir;
};
}

View file

@ -10,9 +10,9 @@
#include <AK/Debug.h>
#include <AK/Endian.h>
#include <AK/FixedArray.h>
#include <AK/MemoryStream.h>
#include <AK/NumericLimits.h>
#include <AK/Try.h>
#include <LibCore/MemoryStream.h>
namespace Audio {
@ -35,7 +35,7 @@ Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Stri
Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Bytes buffer)
{
auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer));
auto stream = LOADER_TRY(FixedMemoryStream::construct(buffer));
auto loader = make<WavLoaderPlugin>(move(stream));
LOADER_TRY(loader->initialize());
@ -115,7 +115,7 @@ static ErrorOr<double> read_sample(AK::Stream& stream)
LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const
{
FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::create(samples_to_read));
auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(move(data)));
auto stream = LOADER_TRY(FixedMemoryStream::construct(move(data)));
switch (m_sample_format) {
case PcmSampleFormat::Uint8:

View file

@ -10,7 +10,7 @@
#include <AK/BinaryHeap.h>
#include <AK/BinarySearch.h>
#include <AK/BitStream.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <string.h>
#include <LibCompress/Deflate.h>
@ -317,9 +317,9 @@ void DeflateDecompressor::close()
ErrorOr<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
{
auto memory_stream = TRY(Core::Stream::FixedMemoryStream::construct(bytes));
auto memory_stream = TRY(FixedMemoryStream::construct(bytes));
auto deflate_stream = TRY(DeflateDecompressor::construct(move(memory_stream)));
Core::Stream::AllocatingMemoryStream output_stream;
AllocatingMemoryStream output_stream;
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (!deflate_stream->is_eof()) {
@ -1017,7 +1017,7 @@ ErrorOr<void> DeflateCompressor::final_flush()
ErrorOr<ByteBuffer> DeflateCompressor::compress_all(ReadonlyBytes bytes, CompressionLevel compression_level)
{
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
auto output_stream = TRY(try_make<AllocatingMemoryStream>());
auto deflate_stream = TRY(DeflateCompressor::construct(MaybeOwned<AK::Stream>(*output_stream), compression_level));
TRY(deflate_stream->write_entire_buffer(bytes));

View file

@ -8,8 +8,8 @@
#include <LibCompress/Gzip.h>
#include <AK/DeprecatedString.h>
#include <AK/MemoryStream.h>
#include <LibCore/DateTime.h>
#include <LibCore/MemoryStream.h>
namespace Compress {
@ -164,9 +164,9 @@ Optional<DeprecatedString> GzipDecompressor::describe_header(ReadonlyBytes bytes
ErrorOr<ByteBuffer> GzipDecompressor::decompress_all(ReadonlyBytes bytes)
{
auto memory_stream = TRY(Core::Stream::FixedMemoryStream::construct(bytes));
auto memory_stream = TRY(FixedMemoryStream::construct(bytes));
auto gzip_stream = make<GzipDecompressor>(move(memory_stream));
Core::Stream::AllocatingMemoryStream output_stream;
AllocatingMemoryStream output_stream;
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
while (!gzip_stream->is_eof()) {
@ -235,7 +235,7 @@ void GzipCompressor::close()
ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes)
{
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
auto output_stream = TRY(try_make<AllocatingMemoryStream>());
GzipCompressor gzip_stream { MaybeOwned<AK::Stream>(*output_stream) };
TRY(gzip_stream.write_entire_buffer(bytes));

View file

@ -5,12 +5,12 @@
*/
#include <AK/IntegralMath.h>
#include <AK/MemoryStream.h>
#include <AK/Span.h>
#include <AK/TypeCasts.h>
#include <AK/Types.h>
#include <LibCompress/Deflate.h>
#include <LibCompress/Zlib.h>
#include <LibCore/MemoryStream.h>
namespace Compress {
@ -163,7 +163,7 @@ ErrorOr<void> ZlibCompressor::finish()
ErrorOr<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, ZlibCompressionLevel compression_level)
{
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
auto output_stream = TRY(try_make<AllocatingMemoryStream>());
auto zlib_stream = TRY(ZlibCompressor::construct(MaybeOwned<AK::Stream>(*output_stream), compression_level));
TRY(zlib_stream->write_entire_buffer(bytes));

View file

@ -13,7 +13,6 @@ set(SOURCES
IODevice.cpp
LockFile.cpp
MappedFile.cpp
MemoryStream.cpp
MimeData.cpp
NetworkJob.cpp
Notifier.cpp

View file

@ -1,274 +0,0 @@
/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
* Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FixedArray.h>
#include <AK/MemMem.h>
#include <LibCore/MemoryStream.h>
namespace Core::Stream {
FixedMemoryStream::FixedMemoryStream(Bytes bytes)
: m_bytes(bytes)
{
}
FixedMemoryStream::FixedMemoryStream(ReadonlyBytes bytes)
: m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
, m_writing_enabled(false)
{
}
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(Bytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(ReadonlyBytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
bool FixedMemoryStream::is_eof() const
{
return m_offset >= m_bytes.size();
}
bool FixedMemoryStream::is_open() const
{
return true;
}
void FixedMemoryStream::close()
{
// FIXME: It doesn't make sense to close a memory stream. Therefore, we don't do anything here. Is that fine?
}
ErrorOr<void> FixedMemoryStream::truncate(off_t)
{
return Error::from_errno(EBADF);
}
ErrorOr<Bytes> FixedMemoryStream::read(Bytes bytes)
{
auto to_read = min(remaining(), bytes.size());
if (to_read == 0)
return Bytes {};
m_bytes.slice(m_offset, to_read).copy_to(bytes);
m_offset += to_read;
return bytes.trim(to_read);
}
ErrorOr<size_t> FixedMemoryStream::seek(i64 offset, SeekMode seek_mode)
{
switch (seek_mode) {
case SeekMode::SetPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
m_offset = offset;
break;
case SeekMode::FromCurrentPosition:
if (offset + static_cast<i64>(m_offset) > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
m_offset += offset;
break;
case SeekMode::FromEndPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the start of the stream memory");
m_offset = m_bytes.size() - offset;
break;
}
return m_offset;
}
ErrorOr<size_t> FixedMemoryStream::write(ReadonlyBytes bytes)
{
VERIFY(m_writing_enabled);
// FIXME: Can this not error?
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
m_offset += nwritten;
return nwritten;
}
ErrorOr<void> FixedMemoryStream::write_entire_buffer(ReadonlyBytes bytes)
{
if (remaining() < bytes.size())
return Error::from_string_literal("Write of entire buffer ends past the memory area");
TRY(write(bytes));
return {};
}
Bytes FixedMemoryStream::bytes()
{
VERIFY(m_writing_enabled);
return m_bytes;
}
ReadonlyBytes FixedMemoryStream::bytes() const
{
return m_bytes;
}
size_t FixedMemoryStream::offset() const
{
return m_offset;
}
size_t FixedMemoryStream::remaining() const
{
return m_bytes.size() - m_offset;
}
ErrorOr<Bytes> AllocatingMemoryStream::read(Bytes bytes)
{
size_t read_bytes = 0;
while (read_bytes < bytes.size()) {
VERIFY(m_write_offset >= m_read_offset);
auto range = TRY(next_read_range());
if (range.size() == 0)
break;
auto copied_bytes = range.copy_trimmed_to(bytes.slice(read_bytes));
read_bytes += copied_bytes;
m_read_offset += copied_bytes;
}
cleanup_unused_chunks();
return bytes.trim(read_bytes);
}
ErrorOr<size_t> AllocatingMemoryStream::write(ReadonlyBytes bytes)
{
size_t written_bytes = 0;
while (written_bytes < bytes.size()) {
VERIFY(m_write_offset >= m_read_offset);
auto range = TRY(next_write_range());
auto copied_bytes = bytes.slice(written_bytes).copy_trimmed_to(range);
written_bytes += copied_bytes;
m_write_offset += copied_bytes;
}
return written_bytes;
}
ErrorOr<void> AllocatingMemoryStream::discard(size_t count)
{
VERIFY(m_write_offset >= m_read_offset);
if (count > used_buffer_size())
return Error::from_string_literal("Number of discarded bytes is higher than the number of allocated bytes");
m_read_offset += count;
cleanup_unused_chunks();
return {};
}
bool AllocatingMemoryStream::is_eof() const
{
return used_buffer_size() == 0;
}
bool AllocatingMemoryStream::is_open() const
{
return true;
}
void AllocatingMemoryStream::close()
{
}
size_t AllocatingMemoryStream::used_buffer_size() const
{
return m_write_offset - m_read_offset;
}
ErrorOr<Optional<size_t>> AllocatingMemoryStream::offset_of(ReadonlyBytes needle) const
{
VERIFY(m_write_offset >= m_read_offset);
if (m_chunks.size() == 0)
return Optional<size_t> {};
// Ensure that we don't have to trim away more than one block.
VERIFY(m_read_offset < chunk_size);
VERIFY(m_chunks.size() * chunk_size - m_write_offset < chunk_size);
auto chunk_count = m_chunks.size();
auto search_spans = TRY(FixedArray<ReadonlyBytes>::create(chunk_count));
for (size_t i = 0; i < chunk_count; i++) {
search_spans[i] = m_chunks[i].span();
}
// Trimming is done first to ensure that we don't unintentionally shift around if the first and last chunks are the same.
search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(m_write_offset % chunk_size);
search_spans[0] = search_spans[0].slice(m_read_offset);
return AK::memmem(search_spans.begin(), search_spans.end(), needle);
}
ErrorOr<ReadonlyBytes> AllocatingMemoryStream::next_read_range()
{
VERIFY(m_write_offset >= m_read_offset);
size_t const chunk_index = m_read_offset / chunk_size;
size_t const chunk_offset = m_read_offset % chunk_size;
size_t const read_size = min(chunk_size - m_read_offset % chunk_size, m_write_offset - m_read_offset);
if (read_size == 0)
return ReadonlyBytes { static_cast<u8*>(nullptr), 0 };
VERIFY(chunk_index < m_chunks.size());
return ReadonlyBytes { m_chunks[chunk_index].data() + chunk_offset, read_size };
}
ErrorOr<Bytes> AllocatingMemoryStream::next_write_range()
{
VERIFY(m_write_offset >= m_read_offset);
size_t const chunk_index = m_write_offset / chunk_size;
size_t const chunk_offset = m_write_offset % chunk_size;
size_t const write_size = chunk_size - m_write_offset % chunk_size;
if (chunk_index >= m_chunks.size())
TRY(m_chunks.try_append(TRY(Chunk::create_uninitialized(chunk_size))));
VERIFY(chunk_index < m_chunks.size());
return Bytes { m_chunks[chunk_index].data() + chunk_offset, write_size };
}
void AllocatingMemoryStream::cleanup_unused_chunks()
{
// FIXME: Move these all at once.
while (m_read_offset >= chunk_size) {
VERIFY(m_write_offset >= m_read_offset);
auto buffer = m_chunks.take_first();
m_read_offset -= chunk_size;
m_write_offset -= chunk_size;
m_chunks.append(move(buffer));
}
}
}

View file

@ -1,79 +0,0 @@
/*
* 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 {
/// 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<NonnullOwnPtr<FixedMemoryStream>> construct(Bytes bytes);
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(ReadonlyBytes bytes);
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;
virtual ErrorOr<size_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override;
virtual ErrorOr<void> 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 AK::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;
ErrorOr<Optional<size_t>> 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<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;
};
}

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibCore/SOCKSProxyClient.h>
enum class Method : u8 {
@ -122,7 +122,7 @@ ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Stream:
ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::Version version, Core::SOCKSProxyClient::HostOrIPV4 target, int port, Core::SOCKSProxyClient::Command command)
{
Core::Stream::AllocatingMemoryStream stream;
AllocatingMemoryStream stream;
Socks5ConnectRequestHeader header {
.version_identifier = to_underlying(version),
@ -218,7 +218,7 @@ ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::
ErrorOr<u8> send_username_password_authentication_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::UsernamePasswordAuthenticationData const& auth_data)
{
Core::Stream::AllocatingMemoryStream stream;
AllocatingMemoryStream stream;
u8 version = 0x01;
auto size = TRY(stream.write({ &version, sizeof(version) }));

View file

@ -9,8 +9,8 @@
#include "Name.h"
#include "PacketHeader.h"
#include <AK/Debug.h>
#include <AK/MemoryStream.h>
#include <AK/StringBuilder.h>
#include <LibCore/MemoryStream.h>
#include <arpa/inet.h>
namespace DNS {
@ -48,7 +48,7 @@ ErrorOr<ByteBuffer> Packet::to_byte_buffer() const
header.set_question_count(m_questions.size());
header.set_answer_count(m_answers.size());
Core::Stream::AllocatingMemoryStream stream;
AllocatingMemoryStream stream;
TRY(stream.write_value(header));
for (auto& question : m_questions) {

View file

@ -8,7 +8,7 @@
#include "DwarfInfo.h"
#include <AK/LEB128.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
namespace Debug::Dwarf {
@ -21,7 +21,7 @@ AbbreviationsMap::AbbreviationsMap(DwarfInfo const& dwarf_info, u32 offset)
ErrorOr<void> AbbreviationsMap::populate_map()
{
auto abbreviation_stream = TRY(Core::Stream::FixedMemoryStream::construct(m_dwarf_info.abbreviation_data()));
auto abbreviation_stream = TRY(FixedMemoryStream::construct(m_dwarf_info.abbreviation_data()));
TRY(abbreviation_stream->discard(m_offset));
Core::Stream::WrapInAKInputStream wrapped_abbreviation_stream { *abbreviation_stream };

View file

@ -9,7 +9,7 @@
#include "DwarfInfo.h"
#include <AK/ByteBuffer.h>
#include <AK/LEB128.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
namespace Debug::Dwarf {
@ -23,7 +23,7 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
{
m_offset = offset;
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_offset));
Core::Stream::WrapInAKInputStream wrapped_stream { *stream };
@ -52,7 +52,7 @@ ErrorOr<void> DIE::rehydrate_from(u32 offset, Optional<u32> parent_offset)
ErrorOr<Optional<AttributeValue>> DIE::get_attribute(Attribute const& attribute) const
{
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
auto stream = TRY(FixedMemoryStream::construct(m_compilation_unit.dwarf_info().debug_info_data()));
// Note: We can't just slice away from the input data here, since get_attribute_value will try to recover the original offset using seek().
TRY(stream->seek(m_data_offset));

View file

@ -11,7 +11,7 @@
#include <AK/ByteReader.h>
#include <AK/LEB128.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibDebug/DebugInfo.h>
namespace Debug::Dwarf {
@ -47,8 +47,8 @@ ErrorOr<void> DwarfInfo::populate_compilation_units()
if (!m_debug_info_data.data())
return {};
auto debug_info_stream = TRY(Core::Stream::FixedMemoryStream::construct(m_debug_info_data));
auto line_info_stream = TRY(Core::Stream::FixedMemoryStream::construct(m_debug_line_data));
auto debug_info_stream = TRY(FixedMemoryStream::construct(m_debug_info_data));
auto line_info_stream = TRY(FixedMemoryStream::construct(m_debug_line_data));
while (!debug_info_stream->is_eof()) {
auto unit_offset = TRY(debug_info_stream->tell());
@ -321,14 +321,14 @@ ErrorOr<void> DwarfInfo::build_cached_dies() const
Vector<DIERange> entries;
if (die.compilation_unit().dwarf_version() == 5) {
auto range_lists_stream = TRY(Core::Stream::FixedMemoryStream::construct(debug_range_lists_data()));
auto range_lists_stream = TRY(FixedMemoryStream::construct(debug_range_lists_data()));
TRY(range_lists_stream->seek(offset));
AddressRangesV5 address_ranges(move(range_lists_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) {
entries.empend(range.start, range.end);
}));
} else {
auto ranges_stream = TRY(Core::Stream::FixedMemoryStream::construct(debug_ranges_data()));
auto ranges_stream = TRY(FixedMemoryStream::construct(debug_ranges_data()));
TRY(ranges_stream->seek(offset));
AddressRangesV4 address_ranges(move(ranges_stream), die.compilation_unit());
TRY(address_ranges.for_each_range([&entries](auto range) {

View file

@ -7,14 +7,14 @@
#include "Expression.h"
#include <AK/Format.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <sys/arch/regs.h>
namespace Debug::Dwarf::Expression {
ErrorOr<Value> evaluate(ReadonlyBytes bytes, [[maybe_unused]] PtraceRegisters const& regs)
{
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(bytes));
auto stream = TRY(FixedMemoryStream::construct(bytes));
while (!stream->is_eof()) {
auto opcode = TRY(stream->read_value<u8>());

View file

@ -11,9 +11,9 @@
#include <AK/Error.h>
#include <AK/IntegralMath.h>
#include <AK/Memory.h>
#include <AK/MemoryStream.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/Try.h>
#include <LibCore/MemoryStream.h>
#include <LibGfx/GIFLoader.h>
#include <string.h>
@ -381,7 +381,7 @@ static ErrorOr<void> load_gif_frame_descriptors(GIFLoadingContext& context)
if (context.data_size < 32)
return Error::from_string_literal("Size too short for GIF frame descriptors");
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { context.data, context.data_size }));
auto stream = TRY(FixedMemoryStream::construct(ReadonlyBytes { context.data, context.data_size }));
TRY(decode_gif_header(*stream));
@ -565,7 +565,7 @@ bool GIFImageDecoderPlugin::set_nonvolatile(bool& was_purged)
bool GIFImageDecoderPlugin::initialize()
{
auto stream_or_error = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size });
auto stream_or_error = FixedMemoryStream::construct(ReadonlyBytes { m_context->data, m_context->data_size });
if (stream_or_error.is_error())
return false;
return !decode_gif_header(*stream_or_error.value()).is_error();
@ -573,7 +573,7 @@ bool GIFImageDecoderPlugin::initialize()
ErrorOr<bool> GIFImageDecoderPlugin::sniff(ReadonlyBytes data)
{
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(data));
auto stream = TRY(FixedMemoryStream::construct(data));
return !decode_gif_header(*stream).is_error();
}

View file

@ -5,14 +5,15 @@
*/
#include <AK/Debug.h>
#include <AK/Endian.h>
#include <AK/Error.h>
#include <AK/FixedArray.h>
#include <AK/HashMap.h>
#include <AK/Math.h>
#include <AK/MemoryStream.h>
#include <AK/String.h>
#include <AK/Try.h>
#include <AK/Vector.h>
#include <LibCore/MemoryStream.h>
#include <LibGfx/JPGLoader.h>
#define JPG_INVALID 0X0000
@ -198,7 +199,7 @@ struct JPGLoadingContext {
HuffmanStreamState huffman_stream;
i32 previous_dc_values[3] = { 0 };
MacroblockMeta mblock_meta;
OwnPtr<Core::Stream::FixedMemoryStream> stream;
OwnPtr<FixedMemoryStream> stream;
Optional<ICCMultiChunkState> icc_multi_chunk_state;
Optional<ByteBuffer> icc_data;
@ -1201,7 +1202,7 @@ static ErrorOr<void> scan_huffman_stream(AK::SeekableStream& stream, JPGLoadingC
static ErrorOr<void> decode_header(JPGLoadingContext& context)
{
if (context.state < JPGLoadingContext::State::HeaderDecoded) {
context.stream = TRY(Core::Stream::FixedMemoryStream::construct({ context.data, context.data_size }));
context.stream = TRY(FixedMemoryStream::construct({ context.data, context.data_size }));
if (auto result = parse_header(*context.stream, context); result.is_error()) {
context.state = JPGLoadingContext::State::Error;

View file

@ -5,7 +5,7 @@
*/
#include <AK/Endian.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/QOILoader.h>
@ -202,13 +202,13 @@ bool QOIImageDecoderPlugin::initialize()
ErrorOr<bool> QOIImageDecoderPlugin::sniff(ReadonlyBytes data)
{
auto stream = TRY(Core::Stream::FixedMemoryStream::construct({ data.data(), data.size() }));
auto stream = TRY(FixedMemoryStream::construct({ data.data(), data.size() }));
return !decode_qoi_header(*stream).is_error();
}
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> QOIImageDecoderPlugin::create(ReadonlyBytes data)
{
auto stream = TRY(Core::Stream::FixedMemoryStream::construct(data));
auto stream = TRY(FixedMemoryStream::construct(data));
return adopt_nonnull_own_or_enomem(new (nothrow) QOIImageDecoderPlugin(move(stream)));
}

View file

@ -8,12 +8,12 @@
#include <AK/CharacterTypes.h>
#include <AK/Debug.h>
#include <AK/JsonObject.h>
#include <AK/MemoryStream.h>
#include <AK/Try.h>
#include <LibCompress/Brotli.h>
#include <LibCompress/Gzip.h>
#include <LibCompress/Zlib.h>
#include <LibCore/Event.h>
#include <LibCore/MemoryStream.h>
#include <LibHTTP/HttpResponse.h>
#include <LibHTTP/Job.h>
#include <stdio.h>
@ -70,7 +70,7 @@ static ErrorOr<ByteBuffer> handle_content_encoding(ByteBuffer const& buf, Deprec
} else if (content_encoding == "br") {
dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is brotli compressed!");
auto bufstream = TRY(Core::Stream::FixedMemoryStream::construct({ buf.data(), buf.size() }));
auto bufstream = TRY(FixedMemoryStream::construct({ buf.data(), buf.size() }));
auto brotli_stream = Compress::BrotliDecompressionStream { *bufstream };
auto uncompressed = TRY(brotli_stream.read_until_eof());

View file

@ -10,6 +10,7 @@
#include <AK/Debug.h>
#include <AK/GenericLexer.h>
#include <AK/JsonObject.h>
#include <AK/MemoryStream.h>
#include <AK/RedBlackTree.h>
#include <AK/ScopeGuard.h>
#include <AK/ScopedValueRollback.h>
@ -20,7 +21,6 @@
#include <LibCore/Event.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/MemoryStream.h>
#include <LibCore/Notifier.h>
#include <errno.h>
#include <fcntl.h>
@ -1329,7 +1329,7 @@ ErrorOr<void> Editor::cleanup()
ErrorOr<void> Editor::refresh_display()
{
Core::Stream::AllocatingMemoryStream output_stream;
AllocatingMemoryStream output_stream;
ScopeGuard flush_stream {
[&] {
m_shown_lines = current_prompt_metrics().lines_with_addition(m_cached_buffer_metrics, m_num_columns);

View file

@ -6,8 +6,9 @@
*/
#include <AK/BitStream.h>
#include <AK/Endian.h>
#include <AK/MemoryStream.h>
#include <AK/Tuple.h>
#include <LibCore/MemoryStream.h>
#include <LibPDF/CommonNames.h>
#include <LibPDF/Document.h>
#include <LibPDF/DocumentParser.h>
@ -595,7 +596,7 @@ PDFErrorOr<DocumentParser::PageOffsetHintTable> DocumentParser::parse_page_offse
PDFErrorOr<Vector<DocumentParser::PageOffsetHintTableEntry>> DocumentParser::parse_all_page_offset_hint_table_entries(PageOffsetHintTable const& hint_table, ReadonlyBytes hint_stream_bytes)
{
auto input_stream = TRY(Core::Stream::FixedMemoryStream::construct(hint_stream_bytes));
auto input_stream = TRY(FixedMemoryStream::construct(hint_stream_bytes));
TRY(input_stream->seek(sizeof(PageOffsetHintTable)));
auto bit_stream = TRY(LittleEndianInputBitStream::construct(move(input_stream)));

View file

@ -10,9 +10,9 @@
#include <AK/ByteBuffer.h>
#include <AK/DeprecatedString.h>
#include <AK/Function.h>
#include <AK/MemoryStream.h>
#include <AK/RefCounted.h>
#include <AK/WeakPtr.h>
#include <LibCore/MemoryStream.h>
#include <LibCore/Notifier.h>
#include <LibCore/Stream.h>
#include <LibIPC/Forward.h>
@ -68,7 +68,7 @@ private:
bool m_should_buffer_all_input { false };
struct InternalBufferedData {
Core::Stream::AllocatingMemoryStream payload_stream;
AllocatingMemoryStream payload_stream;
HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> response_headers;
Optional<u32> response_code;
};

View file

@ -6,7 +6,7 @@
*/
#include <AK/Debug.h>
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibWasm/AbstractMachine/AbstractMachine.h>
#include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
#include <LibWasm/AbstractMachine/Configuration.h>
@ -203,7 +203,7 @@ struct ConvertToRaw<float> {
{
LittleEndian<u32> res;
ReadonlyBytes bytes { &value, sizeof(float) };
auto stream = Core::Stream::FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors();
stream->read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors();
return static_cast<u32>(res);
}
@ -215,7 +215,7 @@ struct ConvertToRaw<double> {
{
LittleEndian<u64> res;
ReadonlyBytes bytes { &value, sizeof(double) };
auto stream = Core::Stream::FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(bytes).release_value_but_fixme_should_propagate_errors();
stream->read_entire_buffer(res.bytes()).release_value_but_fixme_should_propagate_errors();
return static_cast<u64>(res);
}
@ -253,7 +253,7 @@ template<typename T>
T BytecodeInterpreter::read_value(ReadonlyBytes data)
{
LittleEndian<T> value;
auto stream = Core::Stream::FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto maybe_error = stream->read_entire_buffer(value.bytes());
if (maybe_error.is_error()) {
dbgln("Read from {} failed", data.data());
@ -266,7 +266,7 @@ template<>
float BytecodeInterpreter::read_value<float>(ReadonlyBytes data)
{
LittleEndian<u32> raw_value;
auto stream = Core::Stream::FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto maybe_error = stream->read_entire_buffer(raw_value.bytes());
if (maybe_error.is_error())
m_trap = Trap { "Read from memory failed" };
@ -277,7 +277,7 @@ template<>
double BytecodeInterpreter::read_value<double>(ReadonlyBytes data)
{
LittleEndian<u64> raw_value;
auto stream = Core::Stream::FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto maybe_error = stream->read_entire_buffer(raw_value.bytes());
if (maybe_error.is_error())
m_trap = Trap { "Read from memory failed" };

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibWasm/AbstractMachine/Configuration.h>
#include <LibWasm/AbstractMachine/Interpreter.h>
#include <LibWasm/Printer/Printer.h>
@ -86,7 +86,7 @@ Result Configuration::execute(Interpreter& interpreter)
void Configuration::dump_stack()
{
auto print_value = []<typename... Ts>(CheckedFormatString<Ts...> format, Ts... vs) {
Core::Stream::AllocatingMemoryStream memory_stream;
AllocatingMemoryStream memory_stream;
Printer { memory_stream }.print(vs...);
auto buffer = ByteBuffer::create_uninitialized(memory_stream.used_buffer_size()).release_value_but_fixme_should_propagate_errors();
memory_stream.read_entire_buffer(buffer).release_value_but_fixme_should_propagate_errors();

View file

@ -6,9 +6,9 @@
#include <AK/Debug.h>
#include <AK/LEB128.h>
#include <AK/MemoryStream.h>
#include <AK/ScopeGuard.h>
#include <AK/ScopeLogger.h>
#include <LibCore/MemoryStream.h>
#include <LibWasm/Types.h>
namespace Wasm {
@ -261,7 +261,7 @@ ParseResult<BlockType> BlockType::parse(AK::Stream& stream)
return BlockType {};
{
auto value_stream = Core::Stream::FixedMemoryStream::construct(ReadonlyBytes { &kind, 1 }).release_value_but_fixme_should_propagate_errors();
auto value_stream = FixedMemoryStream::construct(ReadonlyBytes { &kind, 1 }).release_value_but_fixme_should_propagate_errors();
if (auto value_type = ValueType::parse(*value_stream); !value_type.is_error())
return BlockType { value_type.release_value() };
}

View file

@ -11,8 +11,8 @@
#include "WebAssemblyModulePrototype.h"
#include "WebAssemblyTableObject.h"
#include "WebAssemblyTablePrototype.h"
#include <AK/MemoryStream.h>
#include <AK/ScopeGuard.h>
#include <LibCore/MemoryStream.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/BigInt.h>
@ -121,7 +121,7 @@ JS::ThrowCompletionOr<size_t> parse_module(JS::VM& vm, JS::Object* buffer_object
} else {
return vm.throw_completion<JS::TypeError>("Not a BufferSource");
}
auto stream = Core::Stream::FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto stream = FixedMemoryStream::construct(data).release_value_but_fixme_should_propagate_errors();
auto module_result = Wasm::Module::parse(*stream);
if (module_result.is_error()) {
// FIXME: Throw CompileError instead.