ladybird/AK/ConstrainedStream.h
Ali Mohammad Pur 834fb0be36 AK: Make some Stream::read* functions available inline
These are quite bottlenecky in wasm, the next commit will try to make
use of this by calling them directly instead of doing a vcall, and
having them inlineable helps the compiler a bit.
2025-08-08 12:54:06 +02:00

37 lines
920 B
C++

/*
* Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
* 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 ConstrainedStream : public Stream {
public:
ConstrainedStream(MaybeOwned<Stream>, u64 limit);
virtual ErrorOr<Bytes> read_some(Bytes bytes) override
{
auto result = TRY(m_stream->read_some(bytes.trim(m_limit)));
m_limit -= result.size();
return result;
}
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;
u64 remaining() const { return m_limit; }
private:
MaybeOwned<Stream> m_stream;
u64 m_limit;
};
}