CSharp Binary Stream library
    Preparing search index...

    Class BinaryWriter

    A binary stream writer compatible with majority of methods in C#'s BinaryWriter.

    All write operations advance the position by the number of bytes that were written.

    Any time the word stream or buffer is used in the documentation it refers to the internal array that represents the written data.

    Index

    Constructors

    • Creates a new BinaryWriter with empty writing buffer.

      Parameters

      • Optionalendianness: Endianness

        Defaults to Little Endian.

      Returns BinaryWriter

    • Creates a new BinaryWriter and fills its buffer with the specified array. Position is set to the end of the buffer, meaning any subsequent writes will append new data at the end.

      Parameters

      • array: number[]
      • Optionalendianness: Endianness

        Defaults to Little Endian.

      Returns BinaryWriter

      There is no syncing between the buffer and the passed array, changes to either won't be reflected in the other.

      OutOfBoundsError Thrown when any of the array elements provided is outside byte range

    • Creates a new BinaryWriter and fills its buffer with the contents of the array. Position is set to the end of the buffer, meaning any subsequent writes will append new data at the end.

      Parameters

      • array: Uint8Array
      • Optionalendianness: Endianness

        Defaults to Little Endian.

      Returns BinaryWriter

      There is no syncing between the buffer and the passed array, changes to either won't be reflected in the other.

    Accessors

    • get position(): number

      Current position inside the buffer denoting the place at which the next write operation will happen.

      Returns number

    • set position(value: number): void

      Changes the position inside the buffer at which the next write operation will happen. Setting it to less than 0 will clamp it to 0, and setting it to anything more than length will clamp it to length.

      Parameters

      • value: number

      Returns void

    Methods

    • Completely clears the underlying buffer and changes position and length to zero.

      Returns void

    • Returns the contents of the writer as regular array of bytes.

      Returns number[]

    • Returns the contents of the writer as Uint8Array

      Returns Uint8Array

    • Writes a single character in the specified encoding and advances the position by the number of bytes the character takes in that encoding.

      Parameters

      • character: string | number

        Unicode codepoint of the character to write or a string, in which case only the first character is used.

      • encoding: Encoding = Encoding.Utf8

        Character encoding to use when writing the character.

      Returns void

      InvalidArgumentError Thrown when null is passed for character or when the codepoint passed in character is negative, +/- infinite or NaN

      EncodingError Thrown when unknown or unsupported encoding is passed.

    • Writes multiple characters in the specified encoding and advances the position by the number of bytes the characters take in that encoding.

      Parameters

      • characters: string | number[]

        Unicode codepoints of the character to write or a string.

      • encoding: Encoding = Encoding.Utf8

        Character encoding to use when writing the characters.

      Returns void

      InvalidArgumentError Thrown when null is passed for character or when any of the codepoints passed in characters is negative, +/- infinite or NaN

      EncodingError Thrown when unknown or unsupported encoding is passed.

    • Writes a long and advances the position by eight bytes.

      Parameters

      • value: string | number

        Long to write accepted both as a string (for 100% precision in very low/high numbers) and number, when precision is not a requirement.

      Returns void

      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.

      InvalidArgumentError Thrown when value is NaN or +/- infinite.

      OutOfBoundsError Thrown when value is less than -9,223,372,036,854,775,808 more than 9,223,372,036,854,775,807 +/- infinity or NaN.

    • Writes the same byte multiple times and advances the position by repeats bytes.

      Parameters

      • value: number

        Byte to write.

      • repeats: number

        Number of times to write the byte.

      Returns void

      InvalidArgumentError Thrown when repeats is less than 0, +/- infinity or NaN.

      OutOfBoundsError Thrown when value is less than 0, more than 255, +/- infinity or NaN.

    • Writes length-prefixed multiple characters in the specified encoding and advances the position by the number of bytes the characters take in that encoding.

      Parameters

      • value: string | number[]

        Unicode codepoints of the character to write or a string.

      • encoding: Encoding = Encoding.Utf8

        Character encoding to use when writing the characters.

      Returns void

      InvalidArgumentError Thrown when null is passed for character or when any of the codepoints passed in characters is negative, +/- infinite or NaN

      EncodingError Thrown when unknown or unsupported encoding is passed.

    • Writes an unsigned long and advances the position by eight bytes. See the remark in writeLong for details about why strings are preferred.

      Parameters

      • value: string | number

        Unsigned long to write accepted both as a string (for 100% precision in very low/high numbers) and number, when precision is not a requirement.

      Returns void

      InvalidArgumentError Thrown when value is NaN or +/- infinite.

      OutOfBoundsError Thrown when value is less than 0 more than 18,446,744,073,709,551,615 +/- infinity or NaN.