diff options
author | Michiel Visser <opensource@webmichiel.nl> | 2022-04-05 21:17:38 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-05-21 22:41:40 +0200 |
commit | 68772463cb662975cd4dfdc232fb1be38487ad3c (patch) | |
tree | 9c073b32fee3a4fb6245e9543c464589f62bcbce | |
parent | af50b0363b21d3063a397a359e9bedfaf86d8e4e (diff) | |
download | serenity-68772463cb662975cd4dfdc232fb1be38487ad3c.zip |
LibCore: Add Stream::read_all()
-rw-r--r-- | Userland/Libraries/LibCore/Stream.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibCore/Stream.h | 1 |
2 files changed, 17 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index c142bfb685..8e18320c61 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -47,6 +47,22 @@ bool Stream::read_or_error(Bytes buffer) return true; } +ErrorOr<ByteBuffer> Stream::read_all() +{ + ByteBuffer output; + u8 buffer_raw[4096]; + Bytes buffer { buffer_raw, 4096 }; + + while (true) { + Bytes read_bytes = TRY(read(buffer)); + if (read_bytes.is_empty()) + break; + output.append(read_bytes); + } + + return output; +} + bool Stream::write_or_error(ReadonlyBytes buffer) { VERIFY(buffer.size()); diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 2089fdac49..ae7aff18da 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -37,6 +37,7 @@ public: /// Tries to fill the entire buffer through reading. Returns whether the /// buffer was filled without an error. virtual bool read_or_error(Bytes); + ErrorOr<ByteBuffer> read_all(); virtual bool is_writable() const { return false; } /// Tries to write the entire contents of the buffer. It is possible for |