summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--AK/CMakeLists.txt1
-rw-r--r--AK/CountingStream.cpp58
-rw-r--r--AK/CountingStream.h32
-rw-r--r--AK/Forward.h2
4 files changed, 93 insertions, 0 deletions
diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt
index 964f672a8f..b9eb1cb487 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 0000000000..c6074b25aa
--- /dev/null
+++ b/AK/CountingStream.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/CountingStream.h>
+
+namespace AK {
+
+CountingStream::CountingStream(MaybeOwned<Stream> stream)
+ : m_stream(move(stream))
+{
+}
+
+u64 CountingStream::read_bytes() const
+{
+ return m_read_bytes;
+}
+
+ErrorOr<Bytes> CountingStream::read_some(Bytes bytes)
+{
+ auto result = TRY(m_stream->read_some(bytes));
+
+ m_read_bytes += result.size();
+
+ return result;
+}
+
+ErrorOr<void> CountingStream::discard(size_t discarded_bytes)
+{
+ TRY(m_stream->discard(discarded_bytes));
+
+ m_read_bytes += discarded_bytes;
+
+ return {};
+}
+
+ErrorOr<size_t> 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 0000000000..3afd1f4b48
--- /dev/null
+++ b/AK/CountingStream.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/MaybeOwned.h>
+#include <AK/Stream.h>
+
+namespace AK {
+
+class CountingStream : public Stream {
+public:
+ CountingStream(MaybeOwned<Stream>);
+
+ u64 read_bytes() const;
+
+ virtual ErrorOr<Bytes> read_some(Bytes) override;
+ virtual ErrorOr<void> discard(size_t discarded_bytes) override;
+ virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
+ virtual bool is_eof() const override;
+ virtual bool is_open() const override;
+ virtual void close() override;
+
+private:
+ MaybeOwned<Stream> m_stream;
+ u64 m_read_bytes { 0 };
+};
+
+}
diff --git a/AK/Forward.h b/AK/Forward.h
index e9b7dde845..42f9d3a8c0 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;