Skip to content
Daniel Wirtz edited this page Jun 12, 2014 · 18 revisions

Class ByteBuffer

The swiss army knife for binary data in JavaScript.


new ByteBuffer(capacity=, littleEndian=, noAssert=)

Constructs a new ByteBuffer.

Parameter Type Description
capacity number Initial capacity. Defaults to ByteBuffer.DEFAULT_CAPACITY.
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.

ByteBuffer.BIG_ENDIAN

Big endian constant that can be used instead of its boolean value. Evaluates to false.

| | |-----------------|----------------- | @type | boolean
| @access | public const

ByteBuffer.DEFAULT_CAPACITY

Default initial capacity of 16.

| | |-----------------|----------------- | @type | number

ByteBuffer.DEFAULT_ENDIAN

Default endianess of false for big endian.

| | |-----------------|----------------- | @type | boolean

ByteBuffer.DEFAULT_NOASSERT

Default no assertions flag of false.

| | |-----------------|----------------- | @type | boolean

ByteBuffer.LITTLE_ENDIAN

Little endian constant that can be used instead of its boolean value. Evaluates to true.

| | |-----------------|----------------- | @type | boolean
| @access | public const

ByteBuffer.Long

A Long class for representing a 64-bit two's-complement integer value. May be null if Long.js has not been loaded and int64 support is not available.

| | |-----------------|----------------- | @type | Long
| @access | public const

ByteBuffer.MAX_VARINT32_BYTES

Maximum number of bytes required to store a 32bit base 128 variable-length integer.

| | |-----------------|----------------- | @type | number
| @access | public const

ByteBuffer.MAX_VARINT64_BYTES

Maximum number of bytes required to store a 64bit base 128 variable-length integer.

| | |-----------------|----------------- | @type | number
| @access | public const

ByteBuffer.METRICS_BYTES

Metrics representing number of bytes. Evaluates to 2.

| | |-----------------|----------------- | @type | number
| @access | public const

ByteBuffer.METRICS_CHARS

Metrics representing number of UTF8 characters. Evaluates to 1.

| | |-----------------|----------------- | @type | number
| @access | public const

ByteBuffer.VERSION

ByteBuffer version.

| | |-----------------|----------------- | @type | string
| @access | public const

ByteBuffer.allocate(capacity=, littleEndian=, noAssert=)

Allocates a new ByteBuffer backed by a buffer of the specified capacity.

Parameter Type Description
capacity number Initial capacity. Defaults to ByteBuffer.DEFAULT_CAPACITY.
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer

ByteBuffer.atob(b64)

Decodes a base64 encoded string to binary like window.atob does.

Parameter Type Description
b64 string Base64 encoded string
@returns string Binary string

ByteBuffer.btoa(str)

Encodes a binary string to base64 like window.btoa does.

Parameter Type Description
str string Binary string
@returns string Base64 encoded string

ByteBuffer.calculateUTF8Bytes(str)

Calculates the number of UTF8 bytes of a string.

Parameter Type Description
str string String to calculate
@returns number Number of UTF8 bytes

ByteBuffer.calculateUTF8Chars(str)

Calculates the number of UTF8 characters of a string. JavaScript itself uses UTF-16, so that a string's length property does not reflect its actual UTF8 size if it contains code points larger than 0xFFFF.

Parameter Type Description
str string String to calculate
@returns number Number of UTF8 characters

ByteBuffer.calculateVarint32(value)

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

Parameter Type Description
value number Value to encode
@returns number Number of bytes required. Capped to ByteBuffer.MAX_VARINT32_BYTES

ByteBuffer.calculateVarint64(value)

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

Parameter Type Description
value number | !Long Value to encode
@returns number Number of bytes required. Capped to ByteBuffer.MAX_VARINT64_BYTES

ByteBuffer.concat(buffers, encoding=, littleEndian=, noAssert=)

Concatenates multiple ByteBuffers into one.

Parameter Type Description
buffers !Array.<(!ByteBuffer | !ArrayBuffer | !Uint8Array | string)> Buffers to concatenate
encoding string | boolean String encoding if buffers contains a string ("base64", "hex", "binary", defaults to "utf8")
littleEndian boolean Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer Concatenated ByteBuffer

ByteBuffer.fromBase64(str, littleEndian=, noAssert=)

Decodes a base64 encoded string to a ByteBuffer.

Parameter Type Description
str string String to decode
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer ByteBuffer

ByteBuffer.fromBinary(str, littleEndian=, noAssert=)

Decodes a binary encoded string, that is using only characters 0x00-0xFF as bytes, to a ByteBuffer.

Parameter Type Description
str string String to decode
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer ByteBuffer

ByteBuffer.fromDebug(str, littleEndian=, noAssert=)

Decodes a hex encoded string with marked offsets to a ByteBuffer.

Parameter Type Description
str string Debug string to decode (not be generated with columns = true)
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer ByteBuffer

ByteBuffer.fromHex(str, littleEndian=, noAssert=)

Decodes a hex encoded string to a ByteBuffer.

Parameter Type Description
str string String to decode
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer ByteBuffer

ByteBuffer.fromUTF8(str, littleEndian=, noAssert=)

Decodes an UTF8 encoded string to a ByteBuffer.

Parameter Type Description
str string String to decode
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer ByteBuffer

ByteBuffer.isByteBuffer(bb)

Tests if the specified type is a ByteBuffer.

Parameter Type Description
bb *** ByteBuffer to test
@returns boolean true if it is a ByteBuffer, otherwise false

ByteBuffer.type()

Gets the backing buffer type.

Parameter Type Description
@returns Function Buffer for NB builds, ArrayBuffer for AB builds (classes)

ByteBuffer.wrap(buffer, encoding=, littleEndian=, noAssert=)

Wraps a buffer or a string. Sets the allocated ByteBuffer's ByteBuffer#offset to 0 and its ByteBuffer#limit to the length of the wrapped data.

Parameter Type Description
buffer !ByteBuffer | !ArrayBuffer | !Uint8Array | string Anything that can be wrapped
encoding string | boolean String encoding if buffer is a string ("base64", "hex", "binary", defaults to "utf8")
littleEndian boolean Whether to use little or big endian byte order. Defaults to ByteBuffer.DEFAULT_ENDIAN.
noAssert boolean Whether to skip assertions of offsets and values. Defaults to ByteBuffer.DEFAULT_NOASSERT.
@returns !ByteBuffer A ByteBuffer wrapping buffer

ByteBuffer.zigZagDecode32(n)

Decodes a zigzag encoded signed 32bit integer.

Parameter Type Description
n number Unsigned zigzag encoded 32bit integer
@returns number Signed 32bit integer

ByteBuffer.zigZagDecode64(value)

Decodes a zigzag encoded signed 64bit integer.

Parameter Type Description
value !Long | number Unsigned zigzag encoded long or JavaScript number
@returns !Long Signed long

ByteBuffer.zigZagEncode32(n)

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

Parameter Type Description
n number Signed 32bit integer
@returns number Unsigned zigzag encoded 32bit integer

ByteBuffer.zigZagEncode64(value)

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

Parameter Type Description
value number | !Long Signed long
@returns !Long Unsigned zigzag encoded long

ByteBuffer#buffer

Backing buffer.

| | |-----------------|----------------- | @type | !ArrayBuffer

ByteBuffer#limit

Absolute limit of the contained data. Set to the backing buffer's capacity upon allocation.

| | |-----------------|----------------- | @type | number

ByteBuffer#littleEndian

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

| | |-----------------|----------------- | @type | boolean

ByteBuffer#markedOffset

Marked offset.

| | |-----------------|----------------- | @type | number

ByteBuffer#noAssert

Whether to skip assertions of offsets and values, defaults to false.

| | |-----------------|----------------- | @type | boolean

ByteBuffer#offset

Absolute read/write offset.

| | |-----------------|----------------- | @type | number

ByteBuffer#view

Data view to manipulate the backing buffer. Becomes null if the backing buffer has a capacity of 0.

| | |-----------------|----------------- | @type | DataView

ByteBuffer#BE(bigEndian=)

Switches (to) big endian byte order.

Parameter Type Description
bigEndian boolean Defaults to true, otherwise uses little endian
@returns !ByteBuffer this

ByteBuffer#LE(littleEndian=)

Switches (to) little endian byte order.

Parameter Type Description
littleEndian boolean Defaults to true, otherwise uses big endian
@returns !ByteBuffer this

ByteBuffer#append(source, encoding=, offset=)

Appends some data to this ByteBuffer. This will overwrite any contents behind the specified offset up to the appended data's length.

Parameter Type Description
source !ByteBuffer | !ArrayBuffer | !Uint8Array | string Data to append. If source is a ByteBuffer, its offsets will be modified according to the performed read operation.
encoding string | number Encoding if data is a string ("base64", "hex", "binary", defaults to "utf8")
offset number Offset to append at. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns !ByteBuffer this

ByteBuffer#appendTo(target, offset=)

Appends this ByteBuffer's contents to another ByteBuffer. This will overwrite any contents behind the specified offset up to the length of this ByteBuffer's data.

Parameter Type Description
target !ByteBuffer Target ByteBuffer
offset number Offset to append to. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns !ByteBuffer this

ByteBuffer#assert(assert)

Enables or disables assertions of argument types and offsets. Assertions are enabled by default but you can opt to disable them if your code already makes sure that everything is valid.

Parameter Type Description
assert boolean true to enable assertions, otherwise false
@returns !ByteBuffer this

ByteBuffer#capacity()

Gets the capacity of this ByteBuffer's backing buffer.

Parameter Type Description
@returns number Capacity of the backing buffer

ByteBuffer#clear()

Clears this ByteBuffer's offsets by setting ByteBuffer#offset to 0 and ByteBuffer#limit to the backing buffer's capacity. Discards ByteBuffer#markedOffset.

Parameter Type Description
@returns !ByteBuffer this

ByteBuffer#clone(copy=)

Creates a cloned instance of this ByteBuffer, preset with this ByteBuffer's values for ByteBuffer#offset, ByteBuffer#markedOffset and ByteBuffer#limit.

Parameter Type Description
copy boolean Whether to copy the backing buffer or to return another view on the same, defaults to false
@returns !ByteBuffer Cloned instance

ByteBuffer#compact(begin=, end=)

Compacts this ByteBuffer to be backed by a ByteBuffer#buffer of its contents' length. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will set offset = 0 and limit = capacity and adapt ByteBuffer#markedOffset to the same relative position if set.

Parameter Type Description
begin number Offset to start at, defaults to ByteBuffer#offset
end number Offset to end at, defaults to ByteBuffer#limit
@returns !ByteBuffer this

ByteBuffer#copy(begin=, end=)

Creates a copy of this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.

Parameter Type Description
begin number Begin offset, defaults to ByteBuffer#offset.
end number End offset, defaults to ByteBuffer#limit.
@returns !ByteBuffer Copy

ByteBuffer#copyTo(target, targetOffset=, sourceOffset=, sourceLimit=)

Copies this ByteBuffer's contents to another ByteBuffer. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.

Parameter Type Description
target !ByteBuffer Target ByteBuffer
targetOffset number Offset to copy to. Will use and increase the target's ByteBuffer#offset by the number of bytes copied if omitted.
sourceOffset number Offset to start copying from. Will use and increase ByteBuffer#offset by the number of bytes copied if omitted.
sourceLimit number Offset to end copying from, defaults to ByteBuffer#limit
@returns !ByteBuffer this

ByteBuffer#ensureCapacity(capacity)

Makes sure that this ByteBuffer is backed by a ByteBuffer#buffer of at least the specified capacity. If the current capacity is exceeded, it will be doubled. If double the current capacity is less than the required capacity, the required capacity will be used instead.

Parameter Type Description
capacity number Required capacity
@returns !ByteBuffer this

ByteBuffer#fill(value, begin=, end=)

Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit.

Parameter Type Description
value number | string Byte value to fill with. If given as a string, the first character is used.
begin number Begin offset. Will use and increase ByteBuffer#offset by the number of bytes written if omitted. defaults to ByteBuffer#offset.
end number End offset, defaults to ByteBuffer#limit.
@returns !ByteBuffer this

ByteBuffer#flip()

Makes this ByteBuffer ready for a new sequence of write or relative read operations. Sets limit = offset and offset = 0. Make sure always to flip a ByteBuffer when all relative read or write operations are complete.

Parameter Type Description
@returns !ByteBuffer this

ByteBuffer#mark(offset=)

Marks an offset on this ByteBuffer to be used later.

Parameter Type Description
offset number Offset to mark. Defaults to ByteBuffer#offset.
@returns !ByteBuffer this
@throws TypeError If offset is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#order(littleEndian)

Sets the byte order.

Parameter Type Description
littleEndian boolean true for little endian byte order, false for big endian
@returns !ByteBuffer this

ByteBuffer#prepend(source, encoding=, offset=)

Prepends some data to this ByteBuffer. This will overwrite any contents before the specified offset up to the prepended data's length. If there is not enough space available before the specified offset, the backing buffer will be resized and its contents moved accordingly.

Parameter Type Description
source !ByteBuffer | string | !ArrayBuffer Data to prepend. If source is a ByteBuffer, its offset will be modified according to the performed read operation.
encoding string | number Encoding if data is a string ("base64", "hex", "binary", defaults to "utf8")
offset number Offset to prepend at. Will use and decrease ByteBuffer#offset by the number of bytes prepended if omitted.
@returns !ByteBuffer this

ByteBuffer#prependTo(target, offset=)

Prepends this ByteBuffer to another ByteBuffer. This will overwrite any contents before the specified offset up to the prepended data's length. If there is not enough space available before the specified offset, the backing buffer will be resized and its contents moved accordingly.

Parameter Type Description
target !ByteBuffer Target ByteBuffer
offset number Offset to prepend at. Will use and decrease ByteBuffer#offset by the number of bytes prepended if omitted.
@returns !ByteBuffer this

ByteBuffer#printDebug(out)

Prints debug information about this ByteBuffer's contents.

Parameter Type Description
out function(string) Output function to call, defaults to console.log

ByteBuffer#readByte(offset=)

Reads an 8bit signed integer. This is an alias of ByteBuffer#readInt8.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns number Value read

ByteBuffer#readCString(offset=)

Reads a NULL-terminated UTF8 encoded string. For this to work the string read must not contain any NULL characters itself.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns string | !{string: string, length: number} The string read if offset is omitted, else the string read and the actual number of bytes read.

ByteBuffer#readDouble(offset=)

Reads a 64bit float. This is an alias of ByteBuffer#readFloat64.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns number

ByteBuffer#readFloat(offset=)

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

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns number

ByteBuffer#readFloat32(offset=)

Reads a 32bit float.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns number

ByteBuffer#readFloat64(offset=)

Reads a 64bit float.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns number

ByteBuffer#readIString(offset=)

Reads a length as uint32 prefixed UTF8 encoded string.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns string | !{string: string, length: number} The string read if offset is omitted, else the string read and the actual number of bytes read.

ByteBuffer#readInt(offset=)

Reads a 32bit signed integer. This is an alias of ByteBuffer#readInt32.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 4 if omitted.
@returns number Value read

ByteBuffer#readInt16(offset=)

Reads a 16bit signed integer.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 2 if omitted.
@returns number Value read
@throws TypeError If offset is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#readInt32(offset=)

Reads a 32bit signed integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns number Value read

ByteBuffer#readInt64(offset=)

Reads a 64bit signed integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !Long

ByteBuffer#readInt8(offset=)

Reads an 8bit signed integer.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns number Value read

ByteBuffer#readLong(offset=)

Reads a 64bit signed integer. This is an alias of ByteBuffer#readInt64.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !Long

ByteBuffer#readShort(offset=)

Reads a 16bit signed integer. This is an alias of ByteBuffer#readInt16.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 2 if omitted.
@returns number Value read
@throws TypeError If offset is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#readString(length, metrics=, offset=)

Reads an UTF8 encoded string. This is an alias of ByteBuffer#readUTF8String.

Parameter Type Description
length number Number of characters or bytes to read
metrics number Metrics specifying what n is meant to count. Defaults to ByteBuffer.METRICS_CHARS.
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns string | !{string: string, length: number} The string read if offset is omitted, else the string read and the actual number of bytes read.

ByteBuffer#readUTF8String(length, metrics=, offset=)

Reads an UTF8 encoded string.

Parameter Type Description
length number Number of characters or bytes to read
metrics number Metrics specifying what n is meant to count. Defaults to ByteBuffer.METRICS_CHARS.
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns string | !{string: string, length: number} The string read if offset is omitted, else the string read and the actual number of bytes read.

ByteBuffer#readUint16(offset=)

Reads a 16bit unsigned integer.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 2 if omitted.
@returns number Value read
@throws TypeError If offset is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#readUint32(offset=)

Reads a 32bit unsigned integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns number Value read

ByteBuffer#readUint64(offset=)

Reads a 64bit unsigned integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !Long

ByteBuffer#readUint8(offset=)

Reads an 8bit unsigned integer.

Parameter Type Description
offset number Offset to read from. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns number Value read

ByteBuffer#readVString(offset=)

Reads a length as varint32 prefixed UTF8 encoded string.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns string | !{string: string, length: number} The string read if offset is omitted, else the string read and the actual number of bytes read.

ByteBuffer#readVarint32(offset=)

Reads a 32bit base 128 variable-length integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns number | !{value: number, length: number} The value read if offset is omitted, else the value read and the actual number of bytes read.
@throws Error If it's not a valid varint

ByteBuffer#readVarint32ZigZag(offset=)

Reads a zig-zag encoded 32bit base 128 variable-length integer.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns number | !{value: number, length: number} The value read if offset is omitted, else the value read and the actual number of bytes read.
@throws Error If it's not a valid varint

ByteBuffer#readVarint64(offset=)

Reads a 64bit base 128 variable-length integer. Requires Long.js.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns !Long | !{value: Long, length: number} The value read if offset is omitted, else the value read and the actual number of bytes read.
@throws Error If it's not a valid varint

ByteBuffer#readVarint64ZigZag(offset=)

Reads a zig-zag encoded 64bit base 128 variable-length integer. Requires Long.js.

Parameter Type Description
offset number Offset to read from. Will use and increase ByteBuffer#offset by the number of bytes read if omitted.
@returns !Long | !{value: Long, length: number} The value read if offset is omitted, else the value read and the actual number of bytes read.
@throws Error If it's not a valid varint

ByteBuffer#remaining()

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

Parameter Type Description
@returns number Remaining readable bytes. May be negative if offset > limit.

ByteBuffer#reset()

Resets this ByteBuffer's ByteBuffer#offset. If an offset has been marked through ByteBuffer#mark before, offset will be set to ByteBuffer#markedOffset, which will then be discarded. If no offset has been marked, sets offset = 0.

Parameter Type Description
@returns !ByteBuffer this

ByteBuffer#resize(capacity)

Resizes this ByteBuffer to be backed by a buffer of at least the given capacity. Will do nothing if already that large or larger.

Parameter Type Description
capacity number Capacity required
@returns !ByteBuffer this
@throws TypeError If capacity is not a number
@throws RangeError If capacity < 0

ByteBuffer#reverse(begin=, end=)

Reverses this ByteBuffer's contents.

Parameter Type Description
begin number Offset to start at, defaults to ByteBuffer#offset
end number Offset to end at, defaults to ByteBuffer#limit
@returns !ByteBuffer this

ByteBuffer#skip(length)

Skips the next length bytes. This will just advance

Parameter Type Description
length number Number of bytes to skip. May also be negative to move the offset back.
@returns !ByteBuffer this

ByteBuffer#slice(begin=, end=)

Slices this ByteBuffer by creating a cloned instance with offset = begin and limit = end.

Parameter Type Description
begin number Begin offset, defaults to ByteBuffer#offset.
end number End offset, defaults to ByteBuffer#limit.
@returns !ByteBuffer Clone of this ByteBuffer with slicing applied, backed by the same ByteBuffer#buffer

ByteBuffer#toArrayBuffer(forceCopy=)

Returns a raw buffer compacted to contain this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will transparently ByteBuffer#flip this ByteBuffer if offset > limit but the actual offsets remain untouched. This is an alias of ByteBuffer#toBuffer.

Parameter Type Description
forceCopy boolean If true returns a copy, otherwise returns a view referencing the same memory. Defaults to false
@returns !ArrayBuffer Contents as an ArrayBuffer

ByteBuffer#toBase64(begin=, end=)

Encodes this ByteBuffer's contents to a base64 encoded string.

Parameter Type Description
begin number Offset to begin at, defaults to ByteBuffer#offset.
end number Offset to end at, defaults to ByteBuffer#limit.
@returns string Base64 encoded string

ByteBuffer#toBinary(begin=, end=)

Encodes this ByteBuffer to a binary encoded string, that is using only characters 0x00-0xFF as bytes.

Parameter Type Description
begin number Offset to begin at. Defaults to ByteBuffer#offset.
end number Offset to end at. Defaults to ByteBuffer#limit.
@returns string Binary encoded string
@throws RangeError If offset > limit

ByteBuffer#toBuffer(forceCopy=)

Returns a copy of the backing buffer that contains this ByteBuffer's contents. Contents are the bytes between ByteBuffer#offset and ByteBuffer#limit. Will transparently ByteBuffer#flip this ByteBuffer if offset > limit but the actual offsets remain untouched.

Parameter Type Description
forceCopy boolean If true returns a copy, otherwise returns a view referencing the same memory if possible. Defaults to false
@returns !ArrayBuffer Contents as an ArrayBuffer

ByteBuffer#toDebug(columns=)

Encodes this ByteBuffer to a hex encoded string with marked offsets. Offset symbols are:

  • < : offset,
  • ' : markedOffset,
  • > : limit,
  • | : offset and limit,
  • [ : offset and markedOffset,
  • ] : markedOffset and limit,
  • ! : offset, markedOffset and limit
Parameter Type Description
columns boolean If true returns two columns hex + ascii, defaults to false
@returns string | !Array.<string> Debug string or array of lines if asArray = true

ByteBuffer#toHex(begin=, end=)

Encodes this ByteBuffer's contents to a hex encoded string.

Parameter Type Description
begin number Offset to begin at. Defaults to ByteBuffer#offset.
end number Offset to end at. Defaults to ByteBuffer#limit.
@returns string Hex encoded string

ByteBuffer#toString(encoding=)

Converts the ByteBuffer's contents to a string.

Parameter Type Description
encoding string Output encoding. Returns an informative string representation if omitted but also allows direct conversion to "utf8", "hex", "base64" and "binary" encoding. "debug" returns a hex representation with highlighted offsets.
@returns string String representation
@throws Error If encoding is invalid

ByteBuffer#toUTF8()

Encodes this ByteBuffer's contents between ByteBuffer#offset and ByteBuffer#limit to an UTF8 encoded string.

Parameter Type Description
@returns string Hex encoded string
@throws RangeError If offset > limit

ByteBuffer#writeByte(value, offset=)

Writes an 8bit signed integer. This is an alias of ByteBuffer#writeInt8.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeCString(str, offset=)

Writes a NULL-terminated UTF8 encoded string. For this to work the specified string must not contain any NULL characters itself.

Parameter Type Description
str string String to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes contained in str + 1 if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written

ByteBuffer#writeDouble(value, offset=)

Writes a 64bit float. This is an alias of ByteBuffer#writeFloat64.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeFloat(value, offset=)

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

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeFloat32(value, offset=)

Writes a 32bit float.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 4 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeFloat64(value, offset=)

Writes a 64bit float.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeIString(str, offset=)

Writes a length as uint32 prefixed UTF8 encoded string.

Parameter Type Description
str string String to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written

ByteBuffer#writeInt(value, offset=)

Writes a 32bit signed integer. This is an alias of ByteBuffer#writeInt32.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 4 if omitted.

ByteBuffer#writeInt16(value, offset=)

Writes a 16bit signed integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 2 if omitted.
@throws TypeError If offset or value is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#writeInt32(value, offset=)

Writes a 32bit signed integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 4 if omitted.

ByteBuffer#writeInt64(value, offset=)

Writes a 64bit signed integer.

Parameter Type Description
value number | !Long Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeInt8(value, offset=)

Writes an 8bit signed integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeLong(value, offset=)

Writes a 64bit signed integer. This is an alias of ByteBuffer#writeInt64.

Parameter Type Description
value number | !Long Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeShort(value, offset=)

Writes a 16bit signed integer. This is an alias of ByteBuffer#writeInt16.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 2 if omitted.
@throws TypeError If offset or value is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#writeString(str, offset=)

Writes an UTF8 encoded string. This is an alias of ByteBuffer#writeUTF8String.

Parameter Type Description
str string String to write
offset number Offset to write to. Will use and increase ByteBuffer#offset if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written.

ByteBuffer#writeUTF8String(str, offset=)

Writes an UTF8 encoded string.

Parameter Type Description
str string String to write
offset number Offset to write to. Will use and increase ByteBuffer#offset if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written.

ByteBuffer#writeUint16(value, offset=)

Writes a 16bit unsigned integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 2 if omitted.
@throws TypeError If offset or value is not a valid number
@throws RangeError If offset is out of bounds

ByteBuffer#writeUint32(value, offset=)

Writes a 32bit unsigned integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 4 if omitted.

ByteBuffer#writeUint64(value, offset=)

Writes a 64bit unsigned integer.

Parameter Type Description
value number | !Long Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by 8 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeUint8(value, offset=)

Writes an 8bit unsigned integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and advance ByteBuffer#offset by 1 if omitted.
@returns !ByteBuffer this

ByteBuffer#writeVString(str, offset=)

Writes a length as varint32 prefixed UTF8 encoded string.

Parameter Type Description
str string String to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written

ByteBuffer#writeVarint32(value, offset=)

Writes a 32bit base 128 variable-length integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written

ByteBuffer#writeVarint32ZigZag(value, offset=)

Writes a zig-zag encoded 32bit base 128 variable-length integer.

Parameter Type Description
value number Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written

ByteBuffer#writeVarint64(value, offset=)

Writes a 64bit base 128 variable-length integer.

Parameter Type Description
value number | Long Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written.

ByteBuffer#writeVarint64ZigZag(value, offset=)

Writes a zig-zag encoded 64bit base 128 variable-length integer.

Parameter Type Description
value number | Long Value to write
offset number Offset to write to. Will use and increase ByteBuffer#offset by the number of bytes written if omitted.
@returns !ByteBuffer | number this if offset is omitted, else the actual number of bytes written.

Generated with doco v0.3.0

Clone this wiki locally