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

Class ByteBuffer

The swiss army knife for binary data in JavaScript.


<static> BIG_ENDIAN : boolean

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

<static> DEFAULT_CAPACITY : number

Default initial capacity of 16.

<static> DEFAULT_ENDIAN : boolean

Default endianess of false for big endian.

<static> DEFAULT_NOASSERT : boolean

Default no assertions flag of false.

<static> LITTLE_ENDIAN : boolean

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

<static> Long : 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.

<static> MAX_VARINT32_BYTES : number

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

<static> MAX_VARINT64_BYTES : number

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

<static> METRICS_BYTES : number

Metrics representing number of bytes. Evaluates to 2.

<static> METRICS_CHARS : number

Metrics representing number of UTF8 characters. Evaluates to 1.

<static> VERSION : string

ByteBuffer version.

<static> allocate(capacity=, littleEndian=, noAssert=) → !ByteBuffer

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

<static> atob(b64) → string

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

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

<static> btoa(str) → string

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

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

<static> calculateUTF8Bytes(str) → number

Calculates the number of UTF8 bytes of a string.

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

<static> calculateUTF8Chars(str) → number

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

<static> calculateVarint32(value) → number

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

<static> calculateVarint64(value) → number

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

<static> concat(buffers, encoding=, littleEndian=, noAssert=) → !ByteBuffer

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

<static> fromBase64(str, littleEndian=, noAssert=) → !ByteBuffer

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

<static> fromBinary(str, littleEndian=, noAssert=) → !ByteBuffer

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

<static> fromDebug(str, littleEndian=, noAssert=) → !ByteBuffer

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

<static> fromHex(str, littleEndian=, noAssert=) → !ByteBuffer

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

<static> fromUTF8(str, littleEndian=, noAssert=) → !ByteBuffer

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

<static> isByteBuffer(bb) → boolean

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

<static> type() → Function

Gets the backing buffer type.

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

<static> wrap(buffer, encoding=, littleEndian=, noAssert=) → !ByteBuffer

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

<static> zigZagDecode32(n) → number

Decodes a zigzag encoded signed 32bit integer.

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

<static> zigZagDecode64(value) → !Long

Decodes a zigzag encoded signed 64bit integer.

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

<static> zigZagEncode32(n) → number

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

<static> zigZagEncode64(value) → !Long

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

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.

buffer : !ArrayBuffer

Backing buffer.

limit : number

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

littleEndian : boolean

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

markedOffset : number

Marked offset.

noAssert : boolean

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

offset : number

Absolute read/write offset.

view : DataView

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

BE(bigEndian=) → !ByteBuffer

Switches (to) big endian byte order.

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

LE(littleEndian=) → !ByteBuffer

Switches (to) little endian byte order.

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

append(source, encoding=, offset=) → !ByteBuffer

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

appendTo(target, offset=) → !ByteBuffer

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

assert(assert) → !ByteBuffer

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

capacity() → number

Gets the capacity of this ByteBuffer's backing buffer.

Parameter Type Description
@returns number Capacity of the backing buffer

clear() → !ByteBuffer

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

clone(copy=) → !ByteBuffer

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

compact(begin=, end=) → !ByteBuffer

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

copy(begin=, end=) → !ByteBuffer

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

copyTo(target, targetOffset=, sourceOffset=, sourceLimit=) → !ByteBuffer

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

ensureCapacity(capacity) → !ByteBuffer

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

fill(value, begin=, end=) → !ByteBuffer

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

flip() → !ByteBuffer

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

mark(offset=) → !ByteBuffer

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

order(littleEndian) → !ByteBuffer

Sets the byte order.

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

prepend(source, encoding=, offset=) → !ByteBuffer

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

prependTo(target, offset=) → !ByteBuffer

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

printDebug(out)

Prints debug information about this ByteBuffer's contents.

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

readByte(offset=) → number

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

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

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.

readDouble(offset=) → number

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

readFloat(offset=) → number

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

readFloat32(offset=) → number

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

readFloat64(offset=) → number

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

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

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.

readInt(offset=) → number

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

readInt16(offset=) → number

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

readInt32(offset=) → number

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

readInt64(offset=) → !Long

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

readInt8(offset=) → number

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

readLong(offset=) → !Long

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

readShort(offset=) → number

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

readString(length, metrics=, offset=) → string | !{string: string, length: number}

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.

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

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.

readUint16(offset=) → number

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

readUint32(offset=) → number

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

readUint64(offset=) → !Long

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

readUint8(offset=) → number

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

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

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.

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

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.

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

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.

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

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.

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

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.

remaining() → number

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.

reset() → !ByteBuffer

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

resize(capacity) → !ByteBuffer

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

reverse(begin=, end=) → !ByteBuffer

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

skip(length) → !ByteBuffer

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

slice(begin=, end=) → !ByteBuffer

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

toArrayBuffer(forceCopy=) → !ArrayBuffer

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

toBase64(begin=, end=) → string

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

toBinary(begin=, end=) → string

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

toBuffer(forceCopy=) → !ArrayBuffer

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

toDebug(columns=) → string | !Array.<string>

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

toHex(begin=, end=) → string

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

toString(encoding=) → string

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

toUTF8() → string

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

Parameter Type Description
@returns string Hex encoded string

writeByte(value, offset=) → !ByteBuffer

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

writeCString(str, offset=) → !ByteBuffer | number

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

writeDouble(value, offset=) → !ByteBuffer

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

writeFloat(value, offset=) → !ByteBuffer

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

writeFloat32(value, offset=) → !ByteBuffer

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

writeFloat64(value, offset=) → !ByteBuffer

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

writeIString(str, offset=) → !ByteBuffer | number

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

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.

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.

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.

writeInt64(value, offset=) → !ByteBuffer

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

writeInt8(value, offset=) → !ByteBuffer

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

writeLong(value, offset=) → !ByteBuffer

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

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.

writeString(str, offset=) → !ByteBuffer | number

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.

writeUTF8String(str, offset=) → !ByteBuffer | number

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.

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.

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.

writeUint64(value, offset=) → !ByteBuffer

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

writeUint8(value, offset=) → !ByteBuffer

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

writeVString(str, offset=) → !ByteBuffer | number

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

writeVarint32(value, offset=) → !ByteBuffer | number

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

writeVarint32ZigZag(value, offset=) → !ByteBuffer | number

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

writeVarint64(value, offset=) → !ByteBuffer | number

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.

writeVarint64ZigZag(value, offset=) → !ByteBuffer | number

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.2.0

Clone this wiki locally