You are reading 12.5.1. See this page in 13.4.1.
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 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
ArrayBufferThe 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.
byteOffset
NumberThe byteOffset of the Buffers underlying ArrayBuffer object.
length
NumberReturns 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 | 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). |
Copies data from a region of buf to a region in target, even if the target memory region overlaps with buf.
| Parameters | 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). |
The number of bytes copied.
Creates and returns an iterator of [index, byte] pairs from the contents of buf.
Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise. Equivalent to buf.compare(otherBuffer) === 0.
| Parameters | Type | Description |
|---|---|---|
| otherBuffer | buffer.Buffer | Uint8Array | A |
Fills buf with the specified value. If the offset and end are not given, the entire buf will be filled.
| Parameters | 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 |
Equivalent to buf.indexOf() !== -1.
| Parameters | Type | Description |
|---|---|---|
| value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
| byteOffset? | Number | Where to begin searching in |
| encoding? | String | If |
true if value was found in buf, false otherwise.
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 buf.slice(). - a number,
valuewill be interpreted as an unsigned 8-bit integer value between0and255.
| Parameters | Type | Description |
|---|---|---|
| value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
| byteOffset? | Number | Where to begin searching in |
| encoding? | String | If |
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).
Identical to buf.indexOf(), except the last occurrence of value is found rather than the first occurrence.
| Parameters | Type | Description |
|---|---|---|
| value | String | Number | buffer.Buffer | Uint8Array | What to search for. |
| byteOffset? | Number | Where to begin searching in |
| encoding? | String | If |
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 | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a 64-bit, little-endian double from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a 32-bit, big-endian float from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a 32-bit, little-endian float from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a signed 16-bit, big-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a signed 16-bit, little-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a signed 32-bit, big-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads a signed 32-bit, little-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
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 | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
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 | Type | Description |
|---|---|---|
| offset | Number | Number of bytes to skip before starting to read. Must satisfy |
| byteLength | Number | Number of bytes to read. Must satisfy |
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 | Type | Description |
|---|---|---|
| offset | Number | Number of bytes to skip before starting to read. Must satisfy |
| byteLength | Number | Number of bytes to read. Must satisfy |
Reads an unsigned 16-bit, big-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads an unsigned 16-bit, little-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads an unsigned 32-bit, big-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads an unsigned 32-bit, little-endian integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
Reads an unsigned 8-bit integer from buf at the specified offset.
| Parameters | Type | Description |
|---|---|---|
| offset? | Number | Number of bytes to skip before starting to read. Must satisfy |
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 | Type | Description |
|---|---|---|
| offset | Number | Number of bytes to skip before starting to read. Must satisfy |
| byteLength | Number | Number of bytes to read. Must satisfy |
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 | 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 a new Buffer that references the same memory as the original, but offset and cropped by the start and end indices.
| Parameters | Type | Description |
|---|---|---|
| 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 | Type | Description |
|---|---|---|
| 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.
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 | 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). |
Creates and returns an iterator for buf values (bytes). This function is called automatically when a Buffer is used in a for..of statement.
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 | 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. |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | 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 |
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 | 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 |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | Type | Description |
|---|---|---|
| value | Number | Number to be written to |
| offset? | Number | Number of bytes to skip before starting to write. Must satisfy |
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 | 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 |
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 | 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 |
offset plus the number of bytes written.