summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorTim Schumacher <timschumi@gmx.de>2022-11-29 15:42:25 +0100
committerLinus Groh <mail@linusgroh.de>2022-12-10 12:05:55 +0000
commit4e7da96d5839e35e0a87406d0cfc78af0de4542e (patch)
treebff380437a4e28554dcb05bd893fdbddcafdde6e /Userland
parent35bcdefdf78215cfd8d36a26e182cc323dd177ac (diff)
downloadserenity-4e7da96d5839e35e0a87406d0cfc78af0de4542e.zip
LibCore: Add a wrapper for adapting Core::Stream to AK::InputStream
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibCore/Stream.cpp46
-rw-r--r--Userland/Libraries/LibCore/Stream.h13
2 files changed, 59 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp
index 3f198cb760..f598144c72 100644
--- a/Userland/Libraries/LibCore/Stream.cpp
+++ b/Userland/Libraries/LibCore/Stream.cpp
@@ -807,4 +807,50 @@ void WrappedAKOutputStream::close()
{
}
+WrapInAKInputStream::WrapInAKInputStream(Core::Stream::Stream& stream)
+ : m_stream(stream)
+{
+}
+
+size_t WrapInAKInputStream::read(Bytes bytes)
+{
+ if (has_any_error())
+ return 0;
+
+ auto data_or_error = m_stream.read(bytes);
+ if (data_or_error.is_error()) {
+ set_fatal_error();
+ return 0;
+ }
+
+ return data_or_error.value().size();
+}
+
+bool WrapInAKInputStream::unreliable_eof() const
+{
+ return m_stream.is_eof();
+}
+
+bool WrapInAKInputStream::read_or_error(Bytes bytes)
+{
+ if (read(bytes) < bytes.size()) {
+ set_fatal_error();
+ return false;
+ }
+
+ return true;
+}
+
+bool WrapInAKInputStream::discard_or_error(size_t count)
+{
+ auto maybe_error = m_stream.discard(count);
+
+ if (maybe_error.is_error()) {
+ set_fatal_error();
+ return false;
+ }
+
+ return true;
+}
+
}
diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h
index 87e983c17d..84785c11ca 100644
--- a/Userland/Libraries/LibCore/Stream.h
+++ b/Userland/Libraries/LibCore/Stream.h
@@ -1030,4 +1030,17 @@ private:
NonnullOwnPtr<OutputStream> m_stream;
};
+// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
+class WrapInAKInputStream final : public InputStream {
+public:
+ WrapInAKInputStream(Core::Stream::Stream& stream);
+ virtual size_t read(Bytes) override;
+ virtual bool unreliable_eof() const override;
+ virtual bool read_or_error(Bytes) override;
+ virtual bool discard_or_error(size_t count) override;
+
+private:
+ Core::Stream::Stream& m_stream;
+};
+
}