Class BinaryDecoder

Decodes binary data from a buffer.

Hierarchy

  • default
    • BinaryDecoder

Constructors

  • Creates a new BinaryDecoder that can read the given buffer.

    Parameters

    • buffer: Buffer

      Buffer to decode.

    • initialOffset: number = 0

      Initial offset to use (0 by default).

    • endianness: Endianness = "LE"

      Endianness to use (little endian by default).

    Returns BinaryDecoder

Accessors

  • get buffer(): Buffer
  • Buffer being read/written.

    Returns Buffer

  • get byteLength(): number
  • Total byte length of buffer.

    Returns number

  • get bytesRemaining(): number
  • Number of bytes between current offset and end of buffer.

    Returns number

  • get isOutOfBounds(): boolean
  • Whether current offset is out of the buffer's bounds.

    Returns boolean

  • get offset(): number
  • Offset where data is currently being read/written.

    Returns number

Methods

  • Reads the next byte as a boolean (0 is false, anything else is true).

    Returns boolean

    The boolean value of the next byte.

    Throws

    If buffer does not contain at least 1 more byte.

  • Reads a single byte as a UInt8 (alias of uint8).

    Returns number

    A single byte value.

    Throws

    If buffer does not contain at least 1 more byte.

  • Reads the given number of bytes as UInt8s.

    Parameters

    • num: number

      The number of bytes to read.

    Returns number[]

    An array of bytes.

    Throws

    If buffer does not contain the specified number of bytes.

  • Reads the given number of bytes as a string with the given encoding.

    Parameters

    • bytes: number

      Number of bytes to read.

    • encoding: BufferEncoding = "utf-8"

      Character encoding to parse with ("utf-8" by default).

    Returns string

    String parsed with given encoding.

    Throws

    If buffer does not contain the specified number of bytes.

  • Reads the given number of bytes as a string of Base64 characters.

    Parameters

    • bytes: number

      Number of bytes to read.

    Returns string

    The characters read as a string with Base64 encoding.

    Deprecated

    Use chars instead (use "base64" as second argument).

    Throws

    If buffer does not contain the specified number of bytes.

  • Reads the given number of bytes as a string of UTF-8 characters.

    Parameters

    • bytes: number

      Number of bytes to read.

    Returns string

    The characters read as a string with UTF-8 encoding.

    Deprecated

    Use chars instead.

    Throws

    If buffer does not contain the specified number of bytes.

  • Reads a double. Consumes 8 bytes.

    Returns number

    The read double.

    Throws

    If buffer does not contain at least 8 more bytes.

  • Reads a float. Consumes 4 bytes.

    Returns number

    The read float.

    Throws

    If buffer does not contain at least 4 more bytes.

  • Returns a new BinaryDecoder using this coder's buffer.

    Parameters

    • initialOffset: number = 0

      Initial offset to use (0 by default).

    • endianness: Endianness = "LE"

      Endianness to use (little endian by default).

    Returns BinaryDecoder

    New decoder for this coder's buffer.

  • Returns a new BinaryEncoder using this coder's buffer.

    Parameters

    • initialOffset: number = 0

      Initial offset to use (0 by default).

    • endianness: Endianness = "LE"

      Endianness to use (little endian by default).

    Returns BinaryEncoder

    New encoder for this coder's buffer.

  • Returns whether this coder can safely consume the given number of bytes from the given offset (or the current offset if omitted).

    Parameters

    • bytes: number

      Number of bytes to check.

    • Optional fromOffset: number

      Offset to check from, if different than current one.

    Returns boolean

    True if these bytes can be safely consumed, false otherwise.

  • Reads a 16-bit signed integer. Consumes 2 bytes.

    Returns number

    The read Int16.

    Throws

    If buffer does not contain at least 2 more bytes.

  • Reads a 32-bit signed integer. Consumes 4 bytes.

    Returns number

    The read Int32.

    Throws

    If buffer does not contain at least 4 more bytes.

  • Reads a 64-bit signed integer. Consumes 8 bytes.

    Returns bigint

    The read Int64.

    Throws

    If buffer does not contain at least 8 more bytes.

  • Reads an 8-bit signed integer. Consumes 1 byte.

    Returns number

    The read Int8.

    Throws

    If buffer does not contain at least 1 more byte.

  • Returns whether or not the offset is at the end of the file (i.e. reading anything will cause an exception).

    Returns boolean

    True if offset is at EOF, false otherwise.

    Deprecated

    Use isOutOfBounds getter instead.

  • Calls the given function n times, appending its result to a list after each iteration, and returning the resulting list.

    Type Parameters

    • T

    Parameters

    • n: number

      Number of iterations / length of resulting list.

    • fn: ((i) => T)

      Function to call on each iteration / to populate each list item.

        • (i): T
        • Parameters

          • i: number

          Returns T

    Returns T[]

    List containing the results of calling fn() n times.

    Throws

    If n is negative.

  • Returns the byte at the current offset, without advancing the offset.

    Returns number

    Byte at the current offset.

  • Saves the value of the current endianness, runs the given function, and then restores the endianness to the initial saved value.

    Type Parameters

    • T

    Parameters

    • fn: (() => T)

      Function to call.

        • (): T
        • Returns T

    Returns T

    Result of the given function call.

  • Saves the value of the current offset, runs the given function, and then restores the offset to the initial saved value.

    Type Parameters

    • T

    Parameters

    • fn: (() => T)

      Function to call.

        • (): T
        • Returns T

    Returns T

    Result of the given function call.

  • Preserves the original offset after calling the given function.

    Type Parameters

    • T

    Parameters

    • fn: (() => T)

      Function to call.

        • (): T
        • Returns T

    Returns T

    Deprecated

    Use saveOffset method instead.

  • Moves the offset to a specific byte.

    Parameters

    • offset: number

      The value to set the offset to.

    Returns void

  • Moves the offset to a specific byte.

    Parameters

    • offset: number | bigint

      The value to set the offset to.

    Returns void

    Deprecated

    Passing bigints is no longer supported.

  • Sets the endianness to use for reading/writing.

    Parameters

    Returns void

  • Advances the offset by the given number of bytes.

    Parameters

    • bytes: number

      The number of bytes to skip.

    Returns number

    The new offset after skipping bytes.

  • Advances the offset by the given number of bytes.

    Parameters

    • bytes: number | bigint

      The number of bytes to skip.

    Returns number

    The new offset after skipping bytes.

    Deprecated

    Passing bigints is no longer supported.

  • Slices a sub-buffer of the given size starting at the current offset.

    Parameters

    • size: number

      Size of the sub-buffer to slice.

    Returns Buffer

    The sliced buffer.

    Deprecated

    Use subarray instead.

    Throws

    If buffer does not contain the specified number of bytes.

  • Reads bytes until a null byte is found, parses them as a string with UTF-8 encoding, and returns the result. The offset will be advanced by the byte length + 1 (for the null byte).

    Returns string

    The string read with UTF-8 encoding.

    Deprecated

    Use terminatedString instead.

    Throws

    If there is no null byte between current offset and end of buffer.

  • Reads the given number of bytes into a subarray buffer.

    Parameters

    • size: number

      Number of bytes to read into subarray.

    Returns Buffer

    Buffer containing read bytes.

    Throws

    If buffer does not contain the specified number of bytes.

  • Returns the current offset.

    Returns number

    The current offset.

    Deprecated

    Use offset getter instead.

  • Reads bytes until a null byte is found, parses them as a string with the given encoding, and returns the result. The offset will be advanced by the byte length + 1 (for the null byte).

    Parameters

    • encoding: BufferEncoding = "utf-8"

      Character encoding to parse with ("utf-8" by default).

    Returns string

    String parsed with given encoding.

    Throws

    If there is no null byte between current offset and end of buffer.

  • Reads a 16-bit unsigned integer. Consumes 2 bytes.

    Returns number

    The read UInt16.

    Throws

    If buffer does not contain at least 2 more bytes.

  • Reads a 16-bit unsigned integer, applies the given bit masks, and returns the masked values in an array. Consumes 2 bytes.

    // flag is the first bit, value is the remaining 15
    const [flag, value] = decoder.uint16({ bits: [1, 15] });

    Parameters

    Returns number[]

    Array of values after applying bit masks to the read UInt16.

    Throws

    If buffer does not contain at least 2 more bytes, or if bit masks are not valid.

  • Reads a 32-bit unsigned integer. Consumes 4 bytes.

    Returns number

    The read UInt32.

    Throws

    If buffer does not contain at least 4 more bytes.

  • Reads a 32-bit unsigned integer, applies the given bit masks, and returns the masked values in an array. Consumes 4 bytes.

    // flag is the first bit, value is the remaining 31
    const [flag, value] = decoder.uint32({ bits: [1, 31] });

    Parameters

    Returns number[]

    Array of values after applying bit masks to the read UInt32.

    Throws

    If buffer does not contain at least 4 more bytes, or if bit masks are not valid.

  • Reads a 64-bit unsigned integer. Consumes 8 bytes.

    Returns bigint

    The read UInt64.

    Throws

    If buffer does not contain at least 8 more bytes.

  • Reads a 64-bit unsigned integer, applies the given bit masks, and returns the masked values in an array. Consumes 8 bytes.

    // flag is the first bit, value is the remaining 63
    const [flag, value] = decoder.uint64({ bits: [1, 63] });

    Parameters

    Returns bigint[]

    Array of values after applying bit masks to the read UInt64.

    Throws

    If buffer does not contain at least 8 more bytes, or if bit masks are not valid.

  • Reads an 8-bit unsigned integer. Consumes 1 byte.

    Returns number

    The read UInt8.

    Throws

    If buffer does not contain at least 1 more byte.

  • Reads an 8-bit unsigned integer, applies the given bit masks, and returns the masked values in an array. Consumes 1 byte.

    // flag is the first bit, value is the remaining 7
    const [flag, value] = decoder.uint8({ bits: [1, 7] });

    Parameters

    Returns number[]

    Array of values after applying bit masks to the read UInt8.

    Throws

    If buffer does not contain at least 1 more byte, or if bit masks are not valid.

  • Temporarily sets the endianness to the given value, calls the given function, and then restores the original endianness.

    Type Parameters

    • T

    Parameters

    • endianness: Endianness

      Endianness to use in scope of function.

    • fn: (() => T)

      Function to call.

        • (): T
        • Returns T

    Returns T

    Result of the given function call.

  • Temporarily sets the offset to the given value, calls the given function, and then restores the original offset.

    Type Parameters

    • T

    Parameters

    • offset: number

      Offset to use in scope of function.

    • fn: (() => T)

      Function to call.

        • (): T
        • Returns T

    Returns T

    Result of the given function call.