@tgsnake/bytesio
    Preparing search index...

    Class BytesIO

    A class for reading and writing binary data using Node.js Buffers, similar to Python's BytesIO. Provides methods for seeking, slicing, reading, and writing various data types.

    This class is useful for manipulating binary data in memory, supporting random access and reading/writing of integers, floats, and raw buffers.

    const bio = BytesIO.from([1, 2, 3, 4]);
    bio.seek(0);
    const value = bio.readInt32LE();
    Index

    Constructors

    • Parameters

      • buffer: Buffer = ...

      Returns BytesIO

    Accessors

    • get buffer(): Buffer

      Gets the underlying Buffer.

      Returns Buffer

    • get length(): number

      Gets the length of the buffer in bytes.

      Returns number

    post

    • get post(): number

      Gets the current pointer position.

      Returns number

    Methods

    • Reads a specified number of bytes from the current pointer. Advances the pointer by the number of bytes read.

      Parameters

      • Optionallength: number

        The number of bytes to read. If undefined, reads to the end.

      Returns Buffer

      A Buffer containing the read bytes.

    • Reads a 64-bit signed integer (big-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns bigint

      The read bigint.

    • Reads a 64-bit signed integer (little-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns bigint

      The read bigint.

    • Reads a 64-bit unsigned integer (big-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns bigint

      The read bigint.

    • Reads a 64-bit unsigned integer (little-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns bigint

      The read bigint.

    • Reads a 64-bit floating point number (big-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns number

      The read double.

    • Reads a 64-bit floating point number (little-endian) from the current pointer. Advances the pointer by 8 bytes (or specified size).

      Parameters

      • size: number = 8

        Number of bytes to advance the pointer. Defaults to 8.

      Returns number

      The read double.

    • Reads a 32-bit floating point number (big-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read float.

    • Reads a 32-bit floating point number (little-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read float.

    • Reads a 32-bit signed integer (big-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read integer.

    • Reads a 32-bit signed integer (little-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read integer.

    • Reads a 32-bit unsigned integer (big-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read unsigned integer.

    • Reads a 32-bit unsigned integer (little-endian) from the current pointer. Advances the pointer by 4 bytes (or specified size).

      Parameters

      • size: number = 4

        Number of bytes to advance the pointer. Defaults to 4.

      Returns number

      The read unsigned integer.

    • Moves the internal pointer to a new position.

      Parameters

      • offset: number

        The offset to move the pointer.

      • whence: number = 0

        The reference point: 0 (start), 1 (current), 2 (end).

      Returns number

      The new pointer position.

      If the offset or whence is invalid.

    • Returns a new BytesIO instance containing a slice of the buffer.

      Parameters

      • ...args: any[]

        Arguments passed to Buffer.subarray.

      Returns BytesIO

      A new BytesIO instance with the sliced buffer.

    • Returns a JSON representation of the buffer.

      Returns { data: number[]; type: "Buffer" }

      The buffer as a JSON object.

    • Returns a string representation of the buffer.

      Parameters

      • ...args: any[]

        Arguments passed to Buffer.toString.

      Returns string

      The buffer as a string.

    • Appends data to the end of the buffer.

      Parameters

      • data: Buffer

        The Buffer to append.

      Returns BytesIO

      The BytesIO instance (for chaining).

    • Allocates a new BytesIO instance with a buffer of the given size.

      Parameters

      • size: number

        The size of the buffer to allocate.

      Returns BytesIO

      A new BytesIO instance.

    • Concatenates an array of Buffers into a new BytesIO instance.

      Parameters

      • data: Buffer<ArrayBufferLike>[]

        The array of Buffers to concatenate.

      Returns BytesIO

      A new BytesIO instance.

    • Creates a new BytesIO instance from the given input.

      Parameters

      • input: any

        The input data (Buffer, Array, string, etc.).

      • Optionalencode: any

        Optional encoding if input is a string.

      Returns BytesIO

      A new BytesIO instance.