Class EndianBinaryReader
Equivalent of System.IO.BinaryReader, but with either endianness, depending on the EndianBitConverter it is constructed with. No data is buffered in the reader; the client may seek within the stream at will.
public class EndianBinaryReader : IDisposable
- Inheritance
-
EndianBinaryReader
- Implements
- Inherited Members
Constructors
EndianBinaryReader(Stream)
Equivalent of System.IO.BinaryWriter, but with either endianness, depending on the EndianBitConverter it is constructed with.
public EndianBinaryReader(Stream stream)
Parameters
streamStreamStream to read data from
EndianBinaryReader(Stream, bool)
Constructs a new binary reader with the given stream with the option to leave stream open on object dispose.
public EndianBinaryReader(Stream stream, bool leaveOpen)
Parameters
EndianBinaryReader(Stream, Encoding)
Constructs a new binary reader with the given stream, using the given encoding.
public EndianBinaryReader(Stream stream, Encoding encoding)
Parameters
streamStreamStream to read data from
encodingEncodingEncoding to use when reading character data
EndianBinaryReader(Stream, Encoding, bool)
Constructs a new binary reader with the given stream, using the given encoding with the option to leave stream open on object dispose.
public EndianBinaryReader(Stream stream, Encoding encoding, bool leaveOpen)
Parameters
streamStreamStream to read data from
encodingEncodingEncoding to use when reading character data
leaveOpenboolLeave stream open on object dispose
Properties
BaseStream
Gets the underlying stream of the EndianBinaryReader.
public BitStream BaseStream { get; }
Property Value
BitPosition
Gets the current bit position within the current byte.
public int BitPosition { get; }
Property Value
DesiredFilePosition
Gets or sets the desired file position for seeking operations.
public long DesiredFilePosition { get; set; }
Property Value
Encoding
Gets the encoding used to read strings.
public Encoding Encoding { get; }
Property Value
Endianness
Gets or sets the current endianness for this reader.
public Endianness Endianness { get; set; }
Property Value
Remarks
This property can be changed at any point during reading to switch between big-endian and little-endian byte orders.
FilePosition
Gets or sets the current file position for tracking purposes.
public long FilePosition { get; set; }
Property Value
FileSize
Gets or sets the total file size when reading from a file.
public long FileSize { get; set; }
Property Value
FullBitPosition
Gets the full bit position within the stream, calculated as byte position times 8 plus bit position.
public long FullBitPosition { get; }
Property Value
GlobalEndianness
Gets or sets the default endianness for all binary readers.
public static Endianness GlobalEndianness { get; set; }
Property Value
Length
Gets the length of the underlying stream in bytes.
public long Length { get; }
Property Value
Position
Gets the current position within the underlying stream.
public long Position { get; }
Property Value
Methods
Close()
Closes the reader, including the underlying stream..
public void Close()
Dispose()
Disposes of the underlying stream.
public void Dispose()
Read()
Reads a single character from the stream, using the character encoding for this reader. If no characters have been fully read by the time the stream ends, -1 is returned.
public int Read()
Returns
- int
The character read, or -1 for end of stream.
Read(byte[], int, int)
Reads the specified number of bytes into the given buffer, starting at the given index.
public int Read(byte[] buffer, int index, int count)
Parameters
bufferbyte[]The buffer to copy data into
indexintThe first index to copy data into
countintThe number of bytes to read
Returns
- int
The number of bytes actually read. This will only be less than the requested number of bytes if the end of the stream is reached.
Read(char[], int, int)
Reads the specified number of characters into the given buffer, starting at the given index.
public int Read(char[] data, int index, int count)
Parameters
datachar[]The buffer to copy data into
indexintThe first index to copy data into
countintThe number of characters to read
Returns
- int
The number of characters actually read. This will only be less than the requested number of characters if the end of the stream is reached.
Read(Span<byte>)
Reads the bytes into the given buffer.
public int Read(Span<byte> buffer)
Parameters
Returns
- int
The number of bytes actually read. This will only be less than the buffer capacity if the end of the stream is reached.
Read7BitEncodedInt()
Reads a 7-bit encoded integer from the stream. This is stored with the least significant information first, with 7 bits of information per byte of value, and the top bit as a continuation flag. This method is not affected by the endianness of the bit converter.
public int Read7BitEncodedInt()
Returns
- int
The 7-bit encoded integer read from the stream.
ReadBigEndian7BitEncodedInt()
Reads a 7-bit encoded integer from the stream. This is stored with the most significant information first, with 7 bits of information per byte of value, and the top bit as a continuation flag. This method is not affected by the endianness of the bit converter.
public int ReadBigEndian7BitEncodedInt()
Returns
- int
The 7-bit encoded integer read from the stream.
ReadBits(int)
Reads the specified number of bits from the stream using the current endianness.
public ulong ReadBits(int bitCount)
Parameters
bitCountintThe number of bits to read (maximum 64).
Returns
- ulong
The value read from the specified bits.
ReadBits(int, Endianness)
Reads the specified number of bits from the stream using the specified endianness.
public ulong ReadBits(int bitCount, Endianness endianness)
Parameters
bitCountintThe number of bits to read (maximum 64).
endiannessEndiannessThe endianness to use for the conversion.
Returns
- ulong
The value read from the specified bits.
ReadBoolean()
Reads a boolean from the stream. 1 byte is read.
public bool ReadBoolean()
Returns
- bool
The boolean read
ReadByte()
Reads a single byte from the stream.
public byte ReadByte()
Returns
- byte
The byte read
ReadBytes(int)
Reads the specified number of bytes, returning them in a new byte array. If not enough bytes are available before the end of the stream, this method will return what is available.
public byte[] ReadBytes(int count)
Parameters
countintThe number of bytes to read
Returns
- byte[]
The bytes read
ReadBytesOrThrow(int)
Reads the specified number of bytes, returning them in a new byte array. If not enough bytes are available before the end of the stream, this method will throw an IOException.
public byte[] ReadBytesOrThrow(int count)
Parameters
countintThe number of bytes to read
Returns
- byte[]
The bytes read
ReadDecimal()
Reads a decimal value from the stream, using the bit converter for this reader. 16 bytes are read.
public decimal ReadDecimal()
Returns
- decimal
The decimal value read
ReadDouble()
Reads a double-precision floating-point value from the stream, using the bit converter for this reader. 8 bytes are read.
public double ReadDouble()
Returns
- double
The floating point value read
ReadExpGolomb()
Reads an unsigned Exponential-Golomb coded value from the stream.
public uint ReadExpGolomb()
Returns
- uint
The decoded unsigned integer value.
ReadInt16()
Reads a 16-bit signed integer from the stream, using the bit converter for this reader. 2 bytes are read.
public short ReadInt16()
Returns
- short
The 16-bit integer read
ReadInt32()
Reads a 32-bit signed integer from the stream, using the bit converter for this reader. 4 bytes are read.
public int ReadInt32()
Returns
- int
The 32-bit integer read
ReadInt32(int)
Reads a 32-bit signed integer from the stream, using the bit converter for this reader. byteCount bytes are read (eg 3 bytes).
public int ReadInt32(int byteCount)
Parameters
byteCountintNumber of bytes to read
Returns
- int
The 32-bit integer read
ReadInt32(int, Endianness)
Reads a 32-bit signed integer from the stream, using specified bit converter. byteCount bytes are read (eg 3 bytes).
public int ReadInt32(int byteCount, Endianness endianness)
Parameters
byteCountintNumber of bytes to read
endiannessEndiannessEndian type
Returns
- int
The 32-bit integer read
ReadInt64()
Reads a 64-bit signed integer from the stream, using the bit converter for this reader. 8 bytes are read.
public long ReadInt64()
Returns
- long
The 64-bit integer read
ReadInt64(int)
Reads a 64-bit signed integer from the stream, using the bit converter for this reader. byteCount bytes are read (eg 6 bytes).
public long ReadInt64(int byteCount)
Parameters
byteCountintNumber of bytes to read
Returns
- long
The 64-bit integer read
ReadInt64(int, Endianness)
Reads a 64-bit signed integer from the stream, using specified bit converter. byteCount bytes are read (eg 6 bytes).
public long ReadInt64(int byteCount, Endianness endianness)
Parameters
byteCountintNumber of bytes to read
endiannessEndiannessEndian type
Returns
- long
The 64-bit integer read
ReadSByte()
Reads a single signed byte from the stream.
public sbyte ReadSByte()
Returns
- sbyte
The byte read
ReadSignedExpGolomb()
Reads a signed Exponential-Golomb coded value from the stream.
public int ReadSignedExpGolomb()
Returns
- int
The decoded signed integer value.
ReadSingle()
Reads a single-precision floating-point value from the stream, using the bit converter for this reader. 4 bytes are read.
public float ReadSingle()
Returns
- float
The floating point value read
ReadString()
Reads a length-prefixed string from the stream, using the encoding for this reader. A 7-bit encoded integer is first read, which specifies the number of bytes to read from the stream. These bytes are then converted into a string with the encoding for this reader.
public string ReadString()
Returns
- string
The string read from the stream.
ReadUInt16()
Reads a 16-bit unsigned integer from the stream, using the bit converter for this reader. 2 bytes are read.
public ushort ReadUInt16()
Returns
- ushort
The 16-bit unsigned integer read
ReadUInt32()
Reads a 32-bit unsigned integer from the stream, using the bit converter for this reader. 4 bytes are read.
public uint ReadUInt32()
Returns
- uint
The 32-bit unsigned integer read
ReadUInt32(int)
Reads a 32-bit unsigned integer from the stream, using the bit converter for this reader. byteCount bytes are read (eg 3 bytes).
public uint ReadUInt32(int byteCount)
Parameters
byteCountintNumber of bytes to read
Returns
- uint
The 32-bit unsigned integer read
ReadUInt32(int, Endianness)
Reads a 32-bit unsigned integer from the stream, using specified bit converter. byteCount bytes are read (eg 3 bytes).
public uint ReadUInt32(int byteCount, Endianness endianness)
Parameters
byteCountintNumber of bytes to read
endiannessEndiannessEndian type
Returns
- uint
The 32-bit unsigned integer read
ReadUInt64()
Reads a 64-bit unsigned integer from the stream, using the bit converter for this reader. 8 bytes are read.
public ulong ReadUInt64()
Returns
- ulong
The 64-bit unsigned integer read
Seek(long, SeekOrigin)
Seeks within the stream.
public void Seek(long offset, SeekOrigin origin)
Parameters
offsetlongOffset to seek to.
originSeekOriginOrigin of seek operation.
SeekBits(long, SeekOrigin)
Seeks within the stream.
public void SeekBits(long offset, SeekOrigin origin)
Parameters
offsetlongOffset to seek to.
originSeekOriginOrigin of seek operation.