mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:27:35 +00:00
LibCore: Add Stream::discard()
This commit is contained in:
parent
7a065513cd
commit
6e29619dcb
2 changed files with 38 additions and 0 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue