Options
All
  • Public
  • Public/Protected
  • All
Menu

Class IOBuffer

Hierarchy

  • IOBuffer

Index

Constructors

  • new IOBuffer(data?: InputData, options?: IOBufferOptions): IOBuffer
  • Parameters

    • data: InputData = defaultByteLength

      The data to construct the IOBuffer with. If data is a number, it will be the new buffer's length
      If data is undefined, the buffer will be initialized with a default length of 8Kb
      If data is an ArrayBuffer, SharedArrayBuffer, an ArrayBufferView (Typed Array), an IOBuffer instance, or a Node.js Buffer, a view will be created over the underlying ArrayBuffer.

    • options: IOBufferOptions = {}

    Returns IOBuffer

Properties

buffer: ArrayBufferLike

Reference to the internal ArrayBuffer object.

byteLength: number

Byte length of the internal ArrayBuffer.

byteOffset: number

Byte offset of the internal ArrayBuffer.

length: number

Byte length of the internal ArrayBuffer.

offset: number

The current offset of the buffer's pointer.

Methods

  • available(byteLength?: number): boolean
  • Checks if the memory allocated to the buffer is sufficient to store more bytes after the offset.

    Parameters

    • byteLength: number = 1

      The needed memory in bytes.

    Returns boolean

    true if there is sufficient space and false otherwise.

  • Move the pointer n bytes backward.

    Parameters

    • n: number = 1

      Number of bytes to move back.

    Returns IOBuffer

  • decodeText(n?: number, encoding?: string): string
  • Read the next n bytes, return a string decoded with encoding and move pointer forward by n bytes. If no encoding is passed, the function is equivalent to @see {@link IOBuffer#readUtf8}

    Parameters

    • n: number = 1
    • encoding: string = 'utf-8'

    Returns string

  • ensureAvailable(byteLength?: number): IOBuffer
  • Make sure the buffer has sufficient memory to write a given byteLength at the current pointer offset. If the buffer's memory is insufficient, this method will create a new buffer (a copy) with a length that is twice (byteLength + current offset).

    Parameters

    • byteLength: number = 1

    Returns IOBuffer

  • isBigEndian(): boolean
  • Check if big-endian mode is used for reading and writing multi-byte values.

    Returns boolean

    true if big-endian mode is used, false otherwise.

  • isLittleEndian(): boolean
  • Check if little-endian mode is used for reading and writing multi-byte values.

    Returns boolean

    true if little-endian mode is used, false otherwise.

  • Pop the last pointer offset from the mark stack, and set the current pointer offset to the popped value.

    see

    {@link IOBuffer#pushMark}

    Returns IOBuffer

  • Push the current pointer offset to the mark stack.

    see

    {@link IOBuffer#popMark}

    Returns IOBuffer

  • readArray<T>(size: number, type: T): InstanceType<{ float32: Float32ArrayConstructor; float64: Float64ArrayConstructor; int16: Int16ArrayConstructor; int32: Int32ArrayConstructor; int64: BigInt64ArrayConstructor; int8: Int8ArrayConstructor; uint16: Uint16ArrayConstructor; uint32: Uint32ArrayConstructor; uint64: BigUint64ArrayConstructor; uint8: Uint8ArrayConstructor }[T]>
  • Creates an array of corresponding to the type type and size size. For example type uint8 will create a Uint8Array.

    Type parameters

    • T: "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32" | "uint64" | "int64" | "float32" | "float64"

    Parameters

    • size: number

      size of the resulting array

    • type: T

      number type of elements to read

    Returns InstanceType<{ float32: Float32ArrayConstructor; float64: Float64ArrayConstructor; int16: Int16ArrayConstructor; int32: Int32ArrayConstructor; int64: BigInt64ArrayConstructor; int8: Int8ArrayConstructor; uint16: Uint16ArrayConstructor; uint32: Uint32ArrayConstructor; uint64: BigUint64ArrayConstructor; uint8: Uint8ArrayConstructor }[T]>

  • readBigInt64(): bigint
  • Read a 64-bit signed integer number and move pointer forward by 8 bytes.

    Returns bigint

  • readBigUint64(): bigint
  • Read a 64-bit unsigned integer number and move pointer forward by 8 bytes.

    Returns bigint

  • readBoolean(): boolean
  • Read a byte and return false if the byte's value is 0, or true otherwise. Moves pointer forward by one byte.

    Returns boolean

  • readByte(): number
  • Alias for {@link IOBuffer#readUint8}.

    Returns number

  • readBytes(n?: number): Uint8Array
  • Read n bytes and move pointer forward by n bytes.

    Parameters

    • n: number = 1

    Returns Uint8Array

  • readChar(): string
  • Read a 1-byte ASCII character and move pointer forward by 1 byte.

    Returns string

  • readChars(n?: number): string
  • Read n 1-byte ASCII characters and move pointer forward by n bytes.

    Parameters

    • n: number = 1

    Returns string

  • readFloat32(): number
  • Read a 32-bit floating number and move pointer forward by 4 bytes.

    Returns number

  • readFloat64(): number
  • Read a 64-bit floating number and move pointer forward by 8 bytes.

    Returns number

  • readInt16(): number
  • Read a 16-bit signed integer and move pointer forward by 2 bytes.

    Returns number

  • readInt32(): number
  • Read a 32-bit signed integer and move pointer forward by 4 bytes.

    Returns number

  • readInt8(): number
  • Read a signed 8-bit integer and move pointer forward by 1 byte.

    Returns number

  • readUint16(): number
  • Read a 16-bit unsigned integer and move pointer forward by 2 bytes.

    Returns number

  • readUint32(): number
  • Read a 32-bit unsigned integer and move pointer forward by 4 bytes.

    Returns number

  • readUint8(): number
  • Read an unsigned 8-bit integer and move pointer forward by 1 byte.

    Returns number

  • readUtf8(n?: number): string
  • Read the next n bytes, return a UTF-8 decoded string and move pointer forward by n bytes.

    Parameters

    • n: number = 1

    Returns string

  • Move the pointer back to the last pointer offset set by mark.

    see

    {@link IOBuffer#mark}

    Returns IOBuffer

  • Move the pointer n bytes forward.

    Parameters

    • n: number = 1

      Number of bytes to skip.

    Returns IOBuffer

  • toArray(): Uint8Array
  • Export a Uint8Array view of the internal buffer. The view starts at the byte offset and its length is calculated to stop at the last written byte or the original length.

    Returns Uint8Array

  • Write value as a 64-bit signed bigint and move pointer forward by 8 bytes.

    Parameters

    • value: bigint

    Returns IOBuffer

  • Write value as a 64-bit unsigned bigint and move pointer forward by 8 bytes.

    Parameters

    • value: bigint

    Returns IOBuffer

  • Write 0xff if the passed value is truthy, 0x00 otherwise and move pointer forward by 1 byte.

    Parameters

    • value: unknown

    Returns IOBuffer

  • writeBytes(bytes: ArrayLike<number>): IOBuffer
  • Write all elements of bytes as uint8 values and move pointer forward by bytes.length bytes.

    Parameters

    • bytes: ArrayLike<number>

    Returns IOBuffer

  • Write the charCode of str's first character as an 8-bit unsigned integer and move pointer forward by 1 byte.

    Parameters

    • str: string

    Returns IOBuffer

  • Write the charCodes of all str's characters as 8-bit unsigned integers and move pointer forward by str.length bytes.

    Parameters

    • str: string

    Returns IOBuffer

  • Write value as a 32-bit floating number and move pointer forward by 4 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as a 64-bit floating number and move pointer forward by 8 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as a 16-bit signed integer and move pointer forward by 2 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as a 32-bit signed integer and move pointer forward by 4 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as an 8-bit signed integer and move pointer forward by 1 byte.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as a 16-bit unsigned integer and move pointer forward by 2 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as a 32-bit unsigned integer and move pointer forward by 4 bytes.

    Parameters

    • value: number

    Returns IOBuffer

  • Write value as an 8-bit unsigned integer and move pointer forward by 1 byte.

    Parameters

    • value: number

    Returns IOBuffer

  • UTF-8 encode and write str to the current pointer offset and move pointer forward according to the encoded length.

    Parameters

    • str: string

    Returns IOBuffer