Skip to main content

BinaryBuffer

Classes

Utility for reading/writing binary data. Mostly useful for serialization/deserialization tasks. The buffer is dynamically resized, so you do not need to manage the size manually. It is useful to think of this structure as a "stream".

Constants

Minimum number of bytes to grow the buffer by when the buffer is full.

2^31-1, values above this will be cropped incorrectly when bit-shifting

BinaryBuffer

Utility for reading/writing binary data. Mostly useful for serialization/deserialization tasks. The buffer is dynamically resized, so you do not need to manage the size manually. It is useful to think of this structure as a "stream".

Kind: global class
Author: Alex Goldring
Copyright: Company Named Limited (c) 2025

new exports.BinaryBuffer()

Example

const buffer = new BinaryBuffer();

buffer.writeUTF8String("Hello World");

buffer.position = 0; // rewind to the beginning

const deserialized = buffer.readUTF8String(); // "Hello World"

binaryBuffer.endianness : EndianType | boolean

Default is little-endian as most platforms operate in little-endian The reason this is fixed is to ensure cross-platform compatibility as endianness in JavaScript is platform-dependent.

Kind: instance property of BinaryBuffer
See: https://en.wikipedia.org/wiki/Endianness

binaryBuffer.position : number

Current position in the buffer, where read and write operations will occur. Make sure to set this to the correct value before reading/writing data. Typically, this is set to 0 before reading/writing data.

Kind: instance property of BinaryBuffer

binaryBuffer.length

Deprecated

Kind: instance property of BinaryBuffer

binaryBuffer.length

Deprecated

Kind: instance property of BinaryBuffer

binaryBuffer.capacity : number

Managed by the buffer, do not modify directly

Kind: instance property of BinaryBuffer

binaryBuffer.raw_bytes ⇒ Uint8Array

Access raw underlying bytes attached to the buffer

Kind: instance property of BinaryBuffer

binaryBuffer.isBinaryBuffer : boolean

Kind: instance property of BinaryBuffer
Read only: true

binaryBuffer.fromArrayBuffer(data)

Sets capacity to the size of the input data. Sets position to 0.

Note: if you write to the buffer past the size of the input data ArrayBuffer - bound data will be re-allocated.

Kind: instance method of BinaryBuffer

ParamType
dataArrayBuffer

binaryBuffer.trim() ⇒ BinaryBuffer

Set capacity to contain data only up to the current position. This will re-allocate the data buffer if necessary.

Kind: instance method of BinaryBuffer

binaryBuffer.skip(byte_count)

Advance position(read/write cursor) a certain number of bytes forward.

Kind: instance method of BinaryBuffer

ParamType
byte_countnumber

binaryBuffer.setCapacity(capacity)

This will re-allocate data buffer if necessary. Note that all data is retained. Cannot shink past the current position.

Kind: instance method of BinaryBuffer
Throws:

  • Error if requested capacity is less than current position
ParamType
capacitynumber

binaryBuffer.ensureCapacity(min_capacity)

Kind: instance method of BinaryBuffer

ParamType
min_capacitynumber

binaryBuffer.readFloat16() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readFloat32() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readFloat64() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readInt8() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readInt16() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readInt32() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint8() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint16() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint16LE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint16BE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint24() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint24LE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint24BE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint32() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint32LE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint32BE() ⇒ number

Kind: instance method of BinaryBuffer

binaryBuffer.readUint8Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationUint8Array

binaryBuffer.readUint16Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationUint16Array

binaryBuffer.readUint32Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationUint32Array | Array.<number> | ArrayLike.<number>

binaryBuffer.readInt8Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationInt8Array

binaryBuffer.readInt16Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationInt16Array

binaryBuffer.readInt32Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationInt32Array

binaryBuffer.readFloat32Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationFloat32Array | Array.<number>

binaryBuffer.readFloat64Array(destination_offset, length, destination)

Kind: instance method of BinaryBuffer

ParamTypeDescription
destination_offsetnumberstarting index in the destination array
lengthnumbernumber of elements to read
destinationFloat64Array

binaryBuffer.writeFloat32Array(source_offset, length, source)

Kind: instance method of BinaryBuffer

ParamTypeDescription
source_offsetnumberstarting index in the source array
lengthnumbernumber of elements to read
sourceFloat32Array | Array.<number>

binaryBuffer.writeFloat16(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeFloat32(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeFloat64(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeInt8(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeInt16(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeInt32(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeInt8Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceInt8Array | Array.<number> | ArrayLike.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeInt16Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceInt16Array | Array.<number> | ArrayLike.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeInt32Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceInt32Array | Array.<number> | ArrayLike.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeUint8(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint8Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceUint8Array | Array.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeUint16(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint16BE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint16LE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint16Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceUint16Array | Array.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeUint24(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint24BE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint24LE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUintVar(value)

Write uint using a minimum number of bytes. Compact encoding scheme, if the value is 127 or less - only one byte will be used, if the value is 16383 or less - two bytes will be used, etc. NOTE: uses 7-bit encoding with 1 bit used for carry-over flag. NOTE: explicitly a little-endian format, endianness is ignored.

Kind: instance method of BinaryBuffer

ParamTypeDescription
valuenumbermust be an unsigned integer

binaryBuffer.readUintVar() ⇒ number

Read Uint of variable length, a compliment to #writeUintVar

Kind: instance method of BinaryBuffer

binaryBuffer.writeUint32(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint32BE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint32LE(value)

Kind: instance method of BinaryBuffer

ParamType
valuenumber

binaryBuffer.writeUint32Array(source, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
sourceUint32Array | Array.<number> | ArrayLike.<number>
source_offsetnumber
lengthnumber

binaryBuffer.writeBytes(array, source_offset, length)

Kind: instance method of BinaryBuffer

ParamType
arrayUint8Array | Uint8ClampedArray
source_offsetnumber
lengthnumber

binaryBuffer.readBytes(destination, destination_offset, length)

Kind: instance method of BinaryBuffer

ParamType
destinationUint8Array
destination_offsetnumber
lengthnumber

binaryBuffer.writeUTF8String(string)

Kind: instance method of BinaryBuffer

ParamType
stringstring

binaryBuffer.readUTF8String() ⇒ string

Kind: instance method of BinaryBuffer

binaryBuffer.writeASCIIString(string)

Write an ASCII (American Standard Code for Information Interchange) string. If the string contains characters that are not representable by ASCII, an error will be thrown. Note that ASCII only has 128 code points (characters), so this method is not suitable for representing UTF-8 strings. If the string is not ASCII representable - use writeUTF8String instead.

Kind: instance method of BinaryBuffer
See: https://en.wikipedia.org/wiki/ASCII

ParamType
stringstring

binaryBuffer.readASCIICharacters(length, [null_terminated]) ⇒ string

Read ASCII (American Standard Code for Information Interchange) characters to the buffer. Input is not validated, if the string contains non-ASCII characters, the result is unspecified.

Kind: instance method of BinaryBuffer
See: https://en.wikipedia.org/wiki/ASCII

ParamTypeDefaultDescription
lengthnumbermaximum number of characters to read. If null_terminated flag is on, resulting string might be shorter than length
[null_terminated]booleanfalseif true will stop reading when encountering 0 byte value character (NULL)

binaryBuffer.toString() ⇒ string

Represent the object as a string. Useful mainly for debugging.

Kind: instance method of BinaryBuffer

binaryBuffer.toHexString() ⇒ string

Useful for debugging, outputs contents of the buffer in hex format. Only includes data up to the .position

Kind: instance method of BinaryBuffer
Example

const b = new BinaryBuffer();
b.writeUint8(0xCA);
b.writeUint8(0xFE);
b.toHexString(); // "CAFE"

BinaryBuffer.fromEndianness(type) ⇒ BinaryBuffer

Kind: static method of BinaryBuffer

ParamType
typeEndianType

BinaryBuffer.fromArrayBuffer(v) ⇒ BinaryBuffer

Kind: static method of BinaryBuffer

ParamType
vArrayBuffer

BinaryBuffer.copyUTF8String(source, target) ⇒ string

Kind: static method of BinaryBuffer
Returns: string - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyUintVar(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyUint8(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyUint16(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyUint32(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyFloat32(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyFloat64(source, target) ⇒ number

Kind: static method of BinaryBuffer
Returns: number - Copied value

ParamType
sourceBinaryBuffer
targetBinaryBuffer

BinaryBuffer.copyBytes(source, target, length) ⇒ Uint8Array

Kind: static method of BinaryBuffer
Returns: Uint8Array - Copied data

ParamTypeDescription
sourceBinaryBuffer
targetBinaryBuffer
lengthnumbernumber of bytes to copy

MIN_GROWTH_STEP : number

Minimum number of bytes to grow the buffer by when the buffer is full.

Kind: global constant

MAX_SAFE_UINT_VAR : number

2^31-1, values above this will be cropped incorrectly when bit-shifting

Kind: global constant

DEFAULT_INITIAL_SIZE : number

Kind: global constant
Read only: true