16 m_bLeaveOpen(bLeaveOpen),
17 m_bIsLittleEndian(bIsLittleEndian),
20 if (m_pStream ==
nullptr) {
26 : m_pStream(spStream.Get()),
27 m_spOwnedStream(spStream),
28 m_bLeaveOpen(bLeaveOpen),
29 m_bIsLittleEndian(bIsLittleEndian),
32 if (m_pStream ==
nullptr) {
42 void BinaryReader::EnsureNotDisposed()
const {
58 if (!m_bLeaveOpen && m_pStream !=
nullptr) {
69 return m_bIsLittleEndian;
73 m_bIsLittleEndian = bIsLittleEndian;
76 void BinaryReader::FillBuffer(
char* pBuffer,
int iCount) {
82 while (iTotalRead < iCount) {
83 int iRead = m_pStream->
Read(pBuffer + iTotalRead, 0, iCount - iTotalRead);
93 for (
int i = 0, j = iSize - 1; i < j; ++i, --j) {
94 char chTemp = pBuf[i];
106 FillBuffer(&chBuf, 1);
107 return static_cast<byte>(chBuf);
111 return static_cast<signed char>(
ReadByte());
115 return static_cast<char>(
ReadByte());
127 for (
int i = 0; i < iCount; ++i) {
136 if (iIndex < 0 || iCount < 0 || (iIndex + iCount) > arrBuffer.
GetLength()) {
141 int iRead = m_pStream->Read(
reinterpret_cast<char*
>(arrBuffer.
GetData()) + iIndex, 0, iCount);
142 return iRead < 0 ? 0 : iRead;
146 char szBuf[2] = { 0 };
147 FillBuffer(szBuf, 2);
152 std::memcpy(&iVal, szBuf, 2);
157 return static_cast<unsigned short>(
ReadInt16());
161 char szBuf[4] = { 0 };
162 FillBuffer(szBuf, 4);
167 std::memcpy(&iVal, szBuf, 4);
172 return static_cast<unsigned int>(
ReadInt32());
176 char szBuf[8] = { 0 };
177 FillBuffer(szBuf, 8);
182 std::memcpy(&llVal, szBuf, 8);
187 return static_cast<unsigned long long>(
ReadInt64());
191 char szBuf[4] = { 0 };
192 FillBuffer(szBuf, 4);
197 std::memcpy(&fVal, szBuf, 4);
202 char szBuf[8] = { 0 };
203 FillBuffer(szBuf, 8);
208 std::memcpy(&dVal, szBuf, 8);
224 for (
int i = 0; i < iLength; ++i) {
225 arrChars[i] =
static_cast<char>(arrBytes[i]);
227 arrChars[iLength] =
'\0';
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.
The exception that is thrown when reading is attempted past the end of a stream.
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.
virtual String ReadString(int iLength)
Reads a string of specified byte length from the current stream.
bool IsLittleEndian() const
Gets whether values are decoded in little-endian order.
virtual int Read(Array< byte > &arrBuffer, int iIndex, int iCount)
Reads the specified number of bytes from the stream into an array starting at a specific index.
virtual short ReadInt16()
Reads a 2-byte signed integer from the current stream.
virtual bool ReadBoolean()
Reads a Boolean value from the current stream and advances the current position by one byte.
virtual int ReadInt32()
Reads a 4-byte signed integer from the current stream.
virtual double ReadDouble()
Reads an 8-byte floating point value from the current stream.
virtual signed char ReadSByte()
Reads a signed byte from this stream and advances the current position by one byte.
virtual float ReadSingle()
Reads a 4-byte floating point value from the current stream.
virtual unsigned long long ReadUInt64()
Reads an 8-byte unsigned integer from the current stream.
virtual void Close()
Closes the current reader and the underlying stream if configured.
virtual long long ReadInt64()
Reads an 8-byte signed integer from the current stream.
virtual Array< byte > ReadBytes(int iCount)
Reads the specified number of bytes from the current stream into a byte array.
virtual unsigned short ReadUInt16()
Reads a 2-byte unsigned integer from the current stream.
void Dispose() override
Releases all unmanaged resources used by the BinaryReader.
virtual byte ReadByte()
Reads the next byte from the current stream and advances the current position by one byte.
virtual unsigned int ReadUInt32()
Reads a 4-byte unsigned integer from the current stream.
BinaryReader(Stream *pStream, bool bLeaveOpen=false, bool bIsLittleEndian=true)
Initializes a new instance of the BinaryReader class based on the specified stream and endianness.
Stream * GetBaseStream() const
Exposes access to the underlying stream.
void SetLittleEndian(bool bIsLittleEndian)
Sets the endianness for binary decoding.
virtual char ReadChar()
Reads the next character from the current stream.
virtual ~BinaryReader()
Destructor ensuring deterministic cleanup and closing.
EndOfStreamException(const String &sMessage)
Initializes a new instance of the EndOfStreamException class with a specified error message.
Provides a generic view of a sequence of bytes.
virtual int Read(char *buffer, int offset, int count)=0
Reads a sequence of bytes from the current stream and advances the position within the stream by the ...
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.
String()
Initializes a new instance of the String class to an empty string.
static void ReverseBytes(char *pBuf, int iSize)
Reverse bytes in place for endian conversion.