From 7a065513cde6a906f794d3b5828dfb575ea4d4be Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 28 Nov 2022 17:37:36 +0100 Subject: [PATCH] LibCore: Add a basic wrapper for adapting AK::Stream to Core::Stream --- Userland/Libraries/LibCore/Stream.cpp | 68 +++++++++++++++++++++++++++ Userland/Libraries/LibCore/Stream.h | 28 +++++++++++ 2 files changed, 96 insertions(+) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index 579a23045b..cf19566079 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -714,4 +714,72 @@ ErrorOr LocalSocket::release_fd() return fd; } +WrappedAKInputStream::WrappedAKInputStream(NonnullOwnPtr stream) + : m_stream(move(stream)) +{ +} + +ErrorOr WrappedAKInputStream::read(Bytes bytes) +{ + auto bytes_read = m_stream->read(bytes); + + if (m_stream->has_any_error()) + return Error::from_string_literal("Underlying InputStream indicated an error"); + + return bytes.slice(0, bytes_read); +} + +ErrorOr WrappedAKInputStream::write(ReadonlyBytes) +{ + VERIFY_NOT_REACHED(); +} + +bool WrappedAKInputStream::is_eof() const +{ + return m_stream->unreliable_eof(); +} + +bool WrappedAKInputStream::is_open() const +{ + return true; +} + +void WrappedAKInputStream::close() +{ +} + +WrappedAKOutputStream::WrappedAKOutputStream(NonnullOwnPtr stream) + : m_stream(move(stream)) +{ +} + +ErrorOr WrappedAKOutputStream::read(Bytes) +{ + VERIFY_NOT_REACHED(); +} + +ErrorOr WrappedAKOutputStream::write(ReadonlyBytes bytes) +{ + auto bytes_written = m_stream->write(bytes); + + if (m_stream->has_any_error()) + return Error::from_string_literal("Underlying OutputStream indicated an error"); + + return bytes_written; +} + +bool WrappedAKOutputStream::is_eof() const +{ + VERIFY_NOT_REACHED(); +} + +bool WrappedAKOutputStream::is_open() const +{ + return true; +} + +void WrappedAKOutputStream::close() +{ +} + } diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index e2ed31a75e..3fc95f1cc4 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -968,4 +968,32 @@ private: using ReusableTCPSocket = BasicReusableSocket; using ReusableUDPSocket = BasicReusableSocket; +// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. +class WrappedAKInputStream final : public Stream { +public: + WrappedAKInputStream(NonnullOwnPtr stream); + virtual ErrorOr read(Bytes) override; + virtual ErrorOr write(ReadonlyBytes) override; + virtual bool is_eof() const override; + virtual bool is_open() const override; + virtual void close() override; + +private: + NonnullOwnPtr m_stream; +}; + +// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts. +class WrappedAKOutputStream final : public Stream { +public: + WrappedAKOutputStream(NonnullOwnPtr stream); + virtual ErrorOr read(Bytes) override; + virtual ErrorOr write(ReadonlyBytes) override; + virtual bool is_eof() const override; + virtual bool is_open() const override; + virtual void close() override; + +private: + NonnullOwnPtr m_stream; +}; + }