From e62183f0ba3a2aad9eb9dcdd879f5f7f03c76043 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Thu, 16 Mar 2023 10:23:24 +0100 Subject: [PATCH] AK: Add a Stream wrapper that counts read bytes --- AK/CMakeLists.txt | 1 + AK/CountingStream.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++ AK/CountingStream.h | 32 ++++++++++++++++++++++++ AK/Forward.h | 2 ++ 4 files changed, 93 insertions(+) create mode 100644 AK/CountingStream.cpp create mode 100644 AK/CountingStream.h diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 964f672a8f4..b9eb1cb4874 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -3,6 +3,7 @@ set(AK_SOURCES Base64.cpp CircularBuffer.cpp ConstrainedStream.cpp + CountingStream.cpp DOSPackedTime.cpp DeprecatedFlyString.cpp DeprecatedString.cpp diff --git a/AK/CountingStream.cpp b/AK/CountingStream.cpp new file mode 100644 index 00000000000..c6074b25aa3 --- /dev/null +++ b/AK/CountingStream.cpp @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023, Tim Schumacher + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace AK { + +CountingStream::CountingStream(MaybeOwned stream) + : m_stream(move(stream)) +{ +} + +u64 CountingStream::read_bytes() const +{ + return m_read_bytes; +} + +ErrorOr CountingStream::read_some(Bytes bytes) +{ + auto result = TRY(m_stream->read_some(bytes)); + + m_read_bytes += result.size(); + + return result; +} + +ErrorOr CountingStream::discard(size_t discarded_bytes) +{ + TRY(m_stream->discard(discarded_bytes)); + + m_read_bytes += discarded_bytes; + + return {}; +} + +ErrorOr CountingStream::write_some(ReadonlyBytes bytes) +{ + return m_stream->write_some(bytes); +} + +bool CountingStream::is_eof() const +{ + return m_stream->is_eof(); +} + +bool CountingStream::is_open() const +{ + return m_stream->is_open(); +} + +void CountingStream::close() +{ +} + +} diff --git a/AK/CountingStream.h b/AK/CountingStream.h new file mode 100644 index 00000000000..3afd1f4b48e --- /dev/null +++ b/AK/CountingStream.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023, Tim Schumacher + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace AK { + +class CountingStream : public Stream { +public: + CountingStream(MaybeOwned); + + u64 read_bytes() const; + + virtual ErrorOr read_some(Bytes) override; + virtual ErrorOr discard(size_t discarded_bytes) override; + virtual ErrorOr write_some(ReadonlyBytes) override; + virtual bool is_eof() const override; + virtual bool is_open() const override; + virtual void close() override; + +private: + MaybeOwned m_stream; + u64 m_read_bytes { 0 }; +}; + +} diff --git a/AK/Forward.h b/AK/Forward.h index e9b7dde8457..42f9d3a8c0f 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -23,6 +23,7 @@ class Bitmap; using ByteBuffer = Detail::ByteBuffer<32>; class CircularBuffer; class ConstrainedStream; +class CountingStream; class DeprecatedFlyString; class DeprecatedString; class DeprecatedStringCodePointIterator; @@ -157,6 +158,7 @@ using AK::Bytes; using AK::CircularBuffer; using AK::CircularQueue; using AK::ConstrainedStream; +using AK::CountingStream; using AK::DeprecatedFlyString; using AK::DeprecatedString; using AK::DeprecatedStringCodePointIterator;