new BufferReader(buffer)
A class providing extended functionality for reading Buffer
instances.
Parameters:
Name | Type | Description |
---|---|---|
buffer |
Buffer | A buffer to wrap. |
- Source:
- lib/BufferReader.js, line 24
Properties:
Name | Type | Description |
---|---|---|
length |
Number | The remaining length of the reader. |
Throws:
- Type
- Error
Example
var buffer = new Buffer(256);
var reader = new BufferReader(buffer);
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/BufferReader.js, line 130
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, 0); reader.copy(buffer, 5, 4, 9);
-
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/BufferReader.js, line 79
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(0xAB, 10);
-
readBits(offset, count, bitOffset) → {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. bitOffset
Boolean TRUE, if the specified offset is a number of bits; Defaults to FALSE (offset is expressed in bytes). - Source:
- lib/BufferReader.js, line 513
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/BufferReader.js, line 614
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/BufferReader.js, line 528
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/BufferReader.js, line 567
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/BufferReader.js, line 551
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 792
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 774
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 688
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 706
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/BufferReader.js, line 672
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/BufferReader.js, line 633
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 738
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 756
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/BufferReader.js, line 722
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/BufferReader.js, line 650
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/BufferReader.js, line 198
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/BufferReader.js, line 286
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/BufferReader.js, line 213
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/BufferReader.js, line 251
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/BufferReader.js, line 235
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 493
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 472
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 369
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 390
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/BufferReader.js, line 348
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/BufferReader.js, line 305
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 430
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=false Whether to use little endian instead of big endian. - Source:
- lib/BufferReader.js, line 451
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/BufferReader.js, line 409
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/BufferReader.js, line 322
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/BufferReader.js, line 47
Throws:
If the count is not a number greater than or equal to 0.- Type
- Error
Example
reader.skip(10);