1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:47:35 +00:00

LibCore: Add optional custom read size argument in populate_read_buffer

This is a small preparation so IODevice::read() can use it in the next
commit.
This commit is contained in:
Karol Kosek 2021-09-25 12:58:23 +02:00 committed by Andreas Kling
parent 2f01d34b99
commit 48a925b1d2
2 changed files with 15 additions and 5 deletions

View file

@ -218,12 +218,22 @@ String IODevice::read_line(size_t max_size)
return {};
}
bool IODevice::populate_read_buffer() const
bool IODevice::populate_read_buffer(size_t size) const
{
if (m_fd < 0)
return false;
u8 buffer[1024];
int nread = ::read(m_fd, buffer, sizeof(buffer));
if (!size)
return false;
auto buffer_result = ByteBuffer::create_uninitialized(size);
if (!buffer_result.has_value()) {
dbgln("IODevice::populate_read_buffer: Not enough memory to allocate a buffer of {} bytes", size);
return {};
}
auto buffer = buffer_result.release_value();
auto* buffer_ptr = (char*)buffer.data();
int nread = ::read(m_fd, buffer_ptr, size);
if (nread < 0) {
set_error(errno);
return false;
@ -232,7 +242,7 @@ bool IODevice::populate_read_buffer() const
set_eof(true);
return false;
}
m_buffered_data.append(buffer, nread);
m_buffered_data.append(buffer.data(), nread);
return true;
}

View file

@ -103,7 +103,7 @@ protected:
virtual void did_update_fd(int) { }
private:
bool populate_read_buffer() const;
bool populate_read_buffer(size_t size = 1024) const;
bool can_read_from_fd() const;
int m_fd { -1 };