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

LibCore+Everywhere: Make Core::Stream::read() return Bytes

A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
This commit is contained in:
Sam Atkins 2022-04-15 13:33:02 +01:00 committed by Tim Flynn
parent 6654efcd82
commit 3b1e063d30
22 changed files with 103 additions and 103 deletions

View file

@ -18,18 +18,18 @@ constexpr static size_t MaximumApplicationDataChunkSize = 16 * KiB;
namespace TLS {
ErrorOr<size_t> TLSv12::read(Bytes bytes)
ErrorOr<Bytes> TLSv12::read(Bytes bytes)
{
m_eof = false;
auto size_to_read = min(bytes.size(), m_context.application_buffer.size());
if (size_to_read == 0) {
m_eof = true;
return 0;
return Bytes {};
}
m_context.application_buffer.span().slice(0, size_to_read).copy_to(bytes);
m_context.application_buffer = m_context.application_buffer.slice(size_to_read, m_context.application_buffer.size() - size_to_read);
return size_to_read;
return Bytes { bytes.data(), size_to_read };
}
String TLSv12::read_line(size_t max_size)
@ -186,7 +186,7 @@ ErrorOr<void> TLSv12::read_from_socket()
u8 buffer[16 * KiB];
Bytes bytes { buffer, array_size(buffer) };
size_t nread = 0;
Bytes read_bytes {};
auto& stream = underlying_stream();
do {
auto result = stream.read(bytes);
@ -198,9 +198,9 @@ ErrorOr<void> TLSv12::read_from_socket()
}
continue;
}
nread = result.release_value();
consume(bytes.slice(0, nread));
} while (nread > 0 && !m_context.critical_error);
read_bytes = result.release_value();
consume(read_bytes);
} while (!read_bytes.is_empty() && !m_context.critical_error);
return {};
}