1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +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

@ -40,7 +40,8 @@ public:
{
}
bool eof() const override { return m_offset >= m_bytes.size(); }
bool unreliable_eof() const override { return eof(); }
bool eof() const { return m_offset >= m_bytes.size(); }
size_t read(Bytes bytes) override
{
@ -167,7 +168,8 @@ class DuplexMemoryStream final : public DuplexStream {
public:
static constexpr size_t chunk_size = 4 * 1024;
bool eof() const override { return m_write_offset == m_read_offset; }
bool unreliable_eof() const override { return eof(); }
bool eof() const { return m_write_offset == m_read_offset; }
bool discard_or_error(size_t count) override
{