DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
BinaryWriter.cpp
Go to the documentation of this file.
1#include "pch.h"
7#include <cstring>
8
9namespace DotNetDupe {
10 namespace System {
11 namespace IO {
12
13 BinaryWriter::BinaryWriter(Stream* pStream, bool bLeaveOpen, bool bIsLittleEndian)
14 : m_pStream(pStream),
15 m_bLeaveOpen(bLeaveOpen),
16 m_bIsLittleEndian(bIsLittleEndian),
17 m_bDisposed(false) {
19 if (m_pStream == nullptr) {
20 throw ArgumentNullException("pStream cannot be null.");
21 }
22 }
23
24 BinaryWriter::BinaryWriter(SmartPointer<Stream> spStream, bool bLeaveOpen, bool bIsLittleEndian)
25 : m_pStream(spStream.Get()),
26 m_spOwnedStream(spStream),
27 m_bLeaveOpen(bLeaveOpen),
28 m_bIsLittleEndian(bIsLittleEndian),
29 m_bDisposed(false) {
31 if (m_pStream == nullptr) {
32 throw ArgumentNullException("spStream cannot be null.");
33 }
34 }
35
40
41 void BinaryWriter::EnsureNotDisposed() const {
43 if (m_bDisposed) {
44 throw InvalidOperationException("BinaryWriter is disposed.");
45 }
46 }
47
50 Dispose();
51 }
52
55 if (!m_bDisposed) {
56 m_bDisposed = true;
57 if (!m_bLeaveOpen && m_pStream != nullptr) {
58 m_pStream->Dispose();
59 }
60 }
61 }
62
65 EnsureNotDisposed();
66
68 m_pStream->Flush();
69 }
70
72 return m_pStream;
73 }
74
76 return m_bIsLittleEndian;
77 }
78
79 void BinaryWriter::SetLittleEndian(bool bIsLittleEndian) {
80 m_bIsLittleEndian = bIsLittleEndian;
81 }
82
83 long BinaryWriter::Seek(long lOffset, int iOrigin) {
85 EnsureNotDisposed();
86
88 return m_pStream->Seek(lOffset, iOrigin);
89 }
90
91 void BinaryWriter::WriteInternal(const char* pBuffer, int iCount) {
93 EnsureNotDisposed();
94
96 m_pStream->Write(pBuffer, 0, iCount);
97 }
98
100 static void ReverseBytes(char* pBuf, int iSize) {
101 for (int i = 0, j = iSize - 1; i < j; ++i, --j) {
102 char chTemp = pBuf[i];
103 pBuf[i] = pBuf[j];
104 pBuf[j] = chTemp;
105 }
106 }
107
108 void BinaryWriter::Write(bool bValue) {
109 byte bVal = bValue ? 1 : 0;
110 Write(bVal);
111 }
112
113 void BinaryWriter::Write(byte bValue) {
114 char chBuf = static_cast<char>(bValue);
115 WriteInternal(&chBuf, 1);
116 }
117
118 void BinaryWriter::Write(char chValue) {
119 WriteInternal(&chValue, 1);
120 }
121
122 void BinaryWriter::Write(signed char chValue) {
123 char chBuf = static_cast<char>(chValue);
124 WriteInternal(&chBuf, 1);
125 }
126
127 void BinaryWriter::Write(const Array<byte>& arrBuffer) {
128 Write(arrBuffer, 0, arrBuffer.GetLength());
129 }
130
131 void BinaryWriter::Write(const Array<byte>& arrBuffer, int iIndex, int iCount) {
133 EnsureNotDisposed();
134 if (iIndex < 0 || iCount < 0 || (iIndex + iCount) > arrBuffer.GetLength()) {
135 throw ArgumentOutOfRangeException("Invalid index and count bounds.");
136 }
137
139 const char* pData = reinterpret_cast<const char*>(arrBuffer.GetData()) + iIndex;
140 WriteInternal(pData, iCount);
141 }
142
143 void BinaryWriter::Write(short iValue) {
144 char szBuf[2] = { 0 };
145 std::memcpy(szBuf, &iValue, 2);
146 if (m_bIsLittleEndian != BitConverter::IsLittleEndian) {
147 ReverseBytes(szBuf, 2);
148 }
149 WriteInternal(szBuf, 2);
150 }
151
152 void BinaryWriter::Write(unsigned short uValue) {
153 Write(static_cast<short>(uValue));
154 }
155
156 void BinaryWriter::Write(int iValue) {
157 char szBuf[4] = { 0 };
158 std::memcpy(szBuf, &iValue, 4);
159 if (m_bIsLittleEndian != BitConverter::IsLittleEndian) {
160 ReverseBytes(szBuf, 4);
161 }
162 WriteInternal(szBuf, 4);
163 }
164
165 void BinaryWriter::Write(unsigned int uValue) {
166 Write(static_cast<int>(uValue));
167 }
168
169 void BinaryWriter::Write(long long llValue) {
170 char szBuf[8] = { 0 };
171 std::memcpy(szBuf, &llValue, 8);
172 if (m_bIsLittleEndian != BitConverter::IsLittleEndian) {
173 ReverseBytes(szBuf, 8);
174 }
175 WriteInternal(szBuf, 8);
176 }
177
178 void BinaryWriter::Write(unsigned long long ullValue) {
179 Write(static_cast<long long>(ullValue));
180 }
181
182 void BinaryWriter::Write(float fValue) {
183 char szBuf[4] = { 0 };
184 std::memcpy(szBuf, &fValue, 4);
185 if (m_bIsLittleEndian != BitConverter::IsLittleEndian) {
186 ReverseBytes(szBuf, 4);
187 }
188 WriteInternal(szBuf, 4);
189 }
190
191 void BinaryWriter::Write(double dValue) {
192 char szBuf[8] = { 0 };
193 std::memcpy(szBuf, &dValue, 8);
194 if (m_bIsLittleEndian != BitConverter::IsLittleEndian) {
195 ReverseBytes(szBuf, 8);
196 }
197 WriteInternal(szBuf, 8);
198 }
199
200 void BinaryWriter::Write(const String& sValue) {
202 int iLen = sValue.GetLength();
203 if (iLen > 0) {
204 WriteInternal(sValue.GetRawString(), iLen);
205 }
206 }
207
208 }
209 }
210}
Defines the exception thrown when a null reference is passed to a method that does not accept it.
Defines the exception thrown when an argument value is outside the acceptable range of values.
Converts base data types to arrays of bytes, and arrays of bytes to base data types.
Defines the exception thrown when a method call is invalid for the object's current state.
ArgumentNullException(const String &sMessage)
Initializes a new instance of the ArgumentNullException class with a specified error message.
ArgumentOutOfRangeException(const String &sMessage)
Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
T * GetData()
Gets a pointer to the contiguous internal element buffer.
Definition Array.h:146
static const bool IsLittleEndian
Indicates whether this computer architecture is little-endian.
bool IsLittleEndian() const
Gets whether values are encoded in little-endian order.
virtual void Flush()
Clears all buffers for the current writer and causes any buffered data to be written to the underlyin...
virtual ~BinaryWriter()
Destructor releasing writer resources.
void Dispose() override
Releases all unmanaged resources used by the BinaryWriter.
Stream * GetBaseStream() const
Exposes access to the underlying stream.
virtual void Close()
Closes the current BinaryWriter and the underlying stream if configured.
virtual long Seek(long lOffset, int iOrigin)
Sets the position within the current stream.
virtual void Write(bool bValue)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing t...
void SetLittleEndian(bool bIsLittleEndian)
Sets the endianness for binary encoding.
BinaryWriter(Stream *pStream, bool bLeaveOpen=false, bool bIsLittleEndian=true)
Initializes a new instance of the BinaryWriter class based on the specified stream and endianness.
Provides a generic view of a sequence of bytes.
Definition Stream.h:16
virtual void Write(const char *buffer, int offset, int count)=0
Writes a sequence of bytes to the current stream and advances the current position within this stream...
InvalidOperationException(const String &sMessage)
Initializes a new instance of the InvalidOperationException class with a specified error message.
A unified smart pointer that supports both unique and shared ownership semantics.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
const char * GetRawString() const
Definition String.cpp:230
static void ReverseBytes(char *pBuf, int iSize)
Reverse bytes in place for endian conversion.