# buffer.Buffer
The Buffer class is a global type for dealing with binary data directly. It can be constructed in a variety of ways.
NOTE
This is an abstract type. Any object of this structure can be used where this type is used.
# Overview
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
# buffer
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 byteOffset for details.
# Methods
# compare
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
| Name | Type | Description |
|---|---|---|
target | buffer.Buffer | Uint8Array | A |
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
- Type
- Number
# copy
Copies data from a region of buf to a region in target, even if the target memory region overlaps with buf.
Parameters
| Name | Type | Description |
|---|---|---|
target | buffer.Buffer | Uint8Array | A |
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
The number of bytes copied.
- Type
- Number
# entries
Creates and returns an iterator of [index, byte] pairs from the contents of buf.
Returns
- Type
- Object
# equals
Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise. Equivalent to buf.compare(otherBuffer) === 0.
Parameters
| Name | Type | Description |
|---|---|---|
otherBuffer | buffer.Buffer | Uint8Array | A |
Returns
- Type
- Boolean
# fill
Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled.
Parameters
| Name | Type | Description |
|---|---|---|
value | String | Number | buffer.Buffer | Uint8Array | The value with which to fill |
offset | Number | Number of bytes to skip before starting to fill |
end | Number | Where to stop filling |
encoding | String | The encoding for |
Returns
A reference to buf.
- Type
- buffer.Buffer
# includes
Equivalent to buf.indexOf() !== -1.
Parameters
| Name | Type | Description |
|---|---|---|
value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
byteOffset | Number | Where to begin searching in |
encoding | String | If |
Returns
true if value was found in buf, false otherwise.
- Type
- Boolean
# indexOf
If value is:
- a string,
valueis interpreted according to the character encoding inencoding. - a
BufferorUint8Array, value will be used in its entirety. To compare a partialBuffer, use slice. - a number,
valuewill be interpreted as an unsigned 8-bit integer value between0and255.
Parameters
| Name | Type | Description |
|---|---|---|
value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
byteOffset | Number | Where to begin searching in |
encoding | String | If |
Returns
The index of the first occurrence of value in buf, or -1 if buf does not contain value.
- Type
- Number
# lastIndexOf
Identical to buf.indexOf(), except the last occurrence of value is found rather than the first occurrence.
Parameters
| Name | Type | Description |
|---|---|---|
value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
byteOffset | Number | Where to begin searching in |
encoding | String | If |
Returns
The index of the last occurrence of value in buf, or -1 if buf does not contain value.
- Type
- Number
# readDoubleBE
Reads a 64-bit, big-endian double from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readDoubleLE
Reads a 64-bit, little-endian double from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readFloatBE
Reads a 32-bit, big-endian float from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readFloatLE
Reads a 32-bit, little-endian float from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readInt16BE
Reads a signed 16-bit, big-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readInt16LE
Reads a signed 16-bit, little-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readInt32BE
Reads a signed 32-bit, big-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readInt32LE
Reads a signed 32-bit, little-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readInt8
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
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readIntBE
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
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
byteLength | Number | Number of bytes to read. Must satisfy |
Returns
- Type
- Number
# readIntLE
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
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
byteLength | Number | Number of bytes to read. Must satisfy |
Returns
- Type
- Number
# readUInt16BE
Reads an unsigned 16-bit, big-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readUInt16LE
Reads an unsigned 16-bit, little-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readUInt32BE
Reads an unsigned 32-bit, big-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readUInt32LE
Reads an unsigned 32-bit, little-endian integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readUInt8
Reads an unsigned 8-bit integer from buf at the specified offset.
Parameters
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
Returns
- Type
- Number
# readUIntBE
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
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
byteLength | Number | Number of bytes to read. Must satisfy |
Returns
- Type
- Number
# readUIntLE
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
| Name | Type | Description |
|---|---|---|
offset | Number | Number of bytes to skip before starting to read. Must satisfy |
byteLength | Number | Number of bytes to read. Must satisfy |
Returns
- Type
- Number
# slice
Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.
Parameters
| Name | Type | Description |
|---|---|---|
start | Number | here the new Buffer will start. |
end | Number | Where the new Buffer will end (not inclusive). |
Returns
- Type
- buffer.Buffer
# subarray
Returns a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.
Parameters
| Name | Type | Description |
|---|---|---|
start | Number | here the new Buffer will start. |
end | Number | Where the new Buffer will end (not inclusive). |
Returns
- Type
- buffer.Buffer
# swap16
Interprets buf as an array of unsigned 16-bit integers and swaps the byte order in-place.
Returns
- Type
- buffer.Buffer
# swap32
Interprets buf as an array of unsigned 32-bit integers and swaps the byte order in-place.
Returns
- Type
- buffer.Buffer
# swap64
Interprets buf as an array of unsigned 64-bit integers and swaps the byte order in-place.
Returns
- Type
- buffer.Buffer
# toJSON
Returns a JSON representation of buf. JSON.stringify() implicitly calls this function when stringifying a Buffer instance.
Returns
- Type
- Object
# toString
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
| Name | Type | Description |
|---|---|---|
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
- Type
- String
# values
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
- Type
- Object
# write
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
| Name | Type | Description |
|---|---|---|
string | String | String to write to |
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 of bytes written.
- Type
- Number
# writeDoubleBE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeDoubleLE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeFloatBE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeFloatLE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeInt16BE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeInt16LE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeInt32BE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeInt32LE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeInt8
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeIntBE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
byteLength | Number | Number of bytes to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeIntLE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
byteLength | Number | Number of bytes to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUInt16BE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUInt16LE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUInt32BE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUInt32LE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUInt8
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUIntBE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
byteLength | Number | Number of bytes to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number
# writeUIntLE
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
| Name | Type | Description |
|---|---|---|
value | Number | Number to be written to |
offset | Number | Number of bytes to skip before starting to write. Must satisfy |
byteLength | Number | Number of bytes to write. Must satisfy |
Returns
offset plus the number of bytes written.
- Type
- Number