Class BinaryEncoder

Encodes binary data to a buffer.

Hierarchy

  • default
    • BinaryEncoder

Constructors

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

  • Creates a new buffer with the given size and returns a BinaryEncoder for it.

    Parameters

    • size: number

      Size of buffer to encode.

    • initialOffset: number = 0

      Initial offset to use (0 by default).

    • endianness: Endianness = "LE"

      Endianness to use (little endian by default).

    Returns BinaryEncoder

    Throws

    If size is not a non-negative integer.

  • Creates a BinaryEncoder whose buffer size will dynamically grow to fit all of the data that is written in the given function. When the function ends, the buffer will be cropped so that it contains the exact amount of bytes required.

    Notes

    • New buffers may be generated multiple times; references to them are unreliable.
    • When performance is critical, it is recommended to use alloc or constructor with a pre-calculated size instead.

    Parameters

    Returns BinaryEncoder

    Encoder created with dynamic size.

  • Creates a BinaryEncoder whose buffer size will dynamically grow to fit all of the data that is written in the given function. When the function ends, the buffer will be cropped so that it contains the exact amount of bytes required.

    Notes

    • New buffers may be generated multiple times; references to them are unreliable.
    • When performance is critical, it is recommended to use alloc or constructor with a pre-calculated size instead.

    Parameters

    Returns BinaryEncoder

    Encoder created with dynamic size.

    Throws

    If minimumSize is not a non-negative integer or chunkSize is not a positive integer.

  • Writes a UInt8 value of 1 if the given boolean is true, or 0 if false.

    Parameters

    • value: boolean

      Boolean value to write as a UInt8.

    Returns void

    Throws

    If buffer cannot fit 1 more byte.

  • Writes a single numerical value as a byte (alias for uint8).

    Parameters

    • value: number

      Single byte value to write.

    Returns void

    Throws

    If buffer cannot fit 1 more byte.

  • Writes an array of bytes to the buffer.

    Parameters

    • values: number[] | Uint8Array

      Array of bytes to write.

    Returns void

    Throws

    If buffer cannot fit number of specified byte(s).

  • Writes a string to the buffer using the given encoding.

    Parameters

    • value: string

      The characters to write.

    • encoding: BufferEncoding = "utf-8"

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

    Returns void

    Throws

    If buffer cannot fit number of specified byte(s).

  • Writes a string to the buffer using Base64 encoding.

    Parameters

    • value: string

      The characters to write.

    Returns void

    Deprecated

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

    Throws

    If buffer cannot fit number of specified byte(s).

  • Writes a string to the buffer using UTF-8 encoding.

    Parameters

    • value: string

      The characters to write.

    Returns void

    Deprecated

    Use chars instead.

    Throws

    If buffer cannot fit number of specified byte(s).

  • Writes a double. Consumes 8 bytes.

    Parameters

    • value: number

      The double to write.

    Returns void

    Throws

    If buffer cannot fit 8 more bytes.

  • Checks whether this encoder can safely consume the given number of bytes from the given offset (or the current offset if omitted), and if not, increases the size of the buffer by just enough to do so.

    Notes

    • A new buffer may be generated; references to it will be unreliable.
    • When performance is critical, you should precalculate the entire size of the buffer at time of initialization.

    Parameters

    • bytes: number

      Number of bytes to check.

    • Optional fromOffset: number

      Offset to check from, if different than current one.

    Returns number

    Number of bytes that were added, if any.

    Throws

    If bytes or offset is negative (cannot add bytes to start).

  • Writes a float. Consumes 4 bytes.

    Parameters

    • value: number

      The float to write.

    Returns void

    Throws

    If buffer cannot fit 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.

  • Increases the size of the buffer by the given number of bytes.

    Notes

    • A new buffer will be generated; references to it will be broken.
    • When performance is critical, you should precalculate the entire size of the buffer at time of initialization.

    Parameters

    • bytes: number

      Bytes to increase buffer size by.

    Returns void

    Throws

    If bytes is not a non-negative integer.

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

    Parameters

    • value: number

      The Int16 to write.

    Returns void

    Throws

    If buffer cannot fit 2 more bytes.

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

    Parameters

    • value: number

      The Int32 to write.

    Returns void

    Throws

    If buffer cannot fit 4 more bytes.

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

    Parameters

    • value: number | bigint

      The Int64 to write.

    Returns void

    Throws

    If buffer cannot fit 8 more bytes.

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

    Parameters

    • value: number

      The Int8 to write.

    Returns void

    Throws

    If buffer cannot fit 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.

  • Writes the given number of null bytes. If omitted, only one is written.

    Parameters

    • bytes: number = 1

      Number of null bytes to write (1 by default).

    Returns void

    Throws

    If buffer cannot fit number of specified byte(s).

  • 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.

  • Returns the current offset.

    Returns number

    The current offset.

    Deprecated

    Use offset getter instead.

  • Writes a string to the buffer using the given encoding, then writes a null byte immediately after to terminate it.

    Parameters

    • value: string

      The string to write.

    • encoding: BufferEncoding = "utf-8"

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

    Returns void

    Throws

    If buffer cannot fit number of specified byte(s).

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

    Parameters

    • value: number

      The UInt16 to write.

    Returns void

    Throws

    If buffer cannot fit 2 more bytes.

  • Combines the given values using the given bit masks and writes them as a single 16-bit unsigned integer. Consumes 2 bytes.

    // writing a flag and value
    encoder.uint16({ bits: [1, 15], values: [1, 50] });

    Parameters

    Returns void

    Throws

    If buffer cannot fit 2 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.

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

    Parameters

    • value: number

      The UInt32 to write.

    Returns void

    Throws

    If buffer cannot fit 4 more bytes.

  • Combines the given values using the given bit masks and writes them as a single 32-bit unsigned integer. Consumes 4 bytes.

    // writing a flag and value
    encoder.uint32({ bits: [1, 31], values: [1, 50] });

    Parameters

    Returns void

    Throws

    If buffer cannot fit 4 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.

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

    Parameters

    • value: number | bigint

      The UInt64 to write.

    Returns void

    Throws

    If buffer cannot fit 8 more bytes.

  • Combines the given values using the given bit masks and writes them as a single 64-bit unsigned integer. Consumes 8 bytes.

    // writing a flag and value (mixing numbers and bigints is fine)
    encoder.uint64({ bits: [1, 63], values: [1, 50n] });

    Parameters

    • maskings: EncoderBitMaskings<number | bigint>

      Values and the bit masks to use when combining them.

    Returns void

    Throws

    If buffer cannot fit 8 more bytes, if bit masks are not valid, or if values cannot fit into bit masks.

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

    Parameters

    • value: number

      The UInt8 to write.

    Returns void

    Throws

    If buffer cannot fit 1 more byte.

  • Combines the given values using the given bit masks and writes them as a single 8-bit unsigned integer. Consumes 1 byte.

    // writing a flag and value
    encoder.uint8({ bits: [1, 7], values: [1, 50] });

    Parameters

    Returns void

    Throws

    If buffer cannot fit 1 more byte, if bit masks are not valid, or if values cannot fit into bit masks.

  • Runs a function in which the encoder's size can dynamically grow to fit the data written. When the function ends, any unused bytes that did not exist before calling this function will be pruned.

    Notes

    • New buffers may be generated multiple times; references to them are unreliable.
    • When performance is critical, you should precalculate the entire size of the buffer at time of initialization.

    Parameters

    • fn: (() => void)

      Function to run in a dynamic sizing context.

        • (): void
        • Returns void

    Returns void

  • Runs a function in which the encoder's size can dynamically grow to fit the data written. When the function ends, any unused bytes that did not exist before calling this function will be pruned.

    Notes

    • New buffers may be generated multiple times; references to them are unreliable.
    • When performance is critical, you should precalculate the entire size of the buffer at time of initialization.

    Parameters

    • chunkSize: number

      Number of bytes to add on overflow (256 by default).

    • fn: (() => void)

      Function to run in a dynamic sizing context.

        • (): void
        • Returns void

    Returns void

    Throws

    If chunkSize is not a positive integer.

  • 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.