diff --git a/includes/csharp/io/binary.hpp b/includes/csharp/io/binary.hpp index 0e23609..86bc5fc 100644 --- a/includes/csharp/io/binary.hpp +++ b/includes/csharp/io/binary.hpp @@ -190,14 +190,65 @@ namespace csharp { class BinaryWriter { public: - BinaryWriter(std::shared_ptr const& output) { - OutStream = Stream::Null; + BinaryWriter(std::shared_ptr const& output) + : BinaryWriter(output, false) { } + + BinaryWriter(std::shared_ptr const& output, bool leaveOpen) { + ArgumentNullException::ThrowIfNull(output.get(), "output"); + + if (!output->CanWrite()) + throw ArgumentException(SR::Argument_StreamNotWritable); + + OutStream = output; + _leaveOpen = leaveOpen; } - BinaryWriter(std::shared_ptr const& output, bool leaveOpen); + inline void Close() const { + if (_leaveOpen) + OutStream->Flush(); + else + OutStream->Close(); + } + + inline virtual std::shared_ptr BaseStream() { + Flush(); + return OutStream; + } + + inline virtual void Flush() { + OutStream->Flush(); + } + + inline virtual int64_t Seek(int32_t offset, SeekOrigin origin) { + return OutStream->Seek(offset, origin); + } + + inline virtual void Write(bool value) { + OutStream->WriteByte(static_cast(value ? 1 : 0)); + } + + inline virtual void Write(uint8_t value) { + OutStream->WriteByte(value); + } + + inline virtual void Write(int8_t value) { + OutStream->WriteByte(static_cast(value)); + } + + inline virtual void Write(uint8_t const* buffer, int32_t bufferLength) { + OutStream->Write(buffer, bufferLength, 0, bufferLength); + } + + inline virtual void Write(uint8_t const* buffer, int32_t bufferLength, int32_t index, int32_t count) { + OutStream->Write(buffer, bufferLength, index, count); + } + + virtual void Write(char ch); protected: - BinaryWriter(); + BinaryWriter() { + OutStream = Stream::Null; + } protected: std::shared_ptr OutStream; diff --git a/includes/csharp/sr.hpp b/includes/csharp/sr.hpp index ff8e0a8..4c0b57f 100644 --- a/includes/csharp/sr.hpp +++ b/includes/csharp/sr.hpp @@ -56,7 +56,9 @@ namespace csharp { inline static const std::string Format_Bad7BitInt = "Too many bytes in what should have been a 7-bit encoded integer."; inline static const std::string IO_InvalidStringLen_Len - = "BinaryReader encountered an invalid string length."; + = "BinaryReader encountered an invalid string length."; + inline static const std::string Argument_StreamNotWritable + = "Stream was not writable."; }; };