From a19209559417a68d5eba330a54b3c82452251620 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Wed, 14 Sep 2022 19:45:36 +0000 Subject: LibCore: Rewrite Core::Stream::read_all_impl The previous version relied on manually setting the amount of data to read for the next chunk and was overall unclear. The new version uses the Bytes API to vastly improve readability, and fixes a bug where reading from files where a single read that wasn't of equal size to the block size would cause the byte buffer to be incorrectly resized causing corrupted output. --- Userland/Libraries/LibCore/Stream.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index cfa6da77b4..22affc4bfa 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -55,26 +55,21 @@ ErrorOr Stream::read_all(size_t block_size) ErrorOr Stream::read_all_impl(size_t block_size, size_t expected_file_size) { ByteBuffer data; - data.ensure_capacity(file_size); + data.ensure_capacity(expected_file_size); - size_t total_read {}; - size_t next_reading_size { block_size }; - for (Span chunk; !is_eof();) { - if (next_reading_size == block_size) - chunk = TRY(data.get_bytes_for_writing(next_reading_size)); - auto const nread = TRY(read(chunk)).size(); - - next_reading_size -= nread; - - if (next_reading_size == 0) - next_reading_size = block_size; + size_t total_read = 0; + Bytes buffer; + while (!is_eof()) { + if (buffer.is_empty()) { + buffer = TRY(data.get_bytes_for_writing(block_size)); + } + auto nread = TRY(read(buffer)).size(); total_read += nread; - - if (nread < block_size) - data.resize(total_read); + buffer = buffer.slice(nread); } + data.resize(total_read); return data; } -- cgit v1.2.3