BinaryReader Class

Reads primitive data types as binary values in a specific encoding.

Syntax

Declaration

[ComVisible(true)]
public class BinaryReader : IDisposable

Inheritance Hierarchy

Inherited Members

System.Object

, , , , , ,

Constructors summary

Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.

Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream open.

Properties summary

Exposes access to the underlying stream of the BinaryReader.

Methods summary

Releases all resources used by the current instance of the BinaryReader class.

Releases the unmanaged resources used by the BinaryReader class and optionally releases the managed resources.

Fills the internal buffer with the specified number of bytes read from the stream.

Returns the next available character and does not advance the byte or character position.

Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

Reads the specified number of bytes from the stream, starting from a specified point in the byte array.

Reads the specified number of characters from the stream, starting from a specified point in the character array.

Reads in a 32-bit integer in compressed format.

Reads a Boolean value from the current stream and advances the current position of the stream by one byte.

Reads the next byte from the current stream and advances the current position of the stream by one byte.

Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.

Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.

Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.

Reads a signed byte from this stream and advances the current position of the stream by one byte.

Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.

Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.

Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.

Constructors

  • BinaryReader(Stream)

    Initializes a new instance of the BinaryReader class based on the specified stream and using UTF-8 encoding.

    public BinaryReader(Stream input)

    Parameters

    • input

      The input stream.

    Exceptions

    • The stream does not support reading, is null, or is already closed.

  • BinaryReader(Stream, Encoding)

    Initializes a new instance of the BinaryReader class based on the specified stream and character encoding.

    public BinaryReader(Stream input, Encoding encoding)

    Parameters

    • input

      The input stream.

    • encoding

      The character encoding to use.

    Exceptions

  • BinaryReader(Stream, Encoding, Boolean)

    Initializes a new instance of the BinaryReader class based on the specified stream and character encoding, and optionally leaves the stream open.

    public BinaryReader(Stream input, Encoding encoding, bool leaveOpen)

    Parameters

    • input

      The input stream.

    • encoding

      The character encoding to use.

    • leaveOpen

      true to leave the stream open after the BinaryReader object is disposed; otherwise, false.

    Exceptions

Properties

  • BaseStream

    Exposes access to the underlying stream of the BinaryReader.

    public virtual Stream BaseStream { get; }

    Property Value

    • The underlying stream associated with the BinaryReader.

Methods

  • Dispose()

    Releases all resources used by the current instance of the BinaryReader class.

    public void Dispose()
  • Dispose(Boolean)

    Releases the unmanaged resources used by the BinaryReader class and optionally releases the managed resources.

    protected virtual void Dispose(bool disposing)

    Parameters

    • disposing

      true to release both managed and unmanaged resources; false to release only unmanaged resources.

  • FillBuffer(Int32)

    Fills the internal buffer with the specified number of bytes read from the stream.

    protected virtual void FillBuffer(int numBytes)

    Parameters

    • numBytes

      The number of bytes to be read.

    Exceptions

  • PeekChar()

    Returns the next available character and does not advance the byte or character position.

    public virtual int PeekChar()

    Returns

    • The next available character, or -1 if no more characters are available or the stream does not support seeking.

    Exceptions

  • Read()

    Reads characters from the underlying stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

    public virtual int Read()

    Returns

    • The next character from the input stream, or -1 if no characters are currently available.

    Exceptions

  • Read(Byte[], Int32, Int32)

    Reads the specified number of bytes from the stream, starting from a specified point in the byte array.

    public virtual int Read(byte[] buffer, int index, int count)

    Parameters

    • buffer

      The buffer to read data into.

    • index

      The starting point in the buffer at which to begin reading into the buffer.

    • count

      The number of bytes to read.

    Returns

    • The number of bytes read into buffer. This might be less than the number of bytes requested if that many bytes are not available, or it might be zero if the end of the stream is reached.

    Exceptions

  • Read(Char[], Int32, Int32)

    Reads the specified number of characters from the stream, starting from a specified point in the character array.

    public virtual int Read(char[] buffer, int index, int count)

    Parameters

    • buffer

      The buffer to read data into.

    • index

      The starting point in the buffer at which to begin reading into the buffer.

    • count

      The number of characters to read.

    Returns

    • The total number of characters read into the buffer. This might be less than the number of characters requested if that many characters are not currently available, or it might be zero if the end of the stream is reached.

    Exceptions

  • Read7BitEncodedInt()

    Reads in a 32-bit integer in compressed format.

    protected int Read7BitEncodedInt()

    Returns

    • A 32-bit integer in compressed format.

    Exceptions

  • ReadBoolean()

    Reads a Boolean value from the current stream and advances the current position of the stream by one byte.

    public virtual bool ReadBoolean()

    Returns

    • true if the byte is nonzero; otherwise, false.

    Exceptions

  • ReadByte()

    Reads the next byte from the current stream and advances the current position of the stream by one byte.

    public virtual byte ReadByte()

    Returns

    • The next byte read from the current stream.

    Exceptions

  • ReadBytes(Int32)

    Reads the specified number of bytes from the current stream into a byte array and advances the current position by that number of bytes.

    public virtual byte[] ReadBytes(int count)

    Parameters

    • count

      The number of bytes to read. This value must be 0 or a non-negative number or an exception will occur.

    Returns

    • A byte array containing data read from the underlying stream. This might be less than the number of bytes requested if the end of the stream is reached.

    Exceptions

  • ReadChar()

    Reads the next character from the current stream and advances the current position of the stream in accordance with the Encoding used and the specific character being read from the stream.

    public virtual char ReadChar()

    Returns

    • A character read from the current stream.

    Exceptions

  • ReadChars(Int32)

    Reads the specified number of characters from the current stream, returns the data in a character array, and advances the current position in accordance with the Encoding used and the specific character being read from the stream.

    public virtual char[] ReadChars(int count)

    Parameters

    • count

      The number of characters to read.

    Returns

    • A character array containing data read from the underlying stream. This might be less than the number of characters requested if the end of the stream is reached.

    Exceptions

  • ReadDecimal()

    Reads a decimal value from the current stream and advances the current position of the stream by sixteen bytes.

    public virtual decimal ReadDecimal()

    Returns

    • A decimal value read from the current stream.

    Exceptions

  • ReadDouble()

    Reads an 8-byte floating point value from the current stream and advances the current position of the stream by eight bytes.

    public virtual double ReadDouble()

    Returns

    • An 8-byte floating point value read from the current stream.

    Exceptions

  • ReadInt16()

    Reads a 2-byte signed integer from the current stream and advances the current position of the stream by two bytes.

    public virtual short ReadInt16()

    Returns

    • A 2-byte signed integer read from the current stream.

    Exceptions

  • ReadInt32()

    Reads a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.

    public virtual int ReadInt32()

    Returns

    • A 4-byte signed integer read from the current stream.

    Exceptions

  • ReadInt64()

    Reads an 8-byte signed integer from the current stream and advances the current position of the stream by eight bytes.

    public virtual long ReadInt64()

    Returns

    • An 8-byte signed integer read from the current stream.

    Exceptions

  • ReadSByte()

    Reads a signed byte from this stream and advances the current position of the stream by one byte.

    [CLSCompliant(false)]
    public virtual sbyte ReadSByte()

    Returns

    • A signed byte read from the current stream.

    Exceptions

  • ReadSingle()

    Reads a 4-byte floating point value from the current stream and advances the current position of the stream by four bytes.

    public virtual float ReadSingle()

    Returns

    • A 4-byte floating point value read from the current stream.

    Exceptions

  • ReadString()

    Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

    public virtual string ReadString()

    Returns

    • The string being read.

    Exceptions

  • ReadUInt16()

    Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.

    [CLSCompliant(false)]
    public virtual ushort ReadUInt16()

    Returns

    • A 2-byte unsigned integer read from this stream.

    Exceptions

  • ReadUInt32()

    Reads a 4-byte unsigned integer from the current stream and advances the position of the stream by four bytes.

    [CLSCompliant(false)]
    public virtual uint ReadUInt32()

    Returns

    • A 4-byte unsigned integer read from this stream.

    Exceptions

  • ReadUInt64()

    Reads an 8-byte unsigned integer from the current stream and advances the position of the stream by eight bytes.

    [CLSCompliant(false)]
    public virtual ulong ReadUInt64()

    Returns

    • An 8-byte unsigned integer read from this stream.

    Exceptions

Details

Namespace

System.IO

Assembly

System.IO.dll