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
- MIN_GROWTH_STEP :
number
Minimum number of bytes to grow the buffer by when the buffer is full.
- MAX_SAFE_UINT_VAR :
number
2^31-1, values above this will be cropped incorrectly when bit-shifting
- DEFAULT_INITIAL_SIZE :
number
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
- BinaryBuffer
- new exports.BinaryBuffer()
- instance
- .endianness :
EndianType|boolean - .position :
number .length.length- .capacity :
number - .raw_bytes ⇒
Uint8Array - .isBinaryBuffer :
boolean - .fromArrayBuffer(data)
- .trim() ⇒
BinaryBuffer - .skip(byte_count)
- .setCapacity(capacity)
- .ensureCapacity(min_capacity)
- .readFloat16() ⇒
number - .readFloat32() ⇒
number - .readFloat64() ⇒
number - .readInt8() ⇒
number - .readInt16() ⇒
number - .readInt32() ⇒
number - .readUint8() ⇒
number - .readUint16() ⇒
number - .readUint16LE() ⇒
number - .readUint16BE() ⇒
number - .readUint24() ⇒
number - .readUint24LE() ⇒
number - .readUint24BE() ⇒
number - .readUint32() ⇒
number - .readUint32LE() ⇒
number - .readUint32BE() ⇒
number - .readUint8Array(destination_offset, length, destination)
- .readUint16Array(destination_offset, length, destination)
- .readUint32Array(destination_offset, length, destination)
- .readInt8Array(destination_offset, length, destination)
- .readInt16Array(destination_offset, length, destination)
- .readInt32Array(destination_offset, length, destination)
- .readFloat32Array(destination_offset, length, destination)
- .readFloat64Array(destination_offset, length, destination)
- .writeFloat32Array(source_offset, length, source)
- .writeFloat16(value)
- .writeFloat32(value)
- .writeFloat64(value)
- .writeInt8(value)
- .writeInt16(value)
- .writeInt32(value)
- .writeInt8Array(source, source_offset, length)
- .writeInt16Array(source, source_offset, length)
- .writeInt32Array(source, source_offset, length)
- .writeUint8(value)
- .writeUint8Array(source, source_offset, length)
- .writeUint16(value)
- .writeUint16BE(value)
- .writeUint16LE(value)
- .writeUint16Array(source, source_offset, length)
- .writeUint24(value)
- .writeUint24BE(value)
- .writeUint24LE(value)
- .writeUintVar(value)
- .readUintVar() ⇒
number - .writeUint32(value)
- .writeUint32BE(value)
- .writeUint32LE(value)
- .writeUint32Array(source, source_offset, length)
- .writeBytes(array, source_offset, length)
- .readBytes(destination, destination_offset, length)
- .writeUTF8String(string)
- .readUTF8String() ⇒
string - .writeASCIIString(string)
- .readASCIICharacters(length, [null_terminated]) ⇒
string - .toString() ⇒
string - .toHexString() ⇒
string
- .endianness :
- static
- .fromEndianness(type) ⇒
BinaryBuffer - .fromArrayBuffer(v) ⇒
BinaryBuffer - .copyUTF8String(source, target) ⇒
string - .copyUintVar(source, target) ⇒
number - .copyUint8(source, target) ⇒
number - .copyUint16(source, target) ⇒
number - .copyUint32(source, target) ⇒
number - .copyFloat32(source, target) ⇒
number - .copyFloat64(source, target) ⇒
number - .copyBytes(source, target, length) ⇒
Uint8Array
- .fromEndianness(type) ⇒
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
| Param | Type |
|---|---|
| data | ArrayBuffer |
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
| Param | Type |
|---|---|
| byte_count | number |
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:
Errorif requested capacity is less than currentposition
| Param | Type |
|---|---|
| capacity | number |
binaryBuffer.ensureCapacity(min_capacity)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| min_capacity | number |
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
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Uint8Array |
binaryBuffer.readUint16Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Uint16Array |
binaryBuffer.readUint32Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Uint32Array | Array.<number> | ArrayLike.<number> |
binaryBuffer.readInt8Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Int8Array |
binaryBuffer.readInt16Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Int16Array |
binaryBuffer.readInt32Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Int32Array |
binaryBuffer.readFloat32Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Float32Array | Array.<number> |
binaryBuffer.readFloat64Array(destination_offset, length, destination)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| destination_offset | number | starting index in the destination array |
| length | number | number of elements to read |
| destination | Float64Array |
binaryBuffer.writeFloat32Array(source_offset, length, source)
Kind: instance method of BinaryBuffer
| Param | Type | Description |
|---|---|---|
| source_offset | number | starting index in the source array |
| length | number | number of elements to read |
| source | Float32Array | Array.<number> |
binaryBuffer.writeFloat16(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeFloat32(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeFloat64(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeInt8(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeInt16(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeInt32(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeInt8Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Int8Array | Array.<number> | ArrayLike.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeInt16Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Int16Array | Array.<number> | ArrayLike.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeInt32Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Int32Array | Array.<number> | ArrayLike.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeUint8(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint8Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Uint8Array | Array.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeUint16(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint16BE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint16LE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint16Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Uint16Array | Array.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeUint24(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint24BE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint24LE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
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
| Param | Type | Description |
|---|---|---|
| value | number | must 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
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint32BE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint32LE(value)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| value | number |
binaryBuffer.writeUint32Array(source, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| source | Uint32Array | Array.<number> | ArrayLike.<number> |
| source_offset | number |
| length | number |
binaryBuffer.writeBytes(array, source_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| array | Uint8Array | Uint8ClampedArray |
| source_offset | number |
| length | number |
binaryBuffer.readBytes(destination, destination_offset, length)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| destination | Uint8Array |
| destination_offset | number |
| length | number |
binaryBuffer.writeUTF8String(string)
Kind: instance method of BinaryBuffer
| Param | Type |
|---|---|
| string | string |
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
| Param | Type |
|---|---|
| string | string |
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
| Param | Type | Default | Description |
|---|---|---|---|
| length | number | maximum number of characters to read. If null_terminated flag is on, resulting string might be shorter than length | |
| [null_terminated] | boolean | false | if 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
| Param | Type |
|---|---|
| type | EndianType |
BinaryBuffer.fromArrayBuffer(v) ⇒ BinaryBuffer
Kind: static method of BinaryBuffer
| Param | Type |
|---|---|
| v | ArrayBuffer |
BinaryBuffer.copyUTF8String(source, target) ⇒ string
Kind: static method of BinaryBuffer
Returns: string - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyUintVar(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyUint8(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyUint16(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyUint32(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyFloat32(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyFloat64(source, target) ⇒ number
Kind: static method of BinaryBuffer
Returns: number - Copied value
| Param | Type |
|---|---|
| source | BinaryBuffer |
| target | BinaryBuffer |
BinaryBuffer.copyBytes(source, target, length) ⇒ Uint8Array
Kind: static method of BinaryBuffer
Returns: Uint8Array - Copied data
| Param | Type | Description |
|---|---|---|
| source | BinaryBuffer | |
| target | BinaryBuffer | |
| length | number | number 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