Module: ByteBuffer

ByteBuffer

A full-featured ByteBuffer implementation in JavaScript using typed arrays.

Constructs a new ByteBuffer.

Members

<static, constant> BIG_ENDIAN :boolean

Big endian constant for usage in constructors instead of a boolean value. Evaluates to false.

<static, constant> DEFAULT_CAPACITY :number

Default buffer capacity of 16. The ByteBuffer will be automatically resized by a factor of 2 if required.

<static, constant> LITTLE_ENDIAN :boolean

Little endian constant for usage in constructors instead of a boolean value. Evaluates to true.

<static, constant> Long :Long

Long class for int64 support. May be null if the Long class has not been loaded and int64 support is not available.

<static, constant> MAX_VARINT32_BYTES :number

Maximum number of bytes used by 32bit base 128 variable-length integer.

<static, constant> MAX_VARINT64_BYTES :number

Maximum number of bytes used by a 64bit base 128 variable-length integer.

<static, constant> VERSION :string

Version string.

array :ArrayBuffer

Backing ArrayBuffer.

length :number

Length of the contained data. Offset- and capacity-independent index. Contents are the bytes between offset and length, which are both absolute indexes. There is no capacity property, use ByteBuffer#capacity instead.

littleEndian :boolean

Whether to use little endian multi byte values, defaults to false for big endian.

markedOffset :number

Marked offset set through ByteBuffer#mark. Defaults to -1 (no marked offset).

offset :number

Current read/write offset. Length- and capacity-independent index. Contents are the bytes between offset and length, which are both absolute indexes. There is no capacity property, use ByteBuffer#capacity instead.

view :DataView

DataView to mess with the ArrayBuffer.

Methods

<static> allocate(capacity, littleEndian) → {ByteBuffer}

Allocates a new ByteBuffer.

Parameters:
Name Type Argument Description
capacity number <optional>

Initial capacity. Defaults to ByteBuffer.DEFAULT_CAPACITY.

littleEndian boolean <optional>

true to use little endian multi byte values, defaults to false for big endian.

Returns:
Type
ByteBuffer

<static> calculateUTF8Char(charCode) → {number}

Calculates the actual number of bytes required to encode the specified char code.

Parameters:
Name Type Description
charCode number

Character to encode as char code

Throws:
If the character cannot be calculated (too large)
Type
Error
Returns:

Number of bytes required to encode the specified char code

Type
number

<static> calculateUTF8String(str) → {number}

Calculates the number of bytes required to store an UTF8 encoded string.

Parameters:
Name Type Description
str string

String to calculate

Returns:

Number of bytes required

Type
number

<static> calculateVarint32(value) → {number}

Calculates the actual number of bytes required to encode a 32bit base 128 variable-length integer.

Parameters:
Name Type Description
value number

Value to encode

Returns:

Number of bytes required. Capped to ByteBuffer.MAX_VARINT32_BYTES

Type
number

<static> calculateVarint64(value) → {number}

Calculates the actual number of bytes required to encode a 64bit base 128 variable-length integer.

Parameters:
Name Type Description
value number | !Long

Value to encode

Returns:

Number of bytes required. Capped to ByteBuffer.MAX_VARINT64_BYTES

Type
number

<static> decode64(str, littleEndian) → {ByteBuffer}

Decodes a base64 encoded string to a ByteBuffer.

Parameters:
Name Type Argument Description
str string

Base64 encoded string

littleEndian boolean <optional>

true to use little endian byte order, defaults to false for big endian.

Throws:
If the argument is not a valid base64 encoded string
Type
Error
Returns:

ByteBuffer

Type
ByteBuffer

<static> decodeBinary(str, littleEndian) → {ByteBuffer}

Decodes a binary string to a ByteBuffer. A binary string in this case is a string composed of 8bit values as characters with a char code between 0 and 255 inclusive.

Parameters:
Name Type Argument Description
str string

Binary string

littleEndian boolean <optional>

true to use little endian byte order, defaults to false for big endian.

Throws:
If the argument is not a valid binary string
Type
Error
Returns:

ByteBuffer

Type
ByteBuffer

<static> decodeHex(str, littleEndian) → {ByteBuffer}

Decodes a hex encoded string to a ByteBuffer.

Parameters:
Name Type Argument Description
str string

Hex encoded string

littleEndian boolean <optional>

true to use little endian byte order, defaults to false for big endian.

Throws:
If the argument is not a valid hex encoded string
Type
Error
Returns:

ByteBuffer

Type
ByteBuffer

<static> decodeUTF8Char(src, offset) → {{char: number, length: number}}

Decodes a single UTF8 character from the specified ByteBuffer. The ByteBuffer's offsets are not modified.

Parameters:
Name Type Description
src ByteBuffer
offset number

Offset to read from

Throws:
If the character cannot be decoded or there is a capacity overflow
Type
Error
Returns:

Decoded char code and the actual number of bytes read

Type
{char: number, length: number}

<static> encode64(bb) → {string}

Encodes a ByteBuffer's contents to a base64 string.

Parameters:
Name Type Description
bb ByteBuffer

ByteBuffer to encode. Will be cloned and flipped if length < offset.

Throws:
If the argument is not a valid ByteBuffer
Type
Error
Returns:

Base64 encoded string

Type
string

<static> encodeBinary(bb) → {string}

Encodes a ByteBuffer to a binary string. A binary string in this case is a string composed of 8bit values as characters with a char code between 0 and 255 inclusive.

Parameters:
Name Type Description
bb ByteBuffer

ByteBuffer to encode. Will be cloned and flipped if length < offset.

Throws:
If the argument is not a valid ByteBuffer
Type
Error
Returns:

Binary string

Type
string

<static> encodeHex(bb) → {string}

Encodes a ByteBuffer to a hex encoded string.

Parameters:
Name Type Description
bb ByteBuffer

ByteBuffer to encode. Will be cloned and flipped if length < offset.

Throws:
If the argument is not a valid ByteBuffer
Type
Error
Returns:

Hex encoded string

Type
string

<static> encodeUTF8Char(charCode, dst, offset) → {number}

Encodes a single UTF8 character to the specified ByteBuffer. The ByteBuffer's offsets are not modified.

Parameters:
Name Type Description
charCode number

Character to encode as char code

dst ByteBuffer

ByteBuffer to encode to

offset number

Offset to write to

Throws:
If the character cannot be encoded
Type
Error
Returns:

Actual number of bytes written

Type
number

<static> isByteBuffer(bb) → {boolean}

Tests if the specified type is a ByteBuffer or ByteBuffer-like.

Parameters:
Name Type Description
bb *

ByteBuffer to test

Returns:

true if it is a ByteBuffer or ByteBuffer-like, otherwise false

Type
boolean

<static> wrap(buffer, enc, littleEndian) → {ByteBuffer}

Wraps an ArrayBuffer, any object containing an ArrayBuffer, a node buffer or a string. Sets the created ByteBuffer's offset to 0 and its length to the wrapped object's byte length.

Parameters:
Name Type Argument Description
buffer ArrayBuffer | !Buffer | !{array: !ArrayBuffer} | !{buffer: !ArrayBuffer} | string

Anything that can be wrapped

enc string | boolean <optional>

String encoding if a string is provided (hex, utf8, binary, defaults to base64)

littleEndian boolean <optional>

true to use little endian multi byte values, defaults to false for big endian.

Throws:
If the specified object cannot be wrapped
Type
Error
Returns:
Type
ByteBuffer

<static> zigZagDecode32(n) → {number}

Decodes a zigzag encoded signed 32bit integer.

Parameters:
Name Type Description
n number

Unsigned zigzag encoded 32bit integer

Returns:

Signed 32bit integer

Type
number

<static> zigZagDecode64(n) → {Long}

Decodes a zigzag encoded signed 64bit integer.

Parameters:
Name Type Description
n Long | number

Unsigned zigzag encoded long or JavaScript number

Throws:
If long support is not available
Type
Error
Returns:

Signed long

Type
Long

<static> zigZagEncode32(n) → {number}

Encodes a signed 32bit integer so that it can be effectively used with varint encoding.

Parameters:
Name Type Description
n number

Signed 32bit integer

Returns:

Unsigned zigzag encoded 32bit integer

Type
number

<static> zigZagEncode64(n) → {Long}

Encodes a signed 64bit integer so that it can be effectively used with varint encoding.

Parameters:
Name Type Description
n number | !Long

Signed long

Returns:

Unsigned zigzag encoded long

Type
Long

append(src, offset) → {ByteBuffer}

Appends another ByteBuffer to this one. Appends only the portion between offset and length of the specified ByteBuffer and overwrites any contents behind the specified offset up to the number of bytes contained in the specified ByteBuffer. Offset and length of the specified ByteBuffer will remain the same.

Parameters:
Name Type Argument Description
src *

ByteBuffer or any object that can be wrapped to append

offset number <optional>

Offset to append at. Defaults to ByteBuffer#offset.

Throws:
If the specified buffer is already destroyed
Type
Error
Returns:

this

Type
ByteBuffer

BE(bigEndian) → {ByteBuffer}

Switches big endian byte order.

Parameters:
Name Type Argument Description
bigEndian boolean <optional>

Defaults to true, otherwise uses little endian

Returns:

this

Type
ByteBuffer

capacity() → {number}

Gets the capacity of the backing buffer. This is independent from ByteBuffer#length and returns the size of the entire backing array.

Returns:

Capacity of the backing array or 0 if destroyed

Type
number

clone() → {ByteBuffer}

Clones this ByteBuffer. The returned cloned ByteBuffer shares the same backing array but will have its own offsets.

Returns:

Clone

Type
ByteBuffer

compact() → {ByteBuffer}

Compacts the ByteBuffer to be backed by an ArrayBuffer of its actual length. Will set offset=0 and length=capacity.

Throws:
If the buffer cannot be compacted
Type
Error
Returns:

this

Type
ByteBuffer

copy() → {ByteBuffer}

Copies this ByteBuffer. The copy has its own backing array and uses the same offsets as this one.

Returns:

Copy

Type
ByteBuffer

destroy() → {ByteBuffer}

Manually destroys the ByteBuffer, releasing references to the backing array. Manually destroying a ByteBuffer is usually not required but may be useful in limited memory environments. Most successive operations will rise an error until ByteBuffer#resize or ByteBuffer#ensureCapacity is called to reinitialize the backing array.

Returns:

this

Type
ByteBuffer

ensureCapacity(capacity) → {ByteBuffer}

Makes sure that the specified capacity is available. If the current capacity is exceeded, it will be doubled. If double the previous capacity is less than the required capacity, the required capacity will be used.

Parameters:
Name Type Description
capacity number

Required capacity

Returns:

this

Type
ByteBuffer

flip() → {ByteBuffer}

Makes the buffer ready for a new sequence of write or relative read operations. Sets length=offset and offset=0. Always make sure to flip a buffer when all relative writing operations are complete.

Returns:

this

Type
ByteBuffer

LE(littleEndian) → {ByteBuffer}

Switches little endian byte order.

Parameters:
Name Type Argument Description
littleEndian boolean <optional>

Defaults to true, otherwise uses big endian

Returns:

this

Type
ByteBuffer

mark(offset) → {ByteBuffer}

Marks an offset to be used with ByteBuffer#reset.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to mark. Defaults to ByteBuffer#offset.

See:
  • ByteBuffer#reset
Throws:
If the mark cannot be set
Type
Error
Returns:

this

Type
ByteBuffer

prepend(src, offset) → {ByteBuffer}

Prepends another ByteBuffer to this one. Prepends only the portion between offset and length of the specified ByteBuffer and overwrites any contents before the specified offsets up to the number of bytes contained in the specified ByteBuffer. Offset and length of the specified ByteBuffer will remain the same.

Parameters:
Name Type Argument Description
src *

ByteBuffer or any object that can be wrapped to prepend

offset number <optional>

Offset to prepend at. Defaults to ByteBuffer#offset.

Throws:
If the specified buffer is already destroyed
Type
Error
Returns:

this

Type
ByteBuffer

printDebug(out)

Prints debug information about this ByteBuffer's contents.

Parameters:
Name Type Argument Description
out function(string) <optional>

Output function to call, defaults to console.log

readByte(offset) → {number}

Reads a byte. This is an alias of ByteBuffer#readInt8.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readCString(offset) → {string|!{string: string, length: number}}

Reads a string followed by a NULL character (Uint8).

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If the string cannot be decoded
Type
Error
Returns:

The string read if offset is omitted, else the string read and the actual number of bytes read.

Type
string | !{string: string, length: number}

readDouble(offset) → {number}

Reads a double. This is an alias of ByteBuffer#readFloat64.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readFloat(offset) → {number}

Reads a float. This is an alias of ByteBuffer#readFloat32.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readFloat32(offset) → {number}

Reads a 32bit float.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readFloat64(offset) → {number}

Reads a 64bit float.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readInt(offset) → {number}

Reads an integer. This is an alias of ByteBuffer#readInt32.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readInt8(offset) → {number}

Reads an 8bit signed integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readInt16(offset) → {number}

Reads a 16bit signed integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readInt32(offset) → {number}

Reads a 32bit signed integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readInt64(offset) → {Long}

Reads a 64bit integer. Requires Long.js.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
Long

readJSON(offset, parse) → {*|!{data: *, length: number}}

Reads a JSON payload and unserializes it.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

parse function(string) <optional>

Parse implementation to use. Defaults to JSON.parse.

Throws:
If the data cannot be decoded
Type
Error
Returns:

Data payload if offset is omitted, else the data payload and the actual number of bytes read

Type
* | !{data: *, length: number}

readLong(offset) → {Long}

Reads a long. This is an alias of ByteBuffer#readInt64.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
Long

readLString(offset) → {string|{string: string, length: number}}

Reads a string with a prepended number of characters, which is also encoded as an UTF8 character.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If the string cannot be decoded
Type
Error
Returns:

The string read if offset is omitted, else the string read and the actual number of bytes read.

Type
string | {string: string, length: number}

readShort(offset) → {number}

Reads a short value. This is an alias of ByteBuffer#readInt16.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readUint8(offset) → {number}

Reads an 8bit unsigned integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readUint16(offset) → {number}

Reads a 16bit unsigned integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readUint32(offset) → {number}

Reads a 32bit unsigned integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
number

readUint64(offset) → {Long}

Reads a 64bit unsigned integer. Requires Long.js.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If offset is out of bounds
Type
Error
Returns:
Type
Long

readUTF8String(chars, offset) → {string|!{string: string, length: number}}

Reads an UTF8 string.

Parameters:
Name Type Argument Description
chars number

Number of characters to read

offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If the string cannot be decoded
Type
Error
Returns:

The string read if offset is omitted, else the string read and the actual number of bytes read.

Type
string | !{string: string, length: number}

readUTF8StringBytes(length, offset) → {string|!{string: string, length: number}}

Reads an UTF8 string with the specified byte length.

Parameters:
Name Type Argument Description
length number

Byte length

offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If the length did not match or the string cannot be decoded
Type
Error
Returns:

The string read if offset is omitted, else the string read and the actual number of bytes read.

Type
string | !{string: string, length: number}

readVarint(offset) → {number|{value: number, length: number}}

Reads a base 128 variable-length integer as used in protobuf. This is an alias of ByteBuffer#readVarint32.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Defaults to ByteBuffer#offset which will be modified only if omitted.

Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
number | {value: number, length: number}

readVarint32(offset) → {number|!{value: number, length: number}}

Reads a 32bit base 128 variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If it's not a valid varint
Type
Error
Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
number | !{value: number, length: number}

readVarint64(offset) → {Long|!{value: Long, length: number}}

Reads a 32bit base 128 variable-length integer as used in protobuf. Requires Long.js.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If it's not a valid varint
Type
Error
Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
Long | !{value: Long, length: number}

readVString(offset) → {string|!{string: string, length: number}}

Reads a string with prepended number of characters, which is encoded as a 32bit base 128 variable-length integer.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If the string cannot be decoded or if it is not preceeded by a valid varint
Type
Error
Returns:

The string read if offset is omitted, else the string read and the actual number of bytes read.

Type
string | !{string: string, length: number}

readZigZagVarint(offset) → {number|{value: number, length: number}}

Reads a zigzag encoded base 128 variable-length integer as used in protobuf. This is an alias of ByteBuffer#readZigZagVarint32.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Defaults to ByteBuffer#offset which will be modified only if omitted.

Throws:
If it's not a valid varint
Type
Error
Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
number | {value: number, length: number}

readZigZagVarint32(offset) → {number|!{value: number, length: number}}

Reads a zigzag encoded 32bit base 128 variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Will use and advance ByteBuffer#offset if omitted.

Throws:
If it's not a valid varint
Type
Error
Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
number | !{value: number, length: number}

readZigZagVarint64(offset) → {Long|!{value: Long, length: number}}

Reads a zigzag encoded 64bit base 128 variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
offset number <optional>

Offset to read from. Defaults to ByteBuffer#offset which will be modified only if omitted.

Throws:
If it's not a valid varint
Type
Error
Returns:

The value read if offset is omitted, else the value read and the actual number of bytes read.

Type
Long | !{value: Long, length: number}

remaining() → {number}

Gets the number of remaining readable bytes. Contents are the bytes between offset and length, so this returns length-offset.

Returns:

Remaining readable bytes. May be negative if offset>length.

Type
number

reset() → {ByteBuffer}

Resets the ByteBuffer. If an offset has been marked through ByteBuffer#mark before, the offset will be set to the marked offset and the marked offset will be discarded. Length will not be altered. If there is no marked offset, sets offset=0 and length=0.

See:
  • ByteBuffer#mark
Returns:

this

Type
ByteBuffer

resize(capacity) → {ByteBuffer}

Resizes the ByteBuffer to the given capacity. Will do nothing if already that large or larger.

Parameters:
Name Type Description
capacity number

New capacity

Returns:

this

Type
ByteBuffer

reverse() → {ByteBuffer}

Reverses the backing array and adapts offset and length to retain the same relative position on the reversed data in inverse order. Example: "00<01 02>03 04".reverse() = "04 03<02 01>00". Also clears the marked offset.

Throws:
If the buffer is already destroyed
Type
Error
Returns:

this

Type
ByteBuffer

slice(begin, end) → {ByteBuffer}

Slices the ByteBuffer. This is independent of the ByteBuffer's actual offsets. Does not compact the underlying ArrayBuffer (use ByteBuffer#compact or ByteBuffer.wrap instead).

Parameters:
Name Type Argument Description
begin number <optional>

Begin offset, defaults to ByteBuffer#offset.

end number <optional>

End offset, defaults to ByteBuffer#length.

Throws:
If the buffer cannot be sliced
Type
Error
Returns:

Clone of this ByteBuffer with slicing applied, backed by the same ArrayBuffer

Type
ByteBuffer

toArrayBuffer(forceCopy) → {ArrayBuffer}

Returns an ArrayBuffer compacted to contain this ByteBuffer's actual contents. Will transparently ByteBuffer#flip the ByteBuffer if its offset is larger than its length. Will return a reference to the unmodified backing buffer if offset=0 and length=capacity unless forceCopy is set to true.

Parameters:
Name Type Argument Description
forceCopy boolean <optional>

true forces the creation of a copy, defaults to false

Returns:

Compacted ArrayBuffer or null if already destroyed

Type
ArrayBuffer

toBase64() → {string}

Returns the base64 encoded representation of the ByteBuffer's contents.

Returns:

Base 64 encoded string

Type
string

toBinary() → {string}

Returns the ByteBuffer's contents between offset and length as a binary string. A binary string in this case is a string composed of 8bit values as characters with a char code between 0 and 255 inclusive.

Returns:

Binary string

Type
string

toBuffer() → {Buffer}

Returns a node Buffer compacted to contain this ByteBuffer's actual contents. Will transparently ByteBuffer#flip the ByteBuffer if its offset is larger than its length. Will also copy all data (not a reference).

Returns:

Compacted node Buffer or null if already destroyed

Type
Buffer

toColumns(wrap) → {string}

Returns a textual two columns (hex, ascii) representation of this ByteBuffer's backing array.

Parameters:
Name Type Argument Description
wrap number <optional>

Wrap length. Defaults to 16.

Returns:

Hex representation as of " 00<01 02>03... ASCII DATA" with marked offsets

Type
string

toHex(debug) → {string}

Returns the ByteBuffer's contents between offset and length as a hex string.

Parameters:
Name Type Argument Description
debug boolean <optional>

true to return the entire backing array with marked offsets, defaults to false

Returns:

Hex string or debug string

Type
string

toString(enc) → {string}

Converts the ByteBuffer to a string.

Parameters:
Name Type Argument Description
enc string <optional>

Output encoding. Returns an informative string representation by default but also allows direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with marked offsets.

Returns:

String representation

Type
string

toUTF8() → {string}

Returns the ByteBuffer's contents as an UTF8 encoded string.

Returns:
Type
string

writeByte(value, offset) → {ByteBuffer}

Writes a byte. This is an alias of {ByteBuffer#writeInt8}.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeCString(str, offset) → {ByteBuffer|number}

Writes a string followed by a NULL character (Uint8). Beware: The source string must not contain NULL characters unless this is actually intended. This is not checked. If you have the option it is recommended to use ByteBuffer#writeLString or ByteBuffer#writeVString with the corresponding reading methods instead.

Parameters:
Name Type Argument Description
str string

String to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written

Type
ByteBuffer | number

writeDouble(value, offset) → {ByteBuffer}

Writes a double. This is an alias of ByteBuffer#writeFloat64.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeFloat(value, offset) → {ByteBuffer}

Writes a float. This is an alias of ByteBuffer#writeFloat32.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeFloat32(value, offset) → {ByteBuffer}

Writes a 32bit float.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeFloat64(value, offset) → {ByteBuffer}

Writes a 64bit float.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeInt(value, offset) → {ByteBuffer}

Writes an integer. This is an alias of ByteBuffer#writeInt32.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeInt8(value, offset) → {ByteBuffer}

Writes an 8bit signed integer.

Parameters:
Name Type Argument Description
value number

Value

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeInt16(value, offset) → {ByteBuffer}

Writes a 16bit signed integer.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeInt32(value, offset) → {ByteBuffer}

Writes a 32bit signed integer.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeInt64(value, offset) → {ByteBuffer}

Writes a 64bit integer. Requires Long.js.

Parameters:
Name Type Argument Description
value number | !Long

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeJSON(data, offset, stringify) → {ByteBuffer|number}

Serializes and writes a JSON payload.

Parameters:
Name Type Argument Description
data *

Data payload to serialize

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

stringify function(*) <optional>

Stringify implementation to use. Defaults to JSON.stringify.

Returns:

this if offset is omitted, else the actual number if bytes written

Type
ByteBuffer | number

writeLong(value, offset) → {ByteBuffer}

Writes a long. This is an alias of ByteBuffer#writeInt64.

Parameters:
Name Type Argument Description
value number | !Long

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeLString(str, offset) → {ByteBuffer|number}

Writes a string with prepended number of characters, which is also encoded as an UTF8 character..

Parameters:
Name Type Argument Description
str string

String to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeShort(value, offset) → {ByteBuffer}

Writes a short value. This is an alias of ByteBuffer#writeInt16.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeUint8(value, offset) → {ByteBuffer}

Writes an 8bit unsigned integer.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeUint16(value, offset) → {ByteBuffer}

Writes a 16bit unsigned integer.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeUint32(value, offset) → {ByteBuffer}

Writes a 32bit unsigned integer.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeUint64(value, offset) → {ByteBuffer}

Writes a 64bit unsigned integer. Requires Long.js.

Parameters:
Name Type Argument Description
value number | !Long

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this

Type
ByteBuffer

writeUTF8String(str, offset) → {ByteBuffer|number}

Writes an UTF8 string.

Parameters:
Name Type Argument Description
str string

String to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeVarint(value, offset) → {ByteBuffer|number}

Writes a base 128 variable-length integer as used in protobuf. This is an alias of ByteBuffer#writeVarint32.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Defaults to ByteBuffer#offset which will be modified only if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeVarint32(value, offset) → {ByteBuffer|number}

Writes a 32bit base 128 variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeVarint64(value, offset) → {ByteBuffer|number}

Writes a 64bit base 128 variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
value number | Long

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeVString(str, offset) → {ByteBuffer|number}

Writes a string with prepended number of characters, which is encoded as a 32bit base 128 variable-length integer.

Parameters:
Name Type Argument Description
str string

String to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written

Type
ByteBuffer | number

writeZigZagVarint(value, offset) → {ByteBuffer|number}

Writes a zigzag encoded base 128 encoded variable-length integer as used in protobuf. This is an alias of ByteBuffer#writeZigZagVarint32.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Defaults to ByteBuffer#offset which will be modified only if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeZigZagVarint32(value, offset) → {ByteBuffer|number}

Writes a zigzag encoded 32bit base 128 encoded variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Will use and advance ByteBuffer#offset if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number

writeZigZagVarint64(value, offset) → {ByteBuffer|number}

Writes a zigzag encoded 64bit base 128 encoded variable-length integer as used in protobuf.

Parameters:
Name Type Argument Description
value number

Value to write

offset number <optional>

Offset to write to. Defaults to ByteBuffer#offset which will be modified only if omitted.

Returns:

this if offset is omitted, else the actual number of bytes written.

Type
ByteBuffer | number