Class: BufferReader

utils.BufferReader

new BufferReader(data, zeroCopynullable)

An object that allows reading of buffers in a sane manner.

Parameters:
Name Type Attributes Description
data Buffer
zeroCopy Boolean <nullable>

Do not reallocate buffers when slicing. Note that this can lead to memory leaks if not used carefully.

Source:

Methods

createChecksum() → {Number}

Create a checksum from the last start position.

Source:
Returns:

Checksum.

Type
Number

destroy()

Destroy the reader. Remove references to the data.

Source:

end() → {Number}

Stop reading. Pop the start position off the stack and calculate the size of the data read.

Source:
Throws:

on empty stack.

Returns:

Size.

Type
Number

endData(zeroCopynullable) → {Buffer}

Stop reading. Pop the start position off the stack and return the data read.

Parameters:
Name Type Attributes Description
zeroCopy Bolean <nullable>

Do a fast buffer slice instead of allocating a new buffer (warning: may cause memory leaks if not used with care).

Source:
Throws:

on empty stack.

Returns:

Data read.

Type
Buffer

getSize() → {Buffer}

Get total size of passed-in Buffer.

Source:
Returns:
Type
Buffer

left() → {Number}

Calculate number of bytes left to read.

Source:
Returns:
Type
Number

read8() → {Number}

Read int8.

Source:
Returns:
Type
Number

read16() → {Number}

Read int16le.

Source:
Returns:
Type
Number

read16BE() → {Number}

Read int16be.

Source:
Returns:
Type
Number

read32() → {Number}

Read int32le.

Source:
Returns:
Type
Number

read32BE() → {Number}

Read int32be.

Source:
Returns:
Type
Number

read53() → {Number}

Read first least significant 53 bits of a int64le as a js number. Maintain the sign.

Source:
Returns:
Type
Number

read53BE() → {Number}

Read first least significant 53 bits of a int64be as a js number. Maintain the sign.

Source:
Returns:
Type
Number

read64() → {Number}

Read int64le as a js number.

Source:
Throws:

on num > MAX_SAFE_INTEGER

Returns:
Type
Number

read64BE() → {Number}

Read int64be as a js number.

Source:
Throws:

on num > MAX_SAFE_INTEGER

Returns:
Type
Number

read64BEBN() → {BN}

Read int64be.

Source:
Returns:
Type
BN

read64BN() → {BN}

Read int64le.

Source:
Returns:
Type
BN

readBytes(size, zeroCopynullable) → {Buffer}

Read N bytes (will do a fast slice if zero copy).

Parameters:
Name Type Attributes Description
size Number
zeroCopy Bolean <nullable>

Do a fast buffer slice instead of allocating a new buffer (warning: may cause memory leaks if not used with care).

Source:
Returns:
Type
Buffer

readDouble() → {Number}

Read double float le.

Source:
Returns:
Type
Number

readDoubleBE() → {Number}

Read double float be.

Source:
Returns:
Type
Number

readFloat() → {Number}

Read float le.

Source:
Returns:
Type
Number

readFloatBE() → {Number}

Read float be.

Source:
Returns:
Type
Number

readHash(enc) → {Hash|Buffer}

Read a 32-byte hash.

Parameters:
Name Type Description
enc String

"hex" or null.

Source:
Returns:
Type
Hash | Buffer

readNullString(enc) → {String}

Read a null-terminated string.

Parameters:
Name Type Description
enc String

Any buffer-supported encoding.

Source:
Returns:
Type
String

readString(enc, size) → {String}

Read a string.

Parameters:
Name Type Description
enc String

Any buffer-supported encoding.

size Number
Source:
Returns:
Type
String

readU8() → {Number}

Read uint8.

Source:
Returns:
Type
Number

readU16() → {Number}

Read uint16le.

Source:
Returns:
Type
Number

readU16BE() → {Number}

Read uint16be.

Source:
Returns:
Type
Number

readU32() → {Number}

Read uint32le.

Source:
Returns:
Type
Number

readU32BE() → {Number}

Read uint32be.

Source:
Returns:
Type
Number

readU53() → {Number}

Read first least significant 53 bits of a uint64le as a js number. Maintain the sign.

Source:
Returns:
Type
Number

readU53BE() → {Number}

Read first least significant 53 bits of a uint64be as a js number. Maintain the sign.

Source:
Returns:
Type
Number

readU64() → {Number}

Read uint64le as a js number.

Source:
Throws:

on num > MAX_SAFE_INTEGER

Returns:
Type
Number

readU64BE() → {Number}

Read uint64be as a js number.

Source:
Throws:

on num > MAX_SAFE_INTEGER

Returns:
Type
Number

readU64BEBN() → {BN}

Read uint64be.

Source:
Returns:
Type
BN

readU64BN() → {BN}

Read uint64le.

Source:
Returns:
Type
BN

readVarBytes(zeroCopynullable) → {Buffer}

Read a varint number of bytes (will do a fast slice if zero copy).

Parameters:
Name Type Attributes Description
zeroCopy Bolean <nullable>

Do a fast buffer slice instead of allocating a new buffer (warning: may cause memory leaks if not used with care).

Source:
Returns:
Type
Buffer

readVarint() → {Number}

Read a varint.

Source:
Returns:
Type
Number

readVarint2() → {Number}

Read a varint (type 2).

Source:
Returns:
Type
Number

readVarint2BN() → {BN}

Read a varint (type 2).

Source:
Returns:
Type
BN

readVarintBN() → {BN}

Read a varint.

Source:
Returns:
Type
BN

readVarString(enc, limitnullable) → {String}

Read string of a varint length.

Parameters:
Name Type Attributes Description
enc String

Any buffer-supported encoding.

limit Number <nullable>

Size limit.

Source:
Returns:
Type
String

seek(off)

Seek to a position to read from by offset.

Parameters:
Name Type Description
off Number

Offset (positive or negative).

Source:

skipVarint() → {Number}

Skip past a varint.

Source:
Returns:
Type
Number

skipVarint2() → {Number}

Skip past a varint (type 2).

Source:
Returns:
Type
Number

start()

Mark the current starting position.

Source:

verifyChecksum() → {Number}

Verify a 4-byte checksum against a calculated checksum.

Source:
Throws:

on bad checksum

Returns:

checksum

Type
Number