1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:28:11 +00:00

AK: Lower the requirements for InputStream::eof and rename it.

Consider the following snippet:

    void foo(InputStream& stream) {
        if(!stream.eof()) {
            u8 byte;
            stream >> byte;
        }
    }

There is a very subtle bug in this snippet, for some input streams eof()
might return false even if no more data can be read. In this case an
error flag would be set on the stream.

Until now I've always ensured that this is not the case, but this made
the implementation of eof() unnecessarily complicated.
InputFileStream::eof had to keep a ByteBuffer around just to make this
possible. That meant a ton of unnecessary copies just to get a reliable
eof().

In most cases it isn't actually necessary to have a reliable eof()
implementation.

In most other cases a reliable eof() is avaliable anyways because in
some cases like InputMemoryStream it is very easy to implement.
This commit is contained in:
asynts 2020-09-13 12:24:17 +02:00 committed by Andreas Kling
parent 8a21c528ad
commit 96edcbc27c
12 changed files with 61 additions and 89 deletions

View file

@ -290,7 +290,7 @@ bool DeflateDecompressor::discard_or_error(size_t count)
size_t ndiscarded = 0;
while (ndiscarded < count) {
if (eof()) {
if (unreliable_eof()) {
set_fatal_error();
return false;
}
@ -301,7 +301,7 @@ bool DeflateDecompressor::discard_or_error(size_t count)
return true;
}
bool DeflateDecompressor::eof() const { return m_state == State::Idle && m_read_final_bock; }
bool DeflateDecompressor::unreliable_eof() const { return m_state == State::Idle && m_read_final_bock; }
Optional<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
{
@ -310,7 +310,7 @@ Optional<ByteBuffer> DeflateDecompressor::decompress_all(ReadonlyBytes bytes)
OutputMemoryStream output_stream;
u8 buffer[4096];
while (!deflate_stream.has_any_error() && !deflate_stream.eof()) {
while (!deflate_stream.has_any_error() && !deflate_stream.unreliable_eof()) {
const auto nread = deflate_stream.read({ buffer, sizeof(buffer) });
output_stream.write_or_error({ buffer, nread });
}