Skip to content

Commit dd948b0

Browse files
timschumiAtkinsSJ
authored andcommitted
LibCore: Add Stream::WrapInAKOutputStream
1 parent bd65ecf commit dd948b0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Userland/Libraries/LibCore/Stream.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,33 @@ bool WrapInAKInputStream::discard_or_error(size_t count)
830830
return true;
831831
}
832832

833+
WrapInAKOutputStream::WrapInAKOutputStream(Core::Stream::Stream& stream)
834+
: m_stream(stream)
835+
{
836+
}
837+
838+
size_t WrapInAKOutputStream::write(ReadonlyBytes bytes)
839+
{
840+
if (has_any_error())
841+
return 0;
842+
843+
auto length_or_error = m_stream.write(bytes);
844+
if (length_or_error.is_error()) {
845+
set_fatal_error();
846+
return 0;
847+
}
848+
849+
return length_or_error.value();
850+
}
851+
852+
bool WrapInAKOutputStream::write_or_error(ReadonlyBytes bytes)
853+
{
854+
if (write(bytes) < bytes.size()) {
855+
set_fatal_error();
856+
return false;
857+
}
858+
859+
return true;
860+
}
861+
833862
}

Userland/Libraries/LibCore/Stream.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,4 +1041,15 @@ class WrapInAKInputStream final : public InputStream {
10411041
Core::Stream::Stream& m_stream;
10421042
};
10431043

1044+
// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
1045+
class WrapInAKOutputStream final : public OutputStream {
1046+
public:
1047+
WrapInAKOutputStream(Core::Stream::Stream& stream);
1048+
virtual size_t write(ReadonlyBytes) override;
1049+
virtual bool write_or_error(ReadonlyBytes) override;
1050+
1051+
private:
1052+
Core::Stream::Stream& m_stream;
1053+
};
1054+
10441055
}

0 commit comments

Comments
 (0)