Creates a new reader powered by an ArrayBuffer.
Stream from which to read the data.
Defaults to Little Endian.
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:
Uint8Array, simply use that.Uint8Array from a differently typed array:
new Uint8Array(other.buffer, other.byteOffset, other.byteLength);ArrayBuffer using slice:
arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength);Length of the stream, in bytes loaded, into the reader.
Return the current position in the stream from which the read operations happen.
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).
Number of bytes remaining to be read.
Reads a boolean from the stream and advances the stream by one byte.
false if it's zero and true if it is not zero.
EndOfStreamError Thrown when there are no bytes left in the stream.
Reads a byte (unsigned 8-bit number) from the stream and advances the stream by one byte.
Number between 0 and 255.
EndOfStreamError Thrown when there are no bytes left in the stream.
Reads a specified number of bytes (unsigned 8-bit number) from the stream and advances the stream by the requested number of bytes.
The number of bytes to read from the stream. must be an integer larger or equal than zero.
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.
The encoding to use when reading the chars.
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.
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.
The encoding to use when reading the chars.
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.
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.
The encoding to use when reading the chars.
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 a double (double-precision floating-point number) from the stream and advances the stream by eight bytes.
Double number
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 float (single-precision floating-point number) from the stream and advances the stream by four bytes.
Float number
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 int (signed 32-bit number) from the stream and advances the stream by four bytes.
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.
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).
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 short (signed 16-bit number) from the stream and advances the stream by two bytes.
Number between -65,536 and 32,767.
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 signed byte (signed 8-bit number) from the stream and advances the stream by one byte.
Number between -128 and 127.
EndOfStreamError Thrown when there are no bytes left in the stream.
Reads a string from the stream that is prefixed with its length, encoded as an integer seven bits at a time.
The encoding to use when reading the 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.
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.
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.
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.
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.
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
ArrayBufferprovided in the constructor of this class.