15 m_bLeaveOpen(bLeaveOpen),
16 m_bIsLittleEndian(bIsLittleEndian),
19 if (m_pStream ==
nullptr) {
25 : m_pStream(spStream.Get()),
26 m_spOwnedStream(spStream),
27 m_bLeaveOpen(bLeaveOpen),
28 m_bIsLittleEndian(bIsLittleEndian),
31 if (m_pStream ==
nullptr) {
41 void BinaryWriter::EnsureNotDisposed()
const {
57 if (!m_bLeaveOpen && m_pStream !=
nullptr) {
76 return m_bIsLittleEndian;
80 m_bIsLittleEndian = bIsLittleEndian;
88 return m_pStream->Seek(lOffset, iOrigin);
91 void BinaryWriter::WriteInternal(
const char* pBuffer,
int iCount) {
96 m_pStream->
Write(pBuffer, 0, iCount);
101 for (
int i = 0, j = iSize - 1; i < j; ++i, --j) {
102 char chTemp = pBuf[i];
109 byte bVal = bValue ? 1 : 0;
114 char chBuf =
static_cast<char>(bValue);
115 WriteInternal(&chBuf, 1);
119 WriteInternal(&chValue, 1);
123 char chBuf =
static_cast<char>(chValue);
124 WriteInternal(&chBuf, 1);
134 if (iIndex < 0 || iCount < 0 || (iIndex + iCount) > arrBuffer.
GetLength()) {
139 const char* pData =
reinterpret_cast<const char*
>(arrBuffer.
GetData()) + iIndex;
140 WriteInternal(pData, iCount);
144 char szBuf[2] = { 0 };
145 std::memcpy(szBuf, &iValue, 2);
149 WriteInternal(szBuf, 2);
153 Write(
static_cast<short>(uValue));
157 char szBuf[4] = { 0 };
158 std::memcpy(szBuf, &iValue, 4);
162 WriteInternal(szBuf, 4);
166 Write(
static_cast<int>(uValue));
170 char szBuf[8] = { 0 };
171 std::memcpy(szBuf, &llValue, 8);
175 WriteInternal(szBuf, 8);
179 Write(
static_cast<long long>(ullValue));
183 char szBuf[4] = { 0 };
184 std::memcpy(szBuf, &fValue, 4);
188 WriteInternal(szBuf, 4);
192 char szBuf[8] = { 0 };
193 std::memcpy(szBuf, &dValue, 8);
197 WriteInternal(szBuf, 8);
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...
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
T * GetData()
Gets a pointer to the contiguous internal element buffer.
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.
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.
const char * GetRawString() const
static void ReverseBytes(char *pBuf, int iSize)
Reverse bytes in place for endian conversion.