Class: BufferQueueReader

BufferQueueReader

new BufferQueueReader(buffer1, buffer2, bufferN)

A class providing extended functionality for reading lists/streams of Buffer instances.

Parameters:
Name Type Description
buffer1 Buffer An optional buffer to push.
buffer2 Buffer An optional buffer to push.
bufferN Buffer An optional buffer to push.
Properties:
Name Type Description
length Number The remaining length of the reader.
Source:
  • lib/BufferQueueReader.js, line 31
Throws:
If any of the specified buffers aren't instances of `Buffer`.
Type
Error
Example
var reader = new BufferQueueReader(new Buffer(3), new Buffer(8));

reader.push(new Buffer(10));
reader.push(new Buffer(5));
reader.push(new Buffer(16));

console.log('int16=', reader.shiftInt16());
console.log('uint32=', reader.shiftUInt32());
console.log('bits=', reader.readBits(0, 12));

reader.skip(2);

console.log('double=', reader.shiftDouble());

Methods

copy(targetBuffer, targetStart, sourceStart, sourceEnd) → {Number}

Copies bytes from the reader to the specified target buffer.

Parameters:
Name Type Description
targetBuffer Buffer A buffer to copy to.
targetStart Number=0 A position at which writing to the buffer should begin. Defaults to 0 (the beginning).
sourceStart Number=0 A position from which writing from the reader should begin. Defaults to 0 (the beginning).
sourceEnd Number=this.length A position at which writing from the reader should end. Defaults to the end (the reader's length).
Source:
  • lib/BufferQueueReader.js, line 193
Returns:
A number of bytes written.
Type
Number
Throws:
  • If the specified target buffer is not an instance of Buffer.
    Type
    Error
  • If the specified target start index is not a number between 0 and the target buffer's length.
    Type
    Error
  • If the specified source start index is not a number between 0 and the reader's length (exclusive).
    Type
    Error
  • If the specified source end index is not a number between 0 (exclusive) and the reader's length.
    Type
    Error
  • If the specified source start index is greater than or equal to the source end index.
    Type
    Error
Example
var buffer = new Buffer(10);

reader.copy(buffer, 5, 0, 5);

indexOf(searchElement, fromIndex) → {Number}

Returns a position of the next occurence of the specified byte after the specified starting index.

Parameters:
Name Type Description
searchElement Number A byte value to search for.
fromIndex Number=0 A starting index. Defaults to 0 (the beginning).
Source:
  • lib/BufferQueueReader.js, line 127
Returns:
A position of the found element (starting at 0) or -1 if the search element was not found.
Type
Number
Throws:
  • If the search element is not a number between 0x00 and 0xFF.
    Type
    Error
  • If the starting index is not a number between 0 and the reader's length.
    Type
    Error
Example
var index = reader.indexOf(0xFF, 20);

push(buffer1, buffer2, bufferN)

Adds the specified buffers to the reader.

Empty buffers are ignored.

Parameters:
Name Type Description
buffer1 Buffer A buffer to push.
buffer2 Buffer An optional buffer to push.
bufferN Buffer An optional buffer to push.
Source:
  • lib/BufferQueueReader.js, line 55
Throws:
If any of the specified buffers aren't an instance of `Buffer`.
Type
Error
Example
reader.push(new Buffer('Hello'), new Buffer(' '), new Buffer('World!'));

readBits(offset, count) → {Array.}

Returns an array of bits (boolean values) starting at the specified offset.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 1.
count Number A number of bits to read. Must be between 1 and the reader's length multiplied by 8 minus the starting index.
Source:
  • lib/BufferQueueReader.js, line 574
Returns:
An array of bits.
Type
Array.<Boolean>
Throws:
If the specified count is not a number between 1 and the reader's length multiplied by 8 minus the starting index.
Type
Error
Example
var bitsArray = reader.readBits(5, 13);

readBuffer(offset, count) → {Buffer}

Returns the specified number of bytes as an instance of Buffer starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus the specified count.
count Number A number of bytes to read. Must be between 1 and the reader's length minus the offset.
Source:
  • lib/BufferQueueReader.js, line 692
Returns:
A Buffer of bytes.
Type
Buffer
Throws:
  • If the specified offset exceeds the reader's boundries.
    Type
    Error
  • If the specified count is not a number between 1 and the reader's length minus the offset.
    Type
    Error
Example
var buffer = reader.readBuffer(5, 10);

readByte(offset) → {Number}

Returns a byte at the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 1.
Source:
  • lib/BufferQueueReader.js, line 588
Returns:
A number between 0x00 and 0xFF.
Type
Number
Throws:
If the reader is empty.
Type
Error
Example
var byteValue = reader.readByte(1);

readBytes(offset, count) → {Array.}

Returns the specified number of bytes starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus the specified count.
count Number A number of bytes to read. Must be between 1 and the reader's length minus the offset.
Source:
  • lib/BufferQueueReader.js, line 635
Returns:
An array of bytes.
Type
Array.<Number>
Throws:
  • If the specified offset exceeds the reader's boundries.
    Type
    Error
  • If the specified count is not a number between 1 and the reader's length minus the offset.
    Type
    Error
Example
var bytesArray = reader.readBytes(0, 6);

readChar(offset) → {String}

Returns an ASCII character at the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 1.
Source:
  • lib/BufferQueueReader.js, line 619
Returns:
An ASCII character.
Type
String
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var charValue = reader.readChar(4);

readDouble(offset, littleEndian) → {Number}

Returns a signed 64 bit floating-point number as defined in IEEE 754.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 8.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 912
Returns:
A 64 bit floating-point number.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var doubleBE = reader.readDouble(0);
var doubleLE = reader.readDouble(8, true);

readFloat(offset, littleEndian) → {Number}

Returns a signed 32 bit floating-point number as defined in IEEE 754.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 4.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 896
Returns:
A 32 bit floating-point number.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var floatBE = reader.readFloat(0, );
var floatLE = reader.readFloat(4, true);

readInt16(offset, littleEndian) → {Number}

Returns a signed 16 bit integer starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 2.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 818
Returns:
A number between -32768 and 32767.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var int16BE = reader.readInt16(0);
var int16LE = reader.readInt16(2, true);

readInt32(offset, littleEndian) → {Number}

Returns a signed 32 bit integer starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 4.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 834
Returns:
A number between -2147483648 and 2147483647.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var int32BE = reader.readInt32(0);
var int32LE = reader.readInt32(4, true);

readInt8(offset) → {Number}

Returns a signed 8 bit integer at the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 1.
Source:
  • lib/BufferQueueReader.js, line 802
Returns:
A number between -128 and 127.
Type
Number
Throws:
  • If the specified offset exceeds the reader's boundries.
    Type
    Error
  • If the reader is empty.
    Type
    Error
Example
var int8 = reader.readInt8(5);

readString(offset, length, encoding) → {String}

Returns the specified number of bytes as a string with the specified encoding.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus the specified length.
length Number A number of bytes to read. Must be between 1 and the reader's length minus the offset.
encoding String=utf8 An encoding of the string. Defaults to `utf8`.
Source:
  • lib/BufferQueueReader.js, line 763
Returns:
A string of the specified length.
Type
String
Throws:
  • If the specified offset exceeds the reader's boundries.
    Type
    Error
  • If the specified length is not a number between 1 and the reader's length.
    Type
    Error
  • If the specified encoding is not supported.
    Type
    Error
Example
var stringValue = reader.readString(1, 12, 'ascii');

readUInt16(offset, littleEndian) → {Number}

Returns an unsigned 16 bit integer starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 2.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 864
Returns:
A number between 0 and 65535.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var uint16BE = reader.readUInt16(0);
var uint16LE = reader.readUInt16(2, true);

readUInt32(offset, littleEndian) → {Number}

Returns an unsigned 32 bit integer starting from the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 4.
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 880
Returns:
A number between 0 and 4294967295.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var uint32BE = reader.readUInt32(0);
var uint32LE = reader.readUInt32(4, true);

readUInt8(offset) → {Number}

Returns an unsigned 8 bit integer at the specified position.

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length minus 1.
Source:
  • lib/BufferQueueReader.js, line 848
Returns:
A number between 0 and 255.
Type
Number
Throws:
If the specified offset exceeds the reader's boundries.
Type
Error
Example
var uint8 = reader.readUInt8(0);

readZeroString(offset, encoding) → {String}

Returns a string from the specified offset until the first occurence of the NULL character (\0).

Parameters:
Name Type Description
offset Number A starting index. Must be between 0 and the reader's length.
encoding String=utf8 An encoding of the string. Defaults to `utf8`.
Source:
  • lib/BufferQueueReader.js, line 780
Returns:
A string constructed from the read bytes or empty string if NULL character could not be found.
Type
String
Throws:
  • If the specified offset exceeds the reader's boundries.
    Type
    Error
  • If the specified encoding is not supported.
    Type
    Error
Example
var stringValue = reader.readZeroString(0, 'utf8');

shiftBits(count) → {Array.}

Shifts an array of bits (boolean values) from the reader.

Decreases the reader's length by a number of bytes that is needed to extract the specified number of bits (e.g. 4, 8 bits=1 byte, 9, 13, 16 bits=2 bytes etc.).

Parameters:
Name Type Description
count Number A number of bits to shift. Must be between 1 and the reader's length multiplied by 8.
Source:
  • lib/BufferQueueReader.js, line 276
Returns:
An array of bits.
Type
Array.<Boolean>
Throws:
If the specified count is not a number between 1 and the reader's length multiplied by 8.
Type
Error
Example
var bitsArray = reader.shiftBits(13);

shiftBuffer(count) → {Buffer}

Shifts the specified number of bytes as an instance of Buffer.

Decreases the reader's length by the specified byte count.

Parameters:
Name Type Description
count Number A number of bytes to shift. Must be between 1 and the reader's length.
Source:
  • lib/BufferQueueReader.js, line 380
Returns:
A buffer of the specified size.
Type
Buffer
Throws:
If the specified count is not a number between 1 and the reader's length.
Type
Error
Example
var buffer = reader.shiftBuffer(10);

shiftByte() → {Number}

Shifts a byte from the reader.

Decreases the reader's length by 1.

Source:
  • lib/BufferQueueReader.js, line 291
Returns:
A number between 0x00 and 0xFF.
Type
Number
Throws:
If the reader is empty.
Type
Error
Example
var byteValue = reader.shiftByte();

shiftBytes(count) → {Array.}

Shifts the specified number of bytes from the reader.

Decreases the reader's length by the specified byte count.

Parameters:
Name Type Description
count Number A number of bytes to shift. Must be between 1 and the reader's length.
Source:
  • lib/BufferQueueReader.js, line 339
Returns:
An array of bytes.
Type
Array.<Number>
Throws:
If the specified count is not a number between 1 and the reader's length.
Type
Error
Example
var bytesArray = reader.shiftBytes(6);

shiftChar() → {String}

Shifts an ASCII character from the reader.

Decreases the reader's length by 1.

Source:
  • lib/BufferQueueReader.js, line 323
Returns:
An ASCII character.
Type
String
Throws:
If the reader is empty.
Type
Error
Example
var charValue = reader.shiftChar();

shiftDouble(littleEndian) → {Number}

Shifts a signed 64 bit floating-point number as defined in IEEE 754.

Decreases the reader's length by eight bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 559
Returns:
A 64 bit floating-point number.
Type
Number
Throws:
If the reader's length is less than 8.
Type
Error
Example
var doubleBE = reader.shiftDouble();
var doubleLE = reader.shiftDouble(true);

shiftFloat(littleEndian) → {Number}

Shifts a signed 32 bit floating-point number as defined in IEEE 754.

Decreases the reader's length by four bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 542
Returns:
A 32 bit floating-point number.
Type
Number
Throws:
If the reader's length is less than 4.
Type
Error
Example
var floatBE = reader.shiftFloat();
var floatLE = reader.shiftFloat(true);

shiftInt16(littleEndian) → {Number}

Shifts a signed 16 bit integer.

Decreases the reader's length by two bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 459
Returns:
A number between -32768 and 32767.
Type
Number
Throws:
If the reader's length is less than 2.
Type
Error
Example
var int16BE = reader.shiftInt16();
var int16LE = reader.shiftInt16(true);

shiftInt32(littleEndian) → {Number}

Shifts a signed 32 bit integer.

Decreases the reader's length by four bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 476
Returns:
A number between -2147483648 and 2147483647.
Type
Number
Throws:
If the reader's length is less than 4.
Type
Error
Example
var int32BE = reader.shiftInt32();
var int32LE = reader.shiftInt32(true);

shiftInt8() → {Number}

Shifts a signed 8 bit integer.

Decreases the reader's length by one byte.

Source:
  • lib/BufferQueueReader.js, line 442
Returns:
A number between -128 and 127.
Type
Number
Throws:
If the reader is empty.
Type
Error
Example
var int8 = reader.shiftInt8();

shiftString(length, encoding) → {String}

Shifts the specified number of bytes as a string with the specified encoding.

Decreases the reader's length by the specified byte count.

Parameters:
Name Type Description
length Number A number of bytes to shift. Must be between 1 and the reader's length.
encoding String=utf8 An encoding of the string. Defaults to `utf8`.
Source:
  • lib/BufferQueueReader.js, line 399
Returns:
A string of the specified length.
Type
String
Throws:
  • If the specified length is not a number between 1 and the reader's length.
    Type
    Error
  • If the specified encoding is not supported.
    Type
    Error
Example
var stringValue = reader.shiftString(12, 'ascii');

shiftUInt16(littleEndian) → {Number}

Shifts an unsigned 16 bit integer.

Decreases the reader's length by two bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 508
Returns:
A number between 0 and 65535.
Type
Number
Throws:
If the reader's length is less than 2.
Type
Error
Example
var uint16BE = reader.shiftUInt16();
var uint16LE = reader.shiftUInt16(true);

shiftUInt32(littleEndian) → {Number}

Shifts an unsigned 32 bit integer.

Decreases the reader's length by four bytes.

Parameters:
Name Type Description
littleEndian Boolean Whether to use little endian instead of big endian.
Source:
  • lib/BufferQueueReader.js, line 525
Returns:
A number between 0 and 4294967295.
Type
Number
Throws:
If the reader's length is less than 4.
Type
Error
Example
var uint32BE = reader.shiftUInt32();
var uint32LE = reader.shiftUInt32(true);

shiftUInt8() → {Number}

Shifts an unsigned 8 bit integer.

Decreases the reader's length by one byte.

Source:
  • lib/BufferQueueReader.js, line 491
Returns:
A number between 0 and 255.
Type
Number
Throws:
If the reader is empty.
Type
Error
Example
var uint8 = reader.shiftUInt8();

shiftZeroString(encoding) → {String}

Shifts a string from the beginning of the reader until the first occurence of the NULL character (\0).

Decreases the reader's length by the returned string's length plus one.

Parameters:
Name Type Description
encoding String=utf8 An encoding of the string. Defaults to `utf8`.
Source:
  • lib/BufferQueueReader.js, line 416
Returns:
A string constructed from the shifted bytes or empty string if NULL character could not be found.
Type
String
Throws:
If the specified encoding is not supported.
Type
Error
Example
var stringValue = reader.shiftZeroString('utf8');

skip(count)

Skips the specified number of bytes.

If the byte count was not specified or it's value is greater than the length of the reader, skips all the bytes to the end.

Parameters:
Name Type Description
count Number=this.length A number of bytes to skip. Defaults to the reader's length.
Source:
  • lib/BufferQueueReader.js, line 88
Throws:
If the count is not a number greater than or equal to 0.
Type
Error
Example
reader.skip(10);