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

doco

ByteBuffer()

Constructs a new ByteBuffer.

Name Type Description
capacity* number Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
littleEndian* boolean Whether to use little or big endian byte order. Defaults to
noAssert* boolean Whether to skip assertions of offsets and values. Defaults to

this.buffer

Backing buffer.

this.view

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

this.offset

Absolute read/write offset.

this.markedOffset

Marked offset.

this.limit

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

this.littleEndian

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

this.noAssert

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

ByteBuffer.VERSION

ByteBuffer version.

ByteBuffer.LITTLE_ENDIAN

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

ByteBuffer.BIG_ENDIAN

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

ByteBuffer.DEFAULT_CAPACITY

Default initial capacity of 16.

ByteBuffer.DEFAULT_ENDIAN

Default endianess of false for big endian.

ByteBuffer.DEFAULT_NOASSERT

Default no assertions flag of false.

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.

EMPTY_BUFFER

@type {!ArrayBuffer}

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

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

Name Type Description
capacity* number Initial capacity. Defaults to {@link ByteBuffer.DEFAULT_CAPACITY}.
littleEndian* boolean Whether to use little or big endian byte order. Defaults to
noAssert* boolean Whether to skip assertions of offsets and values. Defaults to
returns !ByteBuffer

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

Concatenates multiple ByteBuffers into one.

Name Type Description
buffers !Array.<!ByteBuffer ¦ !ArrayBuffer !Uint8Array
encoding* (string ¦ boolean) String encoding if buffers contains a string ("base64", "hex", "binary",
littleEndian* boolean Whether to use little or big endian byte order for the resulting ByteBuffer. Defaults
noAssert* boolean Whether to skip assertions of offsets and values for the resulting ByteBuffer. Defaults to
returns !ByteBuffer Concatenated ByteBuffer

ByteBuffer.isByteBuffer(bb)

Tests if the specified type is a ByteBuffer.

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

ByteBuffer.type()

Gets the backing buffer type.

Name 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 {@link ByteBuffer#offset} to 0 and its {@link ByteBuffer#limit} to the length of the wrapped data.

Name Type Description
buffer !ByteBuffer ¦ !ArrayBuffer !Uint8Array
encoding* (string ¦ boolean) String encoding if buffer is a string ("base64", "hex", "binary", defaults to
littleEndian* boolean Whether to use little or big endian byte order. Defaults to
noAssert* boolean Whether to skip assertions of offsets and values. Defaults to
returns !ByteBuffer A ByteBuffer wrapping buffer

ByteBuffer.writeInt8(value, offset*)

Writes an 8bit signed integer.

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

ByteBuffer.prototype.writeByte

Writes an 8bit signed integer. This is an alias of {@link ByteBuffer#writeInt8}.

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

ByteBuffer.readInt8(offset*)

Reads an 8bit signed integer.

Name Type Description
offset* number Offset to read from. Will use and advance {@link ByteBuffer#offset} by 1 if omitted.
returns number Value read

ByteBuffer.prototype.readByte

Reads an 8bit signed integer. This is an alias of {@link ByteBuffer#readInt8}.

Name Type Description
offset* number Offset to read from. Will use and advance {@link ByteBuffer#offset} by 1 if omitted.
returns number Value read

ByteBuffer.writeUint8(value, offset*)

Writes an 8bit unsigned integer.

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

ByteBuffer.readUint8(offset*)

Reads an 8bit unsigned integer.

Name Type Description
offset* number Offset to read from. Will use and advance {@link ByteBuffer#offset} by 1 if omitted.
returns number Value read

ByteBuffer.writeInt16(value, offset*)

Writes a 16bit signed integer.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and advance {@link 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.prototype.writeShort

Writes a 16bit signed integer. This is an alias of {@link ByteBuffer#writeInt16}.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and advance {@link 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.readInt16(offset*)

Reads a 16bit signed integer.

Name Type Description
offset* number Offset to read from. Will use and advance {@link 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.prototype.readShort

Reads a 16bit signed integer. This is an alias of {@link ByteBuffer#readInt16}.

Name Type Description
offset* number Offset to read from. Will use and advance {@link 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.writeUint16(value, offset*)

Writes a 16bit unsigned integer.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and advance {@link 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.readUint16(offset*)

Reads a 16bit unsigned integer.

Name Type Description
offset* number Offset to read from. Will use and advance {@link 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.writeInt32(value, offset*)

Writes a 32bit signed integer.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.

ByteBuffer.prototype.writeInt

Writes a 32bit signed integer. This is an alias of {@link ByteBuffer#writeInt32}.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.

ByteBuffer.readInt32(offset*)

Reads a 32bit signed integer.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.
returns number Value read

ByteBuffer.prototype.readInt

Reads a 32bit signed integer. This is an alias of {@link ByteBuffer#readInt32}.

Name Type Description
offset* number Offset to read from. Will use and advance {@link ByteBuffer#offset} by 4 if omitted.
returns number Value read

ByteBuffer.writeUint32(value, offset*)

Writes a 32bit unsigned integer.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.

ByteBuffer.readUint32(offset*)

Reads a 32bit unsigned integer.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.
returns number Value read

ByteBuffer.writeInt64(value, offset*)

Writes a 64bit signed integer.

Name Type Description
value number ¦ !Long Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !ByteBuffer this

ByteBuffer.prototype.writeLong

Writes a 64bit signed integer. This is an alias of {@link ByteBuffer#writeInt64}.

Name Type Description
value number ¦ !Long Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !ByteBuffer this

ByteBuffer.readInt64(offset*)

Reads a 64bit signed integer.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !Long

ByteBuffer.prototype.readLong

Reads a 64bit signed integer. This is an alias of {@link ByteBuffer#readInt64}.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !Long

ByteBuffer.writeUint64(value, offset*)

Writes a 64bit unsigned integer.

Name Type Description
value number ¦ !Long Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !ByteBuffer this

ByteBuffer.readUint64(offset*)

Reads a 64bit unsigned integer.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns !Long

ByteBuffer.writeFloat32(value, offset*)

Writes a 32bit float.

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

ByteBuffer.prototype.writeFloat

Writes a 32bit float. This is an alias of {@link ByteBuffer#writeFloat32}.

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

ByteBuffer.readFloat32(offset*)

Reads a 32bit float.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.
returns number

ByteBuffer.prototype.readFloat

Reads a 32bit float. This is an alias of {@link ByteBuffer#readFloat32}.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 4 if omitted.
returns number

ByteBuffer.writeFloat64(value, offset*)

Writes a 64bit float.

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

ByteBuffer.prototype.writeDouble

Writes a 64bit float. This is an alias of {@link ByteBuffer#writeFloat64}.

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

ByteBuffer.readFloat64(offset*)

Reads a 64bit float.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns number

ByteBuffer.prototype.readDouble

Reads a 64bit float. This is an alias of {@link ByteBuffer#readFloat64}.

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by 8 if omitted.
returns number

ByteBuffer.MAX_VARINT32_BYTES

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

ByteBuffer.calculateVarint32(value)

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

Name Type Description
value number Value to encode
returns number Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT32_BYTES}

ByteBuffer.zigZagEncode32(n)

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

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

ByteBuffer.zigZagDecode32(n)

Decodes a zigzag encoded signed 32bit integer.

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

ByteBuffer.writeVarint32(value, offset*)

Writes a 32bit base 128 variable-length integer.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
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.

Name Type Description
value number Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
returns !ByteBuffer ¦ number this if offset is omitted, else the actual number of bytes written

ByteBuffer.readVarint32(offset*)

Reads a 32bit base 128 variable-length integer.

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

ByteBuffer.readVarint32ZigZag(offset*)

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

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

ByteBuffer.MAX_VARINT64_BYTES

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

ByteBuffer.calculateVarint64(value)

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

Name Type Description
value number ¦ !Long Value to encode
returns number Number of bytes required. Capped to {@link ByteBuffer.MAX_VARINT64_BYTES}

ByteBuffer.zigZagEncode64(value)

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

Name Type Description
value number ¦ !Long Signed long
returns !Long Unsigned zigzag encoded long

ByteBuffer.zigZagDecode64(value)

Decodes a zigzag encoded signed 64bit integer.

Name Type Description
value !Long ¦ number Unsigned zigzag encoded long or JavaScript number
returns !Long Signed long

ByteBuffer.writeVarint64(value, offset*)

Writes a 64bit base 128 variable-length integer.

Name Type Description
value number ¦ Long Value to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
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.

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

ByteBuffer.readVarint64(offset*)

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

Name Type Description
offset* number Offset to read from. Will use and increase {@link ByteBuffer#offset} by the number of bytes
returns !Long ¦ !{value: Long, length: number} The value read if offset is omitted, else the value read and
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.

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

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.

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

ByteBuffer.readCString(offset*)

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

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

ByteBuffer.writeIString(str, offset*)

Writes a length as uint32 prefixed UTF8 encoded string.

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

ByteBuffer.readIString(offset*)

Reads a length as uint32 prefixed UTF8 encoded string.

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

ByteBuffer.METRICS_CHARS

Metrics representing number of UTF8 characters. Evaluates to 1.

ByteBuffer.METRICS_BYTES

Metrics representing number of bytes. Evaluates to 2.

ByteBuffer.writeUTF8String(str, offset*)

Writes an UTF8 encoded string.

Name Type Description
str string String to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
returns !ByteBuffer ¦ number this if offset is omitted, else the actual number of bytes written.

ByteBuffer.prototype.writeString

Writes an UTF8 encoded string. This is an alias of {@link ByteBuffer#writeUTF8String}.

Name Type Description
str string String to write
offset* number Offset to write to. Will use and increase {@link ByteBuffer#offset} if omitted.
returns !ByteBuffer ¦ number this if offset is omitted, else the actual number of bytes written.

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.

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

ByteBuffer.calculateUTF8Bytes

Calculates the number of UTF8 bytes of a string.

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

ByteBuffer.readUTF8String(length, metrics*, offset*)

Reads an UTF8 encoded string.

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

ByteBuffer.prototype.readString

Reads an UTF8 encoded string. This is an alias of {@link ByteBuffer#readUTF8String}.

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

ByteBuffer.writeVString(str, offset*)

Writes a length as varint32 prefixed UTF8 encoded string.

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

ByteBuffer.readVString(offset*)

Reads a length as varint32 prefixed UTF8 encoded string.

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

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.

Name Type Description
source !ByteBuffer ¦ !ArrayBuffer !Uint8Array
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 {@link ByteBuffer#offset} by the number of bytes
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.

Name Type Description
target !ByteBuffer Target ByteBuffer
offset* number Offset to append to. Will use and increase {@link ByteBuffer#offset} by the number of bytes
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.

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

ByteBuffer.capacity()

Gets the capacity of this ByteBuffer's backing buffer.

Name Type Description
returns number Capacity of the backing buffer

ByteBuffer.clear()

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

Name Type Description
returns !ByteBuffer this

ByteBuffer.clone(copy*)

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

Name 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 {@link ByteBuffer#buffer} of its contents' length. Contents are the bytes between {@link ByteBuffer#offset} and {@link ByteBuffer#limit}. Will set offset = 0 and limit = capacity and adapt {@link ByteBuffer#markedOffset} to the same relative position if set.

Name Type Description
begin* number Offset to start at, defaults to {@link ByteBuffer#offset}
end* number Offset to end at, defaults to {@link ByteBuffer#limit}
returns !ByteBuffer this

ByteBuffer.copy(begin*, end*)

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

Name Type Description
begin* number Begin offset, defaults to {@link ByteBuffer#offset}.
end* number End offset, defaults to {@link ByteBuffer#limit}.
returns !ByteBuffer Copy

ByteBuffer.copyTo(target, targetOffset*, sourceOffset*, sourceLimit*)

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

Name Type Description
target !ByteBuffer Target ByteBuffer
targetOffset* number Offset to copy to. Will use and increase the target's {@link ByteBuffer#offset}
sourceOffset* number Offset to start copying from. Will use and increase {@link ByteBuffer#offset} by the
sourceLimit* number Offset to end copying from, defaults to {@link ByteBuffer#limit}
returns !ByteBuffer this

ByteBuffer.ensureCapacity(capacity)

Makes sure that this ByteBuffer is backed by a {@link 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.

Name 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 {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.

Name 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 {@link ByteBuffer#offset} by the number of bytes
end* number End offset, defaults to {@link 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.

Name Type Description
returns !ByteBuffer this

ByteBuffer.mark(offset*)

Marks an offset on this ByteBuffer to be used later.

Name Type Description
offset* number Offset to mark. Defaults to {@link 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.

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

ByteBuffer.LE(littleEndian*)

Switches (to) little endian byte order.

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

ByteBuffer.BE(bigEndian*)

Switches (to) big endian byte order.

Name Type Description
bigEndian* boolean Defaults to true, otherwise uses little 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.

Name Type Description
source !ByteBuffer ¦ string !ArrayBuffer
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 {@link ByteBuffer#offset} by the number of bytes
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.

Name Type Description
target !ByteBuffer Target ByteBuffer
offset* number Offset to prepend at. Will use and decrease {@link ByteBuffer#offset} by the number of bytes
returns !ByteBuffer this

ByteBuffer.printDebug(out*)

Prints debug information about this ByteBuffer's contents.

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

ByteBuffer.remaining()

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

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

ByteBuffer.reset()

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

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

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

Name Type Description
begin* number Offset to start at, defaults to {@link ByteBuffer#offset}
end* number Offset to end at, defaults to {@link ByteBuffer#limit}
returns !ByteBuffer this

ByteBuffer.skip(length)

Skips the next length bytes. This will just advance

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

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

ByteBuffer.toBuffer(forceCopy*)

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

Name Type Description
forceCopy* boolean If true returns a copy, otherwise returns a view referencing the same memory if
returns !ArrayBuffer Contents as an ArrayBuffer

ByteBuffer.prototype.toArrayBuffer

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

Name Type Description
forceCopy* boolean If true returns a copy, otherwise returns a view referencing the same memory.
returns !ArrayBuffer Contents as an ArrayBuffer

ByteBuffer.toString(encoding*)

Converts the ByteBuffer's contents to a string.

Name Type Description
encoding* string Output encoding. Returns an informative string representation if omitted but also allows
returns string String representation
throws Error If encoding is invalid

B64

Base64 alphabet.

ByteBuffer.toBase64(begin*, end*)

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

Name Type Description
begin* number Offset to begin at, defaults to {@link ByteBuffer#offset}.
end* number Offset to end at, defaults to {@link ByteBuffer#limit}.
returns string Base64 encoded string

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

Decodes a base64 encoded string to a ByteBuffer.

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

ByteBuffer.btoa(str)

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

Name Type Description
str string Binary string
returns string Base64 encoded string

ByteBuffer.atob(b64)

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

Name Type Description
b64 string Base64 encoded string
returns string Binary string

ByteBuffer.toBinary(begin*, end*)

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

Name Type Description
begin* number Offset to begin at. Defaults to {@link ByteBuffer#offset}.
end* number Offset to end at. Defaults to {@link ByteBuffer#limit}.
returns string Binary encoded string
throws RangeError If offset > limit

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

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

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

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
Name Type Description
columns* boolean If true returns two columns hex + ascii, defaults to false
returns string ¦ !Array. Debug string or array of lines if asArray = true

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

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

Name 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
noAssert* boolean Whether to skip assertions of offsets and values. Defaults to
returns !ByteBuffer ByteBuffer

ByteBuffer.toHex(begin*, end*)

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

Name Type Description
begin* number Offset to begin at. Defaults to {@link ByteBuffer#offset}.
end* number Offset to end at. Defaults to {@link ByteBuffer#limit}.
returns string Hex encoded string

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

Decodes a hex encoded string to a ByteBuffer.

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

utf8_decode_char()

Decodes a single UTF8 character from the specified ByteBuffer. The ByteBuffer's offsets remain untouched.

Name Type Description
bb !ByteBuffer ByteBuffer to decode from
offset number Offset to start at
returns !{codePoint: number, length: number} Decoded char code and the number of bytes read

utf8_calc_char()

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

Name Type Description
codePoint number Code point to encode
returns number Number of bytes required to encode the specified code point

utf8_calc_string()

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

Name Type Description
str string String to calculate
returns number Number of bytes required

utf8_encode_char()

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

Name Type Description
codePoint number Code point to encode
bb !ByteBuffer ByteBuffer to encode to
offset number Offset to write to
returns number Number of bytes written

ByteBuffer.toUTF8()

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

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

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

Decodes an UTF8 encoded string to a ByteBuffer.

Name Type Description
str string String to decode
littleEndian* boolean Whether to use little or big endian byte order. Defaults to
noAssert* boolean Whether to skip assertions of offsets and values. Defaults to
returns !ByteBuffer ByteBuffer
Clone this wiki locally