mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 16:38:10 +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:
parent
8a21c528ad
commit
96edcbc27c
12 changed files with 61 additions and 89 deletions
|
@ -68,25 +68,8 @@ public:
|
|||
if (has_any_error())
|
||||
return 0;
|
||||
|
||||
auto nread = m_buffered.bytes().copy_trimmed_to(bytes);
|
||||
|
||||
m_buffered.bytes().slice(nread, m_buffered.size() - nread).copy_to(m_buffered);
|
||||
m_buffered.trim(m_buffered.size() - nread);
|
||||
|
||||
while (nread < bytes.size()) {
|
||||
if (m_file->eof())
|
||||
return nread;
|
||||
|
||||
if (m_file->has_error()) {
|
||||
set_fatal_error();
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto buffer = m_file->read(bytes.size() - nread);
|
||||
nread += buffer.bytes().copy_to(bytes.slice(nread));
|
||||
}
|
||||
|
||||
return nread;
|
||||
const auto buffer = m_file->read(bytes.size());
|
||||
return buffer.bytes().copy_to(bytes);
|
||||
}
|
||||
|
||||
bool read_or_error(Bytes bytes) override
|
||||
|
@ -99,34 +82,9 @@ public:
|
|||
return true;
|
||||
}
|
||||
|
||||
bool discard_or_error(size_t count) override
|
||||
{
|
||||
u8 buffer[4096];
|
||||
bool discard_or_error(size_t count) override { return m_file->seek(count, IODevice::SeekMode::FromCurrentPosition); }
|
||||
|
||||
size_t ndiscarded = 0;
|
||||
while (ndiscarded < count && !eof())
|
||||
ndiscarded += read({ buffer, min<size_t>(count - ndiscarded, sizeof(buffer)) });
|
||||
|
||||
if (eof()) {
|
||||
set_fatal_error();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool eof() const override
|
||||
{
|
||||
if (m_buffered.size() > 0)
|
||||
return false;
|
||||
|
||||
if (m_file->eof())
|
||||
return true;
|
||||
|
||||
m_buffered = m_file->read(4096);
|
||||
|
||||
return m_buffered.size() == 0;
|
||||
}
|
||||
bool unreliable_eof() const override { return m_file->eof(); }
|
||||
|
||||
void close()
|
||||
{
|
||||
|
@ -137,8 +95,7 @@ public:
|
|||
private:
|
||||
InputFileStream() = default;
|
||||
|
||||
mutable NonnullRefPtr<File> m_file;
|
||||
mutable ByteBuffer m_buffered;
|
||||
NonnullRefPtr<File> m_file;
|
||||
};
|
||||
|
||||
class OutputFileStream : public OutputStream {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue