CSharp Binary Stream library
    Preparing search index...

    Class BinaryReader

    A binary stream reader compatible with majority of methods in C#'s BinaryReader.

    Any time the word stream is used in the documentation it refers to the ArrayBuffer provided in the constructor of this class.

    Index

    Constructors

    • Creates a new reader powered by an ArrayBuffer.

      Parameters

      • stream: ArrayBuffer | Uint8Array<ArrayBufferLike>

        Stream from which to read the data.

      • endianness: Endianness = Endianness.Little

        Defaults to Little Endian.

      Returns BinaryReader

      There is a danger when accessing a TypedArray's buffer property, because the TypeArray is just a view into the ArrayBuffer that can have different offset and length.

      For example, imagine a buffer 00 01 02 03 04 05. const arr = new Uint8Array(buffer, 1, 2) will only have access to 01 02, but it is still powered by the same, 6-byte buffer, and doing new BinaryReader(arr.buffer) will also refer to the longer buffer.

      There are three solutions:

      1. If you have access to Uint8Array, simply use that.
      2. Create a new Uint8Array from a differently typed array: new Uint8Array(other.buffer, other.byteOffset, other.byteLength);
      3. Create a new ArrayBuffer using slice: arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength);

    Properties

    endianness: Endianness

    Endianness of all the read operations

    Accessors

    • get length(): number

      Length of the stream, in bytes loaded, into the reader.

      Returns number

    • get position(): number

      Return the current position in the stream from which the read operations happen.

      Returns number

    • set position(value: number): void

      Changes the current position in the stream from which the read operations happen. Trying to set it to value smaller than 0 will set it to 0 instead (the beginning of the stream). Trying to set it to value larger than length will set it to length instead (the end of the stream).

      Parameters

      • value: number

      Returns void

    • get remainingBytes(): number

      Number of bytes remaining to be read.

      Returns number

    Methods

    • Reads a specified number of bytes (unsigned 8-bit number) from the stream and advances the stream by the requested number of bytes.

      Parameters

      • bytesToRead: number

        The number of bytes to read from the stream. must be an integer larger or equal than zero.

      Returns number[]

      Array of bytes (numbers between 0 and 255) read from the stream. If bytesToRead is 0 it returns an empty array.

      InvalidArgumentError when bytesToRead is less than zero.

      EndOfStreamError Thrown when there are no bytes left in the stream.

    • Reads a single character from the string, the number of bytes read dependant on the encoding used.

      Parameters

      • encoding: Encoding = Encoding.Utf8

        The encoding to use when reading the chars.

      Returns string

      A single character read from the stream

      EncodingError Thrown when an unknown encoding is provided as the argument.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

      InvalidUtf8CharacterError Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.

    • Reads the specified number of bytes in the provided encoding and advances the stream by that number.

      Parameters

      • bytesToRead: number

        The number of bytes to read from the stream. If the number is fractional it is rounded down. Has to be at least 1. If it the number is less than 0 it returns an empty string.

      • encoding: Encoding = Encoding.Utf8

        The encoding to use when reading the chars.

      Returns string

      A string read from the stream.

      InvalidArgumentError Thrown when bytesToRead is not a number.

      EncodingError Thrown when an unknown encoding is provided as the argument.

      EndOfStreamError Thrown when there are not enough bytes left in the stream or when the function stops reading in the middle of a character sequence in multibyte character encodings. Position of the stream does not change if this exception is thrown.

      InvalidUtf8CharacterError Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.

    • Reads multiple characters from the string, the number of bytes read dependant on the encoding used.

      Parameters

      • charactersToRead: number

        The number of characters to read from the stream. If the number is fractional it is rounded down. If it the number is less than 0 it returns an empty string.

      • encoding: Encoding = Encoding.Utf8

        The encoding to use when reading the chars.

      Returns string

      A string read from the stream.

      InvalidArgumentError Thrown when charactersToRead is not a valid number.

      EncodingError Thrown when an unknown encoding is provided as the argument.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

      InvalidUtf8CharacterError Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.

    • Reads an int (signed 32-bit number) from the stream and advances the stream by four bytes.

      Returns number

      Number between -2,147,483,648 and 2,147,483,647.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

    • Reads a long (signed 64-bit number) from the stream and advances the stream by eight bytes. If the number is too big or too small precision errors may occur, refer to the remark in readLongString for more details.

      Returns number

      Number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

    • Reads a long (signed 64-bit number) from the stream as a string and advances the stream by eight bytes. The number is returned as string to avoid any precision loss caused by the value being stored in double internally (check the remarks for more details).

      Returns string

      String representing a number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807

      JavaScript internally uses double to represent all numbers. The smallest and largest number that can be represented without loss of precision are, respectively, −9,007,199,254,740,991 −(2^53 − 1) and 9,007,199,254,740,991 2^53 − 1, while long can hold values between -2^63 and 2^63 - 1, while unsigned long goes all the way up to 2^64-1.

      What happens when you go beyond those limits is that some numbers just cannot be expressed. 9007199254740992+1 is the same as 9007199254740992+1+1+1+1 and if you try to set a variable to 9007199254740993 it just gets rounded down.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

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

      Parameters

      • encoding: Encoding = Encoding.Utf8

        The encoding to use when reading the string.

      Returns string

      A string read from the stream.

      InvalidArgumentError Thrown when charactersToRead is not a number nor numeric string or when it is less than 1.

      EncodingError Thrown when an unknown encoding is provided as the argument.

      EndOfStreamError Thrown when there are not enough bytes left in the stream or when the length prefix is longer than 5 bytes.

      InvalidUtf8CharacterError Thrown when using UTF-8 encoding when an incorrect UTF-8 character sequence is encountered.

    • Reads an unsigned int (unsigned 32-bit number) from the stream and advances the stream by four bytes.

      Returns number

      Number between 0 and 4,294,967,296.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

    • Reads an unsigned long (unsigned 64-bit number) from the stream and advances the stream by eight bytes. If the number is too big precision errors may occur, refer to the remark in readLongString for more details.

      Returns number

      Number between 0 and 18,446,744,073,709,551,615.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

    • Reads an unsigned long (unsigned 64-bit number) from the stream as a string and advances the stream by eight bytes. The number is returned as a string to avoid any precision loss caused by the value being stored in double internally, refer to the remark in readLongString for more details.

      Returns string

      String representing a number between 0 and 18,446,744,073,709,551,615.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.

    • Reads an unsigned short (unsigned 16-bit number) from the stream and advances the stream by two bytes.

      Returns number

      Number between 0 and 65,535.

      EndOfStreamError Thrown when there are not enough bytes left in the stream. Position of the stream does not change if this exception is thrown.