Skip to content
Titanium SDK
unreleased
Versionmainunreleased

buffer.Buffer

pseudoAndroidiPhoneiPadmacOSsince 8.2.0

The Buffer class is a global type for dealing with binary data directly. It can be constructed in a variety of ways.

Note that this Buffer class is highly related to the Titanium.Buffer type. Both are wrappers around an underlying array of bytes and can be accessed in an array-like manner:

const value = buffer[index];
buffer[index] = 1;

In fact, this Node.js shim has been written to allow wrapping a Titanium.Buffer into a Node.js compatible instance:

const wrapped = Buffer.from(tiBuffer);

Note that this will result in a slow Buffer instance (because reads/writes pass-through to the underlying Ti.Buffer which in turn goes through the JS/native binding layer). If you only intend to read the data (or do not need to write back to the original Titanium.Buffer) you can perform a one-time copy of the underlying bytes to a faster JS-only Uint8Array:

const fastBuffer = Buffer.from(tiBuffer.toBlob().toArrayBuffer()); // here we're converting from Blob to Ti.Buffer to ArrayBuffer and the shim wraps that copy

Properties 3

buffer

ArrayBuffer

The underlying ArrayBuffer object based on which this Buffer object is created.

This ArrayBuffer is not guaranteed to correspond exactly to the original Buffer. See the notes on buffer.Buffer.byteOffset for details.

The byteOffset of the Buffers underlying ArrayBuffer object.

length

Number

Returns the number of bytes in buf.

Methods 54

Compares buf with target and returns a number indicating whether buf comes before, after, or is the same as target in sort order. Comparison is based on the actual sequence of bytes in each Buffer.

0 is returned if target is the same as buf 1 is returned if target should come before buf when sorted. -1 is returned if target should come after buf when sorted.

Parameters
ParametersTypeDescription
targetbuffer.Buffer | Uint8Array

A Buffer or Uint8Array with which to compare buf.

targetStart?Number

The offset within target at which to begin comparison.

targetEnd?Number

The offset within target at which to end comparison (not inclusive).

sourceStart?Number

The offset within buf at which to begin comparison.

sourceEnd?Number

The offset within buf at which to end comparison (not inclusive).

Returns Number

Copies data from a region of buf to a region in target, even if the target memory region overlaps with buf.

Parameters
ParametersTypeDescription
targetbuffer.Buffer | Uint8Array

A Buffer or Uint8Array with which to compare buf.

targetStart?Number

The offset within target at which to begin writing.

sourceStart?Number

The offset within buf at which to begin copying.

sourceEnd?Number

The offset within buf at which to stop copying (not inclusive).

Returns Number

The number of bytes copied.

Creates and returns an iterator of [index, byte] pairs from the contents of buf.

Returns Object

Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise. Equivalent to buf.compare(otherBuffer) === 0.

Parameters
ParametersTypeDescription
otherBufferbuffer.Buffer | Uint8Array

A Buffer or Uint8Array with which to compare buf.

Returns Boolean

Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled.

Parameters
ParametersTypeDescription
valueString | Number | buffer.Buffer | Uint8Array

The value with which to fill buf.

offset?Number

Number of bytes to skip before starting to fill buf.

end?Number

Where to stop filling buf (not inclusive).

encoding?String

The encoding for value if value is a string.

Returns buffer.Buffer

A reference to buf.

Equivalent to buf.indexOf() !== -1.

Parameters
ParametersTypeDescription
valueString | Number | buffer.Buffer | Uint8Array

What to search for.

byteOffset?Number

Where to begin searching in buf. If negative, then offset is calculated from the end of buf.

encoding?String

If value is a string, this is its encoding.

Returns Boolean

true if value was found in buf, false otherwise.

If value is:

  • a string, value is interpreted according to the character encoding in encoding.
  • a Buffer or Uint8Array, value will be used in its entirety. To compare a partial Buffer, use buf.slice().
  • a number, value will be interpreted as an unsigned 8-bit integer value between 0 and 255.
Parameters
ParametersTypeDescription
valueString | Number | buffer.Buffer | Uint8Array

What to search for.

byteOffset?Number

Where to begin searching in buf. If negative, then offset is calculated from the end of buf.

encoding?String

If value is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf.

Returns Number

The index of the first occurrence of value in buf, or -1 if buf does not contain value.

Creates and returns an iterator of buf keys (indices).

Returns Object

Identical to buf.indexOf(), except the last occurrence of value is found rather than the first occurrence.

Parameters
ParametersTypeDescription
valueString | Number | buffer.Buffer | Uint8Array

What to search for.

byteOffset?Number

Where to begin searching in buf. If negative, then offset is calculated from the end of buf.

encoding?String

If value is a string, this is the encoding used to determine the binary representation of the string that will be searched for in buf.

Returns Number

The index of the last occurrence of value in buf, or -1 if buf does not contain value.

Reads a 64-bit, big-endian double from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8.

Returns Number

Reads a 64-bit, little-endian double from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 8.

Returns Number

Reads a 32-bit, big-endian float from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads a 32-bit, little-endian float from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads a signed 16-bit, big-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

Reads a signed 16-bit, little-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

Reads a signed 32-bit, big-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads a signed 32-bit, little-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads a signed 8-bit integer from buf at the specified offset.

Integers read from a Buffer are interpreted as two's complement signed values.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1.

Returns Number

Reads byteLength number of bytes from buf at the specified offset and interprets the result as a big-endian, two's complement signed value supporting up to 48 bits of accuracy.

Parameters
ParametersTypeDescription
offsetNumber

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to read. Must satisfy 0 < byteLength <= 6.

Returns Number

Reads byteLength number of bytes from buf at the specified offset and interprets the result as a little-endian, two's complement signed value supporting up to 48 bits of accuracy.

Parameters
ParametersTypeDescription
offsetNumber

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to read. Must satisfy 0 < byteLength <= 6.

Returns Number

Reads an unsigned 16-bit, big-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

Reads an unsigned 16-bit, little-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

Reads an unsigned 32-bit, big-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads an unsigned 32-bit, little-endian integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

Reads an unsigned 8-bit integer from buf at the specified offset.

Parameters
ParametersTypeDescription
offset?Number

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - 1.

Returns Number

Reads byteLength number of bytes from buf at the specified offset and interprets the result as an unsigned big-endian value supporting up to 48 bits of accuracy.

Parameters
ParametersTypeDescription
offsetNumber

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to read. Must satisfy 0 < byteLength <= 6.

Returns Number

Reads byteLength number of bytes from buf at the specified offset and interprets the result as an unsigned little-endian value supporting up to 48 bits of accuracy.

Parameters
ParametersTypeDescription
offsetNumber

Number of bytes to skip before starting to read. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to read. Must satisfy 0 < byteLength <= 6.

Returns Number

Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.

Parameters
ParametersTypeDescription
start?Number

here the new Buffer will start.

end?Number

Where the new Buffer will end (not inclusive).

Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.

Parameters
ParametersTypeDescription
start?Number

here the new Buffer will start.

end?Number

Where the new Buffer will end (not inclusive).

Interprets buf as an array of unsigned 16-bit integers and swaps the byte order in-place.

Interprets buf as an array of unsigned 32-bit integers and swaps the byte order in-place.

Interprets buf as an array of unsigned 64-bit integers and swaps the byte order in-place.

Returns a JSON representation of buf. JSON.stringify() implicitly calls this function when stringifying a Buffer instance.

Returns Object

Decodes buf to a string according to the specified character encoding in encoding. start and end may be passed to decode only a subset of buf.

Parameters
ParametersTypeDescription
encoding?String

The character encoding to use.

start?Number

The byte offset to start decoding at.

end?Number

The byte offset to stop decoding at (not inclusive).

Returns String

Creates and returns an iterator for buf values (bytes). This function is called automatically when a Buffer is used in a for..of statement.

Returns Object

Writes string to buf at offset according to the character encoding in encoding. The length parameter is the number of bytes to write. If buf did not contain enough space to fit the entire string, only part of string will be written. However, partially encoded characters will not be written.

Parameters
ParametersTypeDescription
stringString

String to write to buf.

offset?Number

Number of bytes to skip before starting to write string.

length?Number

Maximum number of bytes to write (written bytes will not exceed buf.length - offset).

encoding?String

The character encoding of string.

Returns Number

Number of bytes written.

Writes value to buf at the specified offset as big-endian. The value must be a JavaScript number. Behavior is undefined when value is anything other than a JavaScript number.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 8.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset as little-endian. The value must be a JavaScript number. Behavior is undefined when value is anything other than a JavaScript number.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 8.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset as big-endian. The value must be a JavaScript number. Behavior is undefined when value is anything other than a JavaScript number.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset as little-endian. The value must be a JavaScript number. Behavior is undefined when value is anything other than a JavaScript number.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid signed 16-bit integer. Behavior is undefined when value is anything other than a signed 16-bit integer.

value is interpreted and written as a two's complement signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid signed 16-bit integer. Behavior is undefined when value is anything other than a signed 16-bit integer.

value is interpreted and written as a two's complement signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid signed 32-bit integer. Behavior is undefined when value is anything other than a signed 32-bit integer.

value is interpreted and written as a two's complement signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid signed 32-bit integer. Behavior is undefined when value is anything other than a signed 32-bit integer.

value is interpreted and written as a two's complement signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid signed 8-bit integer. Behavior is undefined when value is anything other than a signed 8-bit integer.

value is interpreted and written as a two's complement signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 1.

Returns Number

offset plus the number of bytes written.

Writes byteLength bytes of value to buf at the specified offset as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than a signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offsetNumber

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to write. Must satisfy 0 < byteLength <= 6.

Returns Number

offset plus the number of bytes written.

Writes byteLength bytes of value to buf at the specified offset as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than a signed integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offsetNumber

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to write. Must satisfy 0 < byteLength <= 6.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid unsigned 16-bit integer. Behavior is undefined when value is anything other than an unsigned 16-bit integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid unsigned 16-bit integer. Behavior is undefined when value is anything other than an unsigned 16-bit integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 2.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid unsigned 32-bit integer. Behavior is undefined when value is anything other than an unsigned 32-bit integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid unsigned 32-bit integer. Behavior is undefined when value is anything other than an unsigned 32-bit integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 4.

Returns Number

offset plus the number of bytes written.

Writes value to buf at the specified offset. value must be a valid unsigned 8-bit integer. Behavior is undefined when value is anything other than an unsigned 8-bit integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offset?Number

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - 1.

Returns Number

offset plus the number of bytes written.

Writes byteLength bytes of value to buf at the specified offset as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than an unsigned integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offsetNumber

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to write. Must satisfy 0 < byteLength <= 6.

Returns Number

offset plus the number of bytes written.

Writes byteLength bytes of value to buf at the specified offset as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when value is anything other than an unsigned integer.

Parameters
ParametersTypeDescription
valueNumber

Number to be written to buf.

offsetNumber

Number of bytes to skip before starting to write. Must satisfy 0 <= offset <= buf.length - byteLength.

byteLengthNumber

Number of bytes to write. Must satisfy 0 < byteLength <= 6.

Returns Number

offset plus the number of bytes written.