Class: BufferBuilder

BufferBuilder

new BufferBuilder()

A builder of dynamically sized Buffers.

Properties:
Name Type Description
length Number A number of pushed bytes.
Source:
  • lib/BufferBuilder.js, line 20
Example
var builder = new BufferBuilder();

builder
  .pushByte(0x01)
  .pushUInt16(12)
  .pushString('Hello World!');

var buffer = builder.toBuffer();

console.log(buffer);

Methods

pushBits(bitsArray) → {BufferBuilder}

Appends the specified bits to this builder.

A number of bytes corresponding to the following formula will be appended to the builder:

var byteCount = Math.ceil(bitsArray.length / 8);

If the number of bits is not a multiple of 8, then the remaining bits will be set to 0.

Each 8 values from the array correspond to the 8 bits being appended to the buffer as bytes. First value of the each octet is the least significant bit, last value - the most significant bit.

Truthy values become 1's and falsy values become 0's.

For example, pushing the following array of 11 values: [0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1] will result in 2 bytes being appended to the builder: 0xE6, because its bit representation is 11100110 and 0x06, because its bit representation is 00000011.

Parameters:
Name Type Description
bitsArray Array.<Boolean> An array of truthy and falsy values.
Source:
  • lib/BufferBuilder.js, line 80
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified argument is not an array.
Type
Error
Example
builder.pushBits([0, 0, 0, 0, 1, 1, 0, 1, 0, 1])
builder.pushBits((0xABCD).toString(2).split('').map(Number))

pushBuffer(sourceBuffer) → {BufferBuilder}

Appends bytes from the specified source Buffer to this builder.

Increases the length of the builder by the specified source buffer.

Parameters:
Name Type Description
sourceBuffer Buffer
Source:
  • lib/BufferBuilder.js, line 237
Returns:
Type
BufferBuilder
Throws:
If the specified argument is not an instance of Buffer.
Type
Error
Example
builder.pushBuffer(new Buffer([0, 1, 2]));
builder.pushBuffer(new Buffer('Hello!'));

pushByte(byteValue) → {BufferBuilder}

Appends the specified byte to this builder.

Increases the length of the builder by 1.

Parameters:
Name Type Description
byteValue Number A number between 0 and 255.
Source:
  • lib/BufferBuilder.js, line 139
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified argument is not a number between 0 and 255.
Type
Error
Example
builder.pushByte(0xFE);

pushBytes(bytesArray) → {BufferBuilder}

Appends the specified bytes to this builder.

Increases the length of the builder by the length of the specified array.

Parameters:
Name Type Description
bytesArray Array.<Number> An array of numbers between 0 and 255.
Source:
  • lib/BufferBuilder.js, line 196
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified argument is not an array.
Type
Error
Example
builder.pushBytes([0x00, 0x01, 0xAB, 0xCD]);

pushChar(charValue) → {BufferBuilder}

Appends the specified ASCII character to this builder.

Increases the length of the builder by 1.

Parameters:
Name Type Description
charValue String An ASCII character.
Source:
  • lib/BufferBuilder.js, line 173
Returns:
Self.
Type
BufferBuilder
Throws:
  • If no char value was specified.
    Type
    ReferenceError
  • If the specified argument is not a string.
    Type
    TypeError
  • If the specified argument is not an ASCII character.
    Type
    Error
Example
builder.pushChar('!');

pushDouble(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as a signed 64 bit floating-point number defined in IEEE 754.

Increases the length of the builder by 8.

Parameters:
Name Type Description
numberValue Number A number between -1.7976931348623157e+308 and 1.7976931348623157e+308.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 546
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a double.
Type
Error
Example
builder.pushDouble(12345.6789);
builder.pushDouble(-12345.99999);

pushFloat(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as a signed 32 bit floating-point number defined in IEEE 754.

Increases the length of the builder by 4.

Parameters:
Name Type Description
numberValue Number A number between -3.4028234663852886e+38 and 3.4028234663852886e+38.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 512
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a float.
Type
Error
Example
builder.pushFloat(123.456);
builder.pushFloat(-123.456);

pushInt16(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as a signed 16-bit integer to this builder.

Increases the length of the builder by 2.

Parameters:
Name Type Description
numberValue Number A number between -32768 and 32767.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 359
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a 16-bit signed integer.
Type
Error
Example
builder.pushInt16(12345);
builder.pushInt16(-12345, true);

pushInt32(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as a signed 32-bit integer to this builder.

Increases the length of the builder by 4.

Parameters:
Name Type Description
numberValue Number A number between -2147483648 and 2147483647.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 390
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a 32-bit signed integer.
Type
Error
Example
builder.pushInt32(-123456789);
builder.pushInt32(123456789, true);

pushInt8(numberValue) → {BufferBuilder}

Appends the specified number as a signed 8-bit integer to this builder.

Increases the length of the builder by 1.

Parameters:
Name Type Description
numberValue Number A number between -128 and 127.
Source:
  • lib/BufferBuilder.js, line 330
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not an 8-bit signed integer.
Type
Error
Example
builder.pushInt8(-100);
builder.pushInt8(10, true);

pushString(stringValue, encoding) → {BufferBuilder}

Appends the specified string in the specified encoding to this builder.

Increases the length of the builder by the byte length of the specified string. Byte length is calculated using Buffer.byteLength() function.

Parameters:
Name Type Description
stringValue String A string value in the specified encoding.
encoding String=utf8 An encoding of the specified string value. Defaults to `utf8`.
Source:
  • lib/BufferBuilder.js, line 273
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a string.
Type
Error
Example
builder.pushString('Hęłłó!');
builder.pushString('Hello!', 'ascii');

pushUInt16(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as an unsigned 16-bit integer to this builder.

Increases the length of the builder by 2.

Parameters:
Name Type Description
numberValue Number A number between 0 and 65535.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 449
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a 16-bit unsigned integer.
Type
Error
Example
builder.pushUInt16(256);
builder.pushUInt16(1, true);

pushUInt32(numberValue, littleEndian) → {BufferBuilder}

Appends the specified number as an unsigned 32-bit integer to this builder.

Increases the length of the builder by 4.

Parameters:
Name Type Description
numberValue Number A number between 0 and 4294967295.
littleEndian Boolean=false TRUE for little endian byte order; FALSE for big endian. Defaults to FALSE.
Source:
  • lib/BufferBuilder.js, line 480
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a 32-bit unsigned integer.
Type
Error
Example
builder.pushUInt32(4000111222);
builder.pushUInt32(4000111222, true);

pushUInt8(numberValue) → {BufferBuilder}

Appends the specified number as an unsigned 8-bit integer to this builder.

Increases the length of the builder by 1.

Parameters:
Name Type Description
numberValue Number A number between 0 and 255.
Source:
  • lib/BufferBuilder.js, line 420
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not an 8-bit unsigned integer.
Type
Error
Example
builder.pushUInt8(255);
builder.pushUInt8(66, true);

pushZeroString(stringValue, encoding) → {BufferBuilder}

Appends the specified string followed by NULL character (\0) to this builder.

Increases the length of the builder by the byte length of the specified string value plus 1. Byte length is calculated using Buffer.byteLength() function.

Parameters:
Name Type Description
stringValue String A string value in the specified encoding.
encoding String=utf8 An encoding of the specified string value. Defaults to `utf8`.
Source:
  • lib/BufferBuilder.js, line 313
Returns:
Self.
Type
BufferBuilder
Throws:
If the specified value is not a string.
Type
Error
Example
builder.pushZeroString('Hęłłó!');
builder.pushZeroString('Hello!', 'ascii');

toBuffer() → {Buffer}

Returns a new Buffer with all data pushed to this builder.

The new Buffer will have the same length as the builder.

Source:
  • lib/BufferBuilder.js, line 35
Returns:
An instance of Buffer filled with all bytes pushed to this builder.
Type
Buffer
Example
var buffer = builder.toBuffer();