1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibCore: Add Stream::discard()

This commit is contained in:
Tim Schumacher 2022-11-28 18:54:14 +01:00 committed by Andreas Kling
parent 7a065513cd
commit 6e29619dcb
2 changed files with 38 additions and 0 deletions

View file

@ -73,6 +73,22 @@ ErrorOr<ByteBuffer> Stream::read_all_impl(size_t block_size, size_t expected_fil
return data;
}
ErrorOr<void> Stream::discard(size_t discarded_bytes)
{
// Note: This was chosen arbitrarily.
// Note: This can't be PAGE_SIZE because it is defined to sysconf() on Lagom.
constexpr size_t continuous_read_size = 4096;
Array<u8, continuous_read_size> buffer;
while (discarded_bytes > 0) {
auto slice = TRY(read(buffer.span().slice(0, min(discarded_bytes, continuous_read_size))));
discarded_bytes -= slice.size();
}
return {};
}
bool Stream::write_or_error(ReadonlyBytes buffer)
{
VERIFY(buffer.size());
@ -120,6 +136,12 @@ ErrorOr<off_t> SeekableStream::size()
return seek_result.value();
}
ErrorOr<void> SeekableStream::discard(size_t discarded_bytes)
{
TRY(seek(discarded_bytes, SeekMode::FromCurrentPosition));
return {};
}
ErrorOr<NonnullOwnPtr<File>> File::open(StringView filename, OpenMode mode, mode_t permissions)
{
auto file = TRY(adopt_nonnull_own_or_enomem(new (nothrow) File(mode)));
@ -729,6 +751,14 @@ ErrorOr<Bytes> WrappedAKInputStream::read(Bytes bytes)
return bytes.slice(0, bytes_read);
}
ErrorOr<void> WrappedAKInputStream::discard(size_t discarded_bytes)
{
if (!m_stream->discard_or_error(discarded_bytes))
return Error::from_string_literal("Underlying InputStream indicated an error");
return {};
}
ErrorOr<size_t> WrappedAKInputStream::write(ReadonlyBytes)
{
VERIFY_NOT_REACHED();

View file

@ -41,6 +41,10 @@ public:
/// is returned once EOF is encountered. The block size determines the size
/// of newly allocated chunks while reading.
virtual ErrorOr<ByteBuffer> read_all(size_t block_size = 4096);
/// Discards the given number of bytes from the stream.
/// Unless specifically overwritten, this just uses read() to read into an
/// internal stack-based buffer.
virtual ErrorOr<void> discard(size_t discarded_bytes);
virtual bool is_writable() const { return false; }
/// Tries to write the entire contents of the buffer. It is possible for
@ -97,6 +101,9 @@ public:
/// Shrinks or extends the stream to the given size. Returns an errno in
/// the case of an error.
virtual ErrorOr<void> truncate(off_t length) = 0;
/// Seeks until after the given amount of bytes to be discarded instead of
/// reading and discarding everything manually;
virtual ErrorOr<void> discard(size_t discarded_bytes) override;
};
/// The Socket class is the base class for all concrete BSD-style socket
@ -973,6 +980,7 @@ class WrappedAKInputStream final : public Stream {
public:
WrappedAKInputStream(NonnullOwnPtr<InputStream> stream);
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<void> discard(size_t discarded_bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;