diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-01-20 14:07:24 +0100 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-01-29 19:16:44 -0700 |
commit | ae64b68717c02b902721e861dbf51c5d32950193 (patch) | |
tree | 74e7205b629f9a51d0af3103ce9fabca1aeefe23 /AK/DeprecatedMemoryStream.h | |
parent | 230cb3b0cb9664839c56f80d6dff3706fffc2243 (diff) | |
download | serenity-ae64b68717c02b902721e861dbf51c5d32950193.zip |
AK: Deprecate the old `AK::Stream`
This also removes a few cases where the respective header wasn't
actually required to be included.
Diffstat (limited to 'AK/DeprecatedMemoryStream.h')
-rw-r--r-- | AK/DeprecatedMemoryStream.h | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/AK/DeprecatedMemoryStream.h b/AK/DeprecatedMemoryStream.h new file mode 100644 index 0000000000..055c0be8ce --- /dev/null +++ b/AK/DeprecatedMemoryStream.h @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2020, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/ByteBuffer.h> +#include <AK/DeprecatedStream.h> +#include <AK/LEB128.h> +#include <AK/MemMem.h> +#include <AK/Vector.h> + +namespace AK { + +class DeprecatedInputMemoryStream final : public DeprecatedInputStream { +public: + explicit DeprecatedInputMemoryStream(ReadonlyBytes bytes) + : m_bytes(bytes) + { + } + + bool unreliable_eof() const override { return eof(); } + bool eof() const { return m_offset >= m_bytes.size(); } + + size_t read(Bytes bytes) override + { + if (has_any_error()) + return 0; + + auto const count = min(bytes.size(), remaining()); + __builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, count); + m_offset += count; + return count; + } + + bool read_or_error(Bytes bytes) override + { + if (remaining() < bytes.size()) { + set_recoverable_error(); + return false; + } + + __builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, bytes.size()); + m_offset += bytes.size(); + return true; + } + + bool discard_or_error(size_t count) override + { + if (remaining() < count) { + set_recoverable_error(); + return false; + } + + m_offset += count; + return true; + } + + void seek(size_t offset) + { + VERIFY(offset < m_bytes.size()); + m_offset = offset; + } + + u8 peek_or_error() const + { + if (remaining() == 0) { + set_recoverable_error(); + return 0; + } + + return m_bytes[m_offset]; + } + + template<typename ValueType> + bool read_LEB128_unsigned(ValueType& result) { return LEB128::read_unsigned(*this, result); } + + template<typename ValueType> + bool read_LEB128_signed(ValueType& result) { return LEB128::read_signed(*this, result); } + + ReadonlyBytes bytes() const { return m_bytes; } + size_t offset() const { return m_offset; } + size_t remaining() const { return m_bytes.size() - m_offset; } + +private: + ReadonlyBytes m_bytes; + size_t m_offset { 0 }; +}; + +class DeprecatedOutputMemoryStream final : public DeprecatedOutputStream { +public: + explicit DeprecatedOutputMemoryStream(Bytes bytes) + : m_bytes(bytes) + { + } + + size_t write(ReadonlyBytes bytes) override + { + auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset)); + m_offset += nwritten; + return nwritten; + } + + bool write_or_error(ReadonlyBytes bytes) override + { + if (remaining() < bytes.size()) { + set_recoverable_error(); + return false; + } + + write(bytes); + return true; + } + + size_t fill_to_end(u8 value) + { + auto const nwritten = m_bytes.slice(m_offset).fill(value); + m_offset += nwritten; + return nwritten; + } + + bool is_end() const { return remaining() == 0; } + + ReadonlyBytes bytes() const { return { data(), size() }; } + Bytes bytes() { return { data(), size() }; } + + u8 const* data() const { return m_bytes.data(); } + u8* data() { return m_bytes.data(); } + + size_t size() const { return m_offset; } + size_t remaining() const { return m_bytes.size() - m_offset; } + void reset() { m_offset = 0; } + +private: + size_t m_offset { 0 }; + Bytes m_bytes; +}; + +} + +#if USING_AK_GLOBALLY +using AK::DeprecatedInputMemoryStream; +using AK::DeprecatedOutputMemoryStream; +#endif |