AK: Add a Stream wrapper that counts read bytes

This commit is contained in:
Tim Schumacher 2023-03-16 10:23:24 +01:00 committed by Andreas Kling
parent d1f6a28ffd
commit e62183f0ba
Notes: sideshowbarker 2024-07-17 06:33:00 +09:00
4 changed files with 93 additions and 0 deletions

View file

@ -3,6 +3,7 @@ set(AK_SOURCES
Base64.cpp Base64.cpp
CircularBuffer.cpp CircularBuffer.cpp
ConstrainedStream.cpp ConstrainedStream.cpp
CountingStream.cpp
DOSPackedTime.cpp DOSPackedTime.cpp
DeprecatedFlyString.cpp DeprecatedFlyString.cpp
DeprecatedString.cpp DeprecatedString.cpp

58
AK/CountingStream.cpp Normal file
View file

@ -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()
{
}
}

32
AK/CountingStream.h Normal file
View file

@ -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 };
};
}

View file

@ -23,6 +23,7 @@ class Bitmap;
using ByteBuffer = Detail::ByteBuffer<32>; using ByteBuffer = Detail::ByteBuffer<32>;
class CircularBuffer; class CircularBuffer;
class ConstrainedStream; class ConstrainedStream;
class CountingStream;
class DeprecatedFlyString; class DeprecatedFlyString;
class DeprecatedString; class DeprecatedString;
class DeprecatedStringCodePointIterator; class DeprecatedStringCodePointIterator;
@ -157,6 +158,7 @@ using AK::Bytes;
using AK::CircularBuffer; using AK::CircularBuffer;
using AK::CircularQueue; using AK::CircularQueue;
using AK::ConstrainedStream; using AK::ConstrainedStream;
using AK::CountingStream;
using AK::DeprecatedFlyString; using AK::DeprecatedFlyString;
using AK::DeprecatedString; using AK::DeprecatedString;
using AK::DeprecatedStringCodePointIterator; using AK::DeprecatedStringCodePointIterator;