# Titanium.Buffer

> Buffer is a mutable, resizable container for raw data.

- Source: https://titaniumsdk.com/docs/sdk/12.6.0/Titanium.Buffer
- Titanium SDK version: 12.6.0
- Kind: proxy
- Platforms: Android, iPhone, iPad, macOS
- Since: Titanium SDK Android 1.7, iPhone 1.7, iPad 1.7, macOS 9.2.0
- Extends: Titanium.Proxy
- Inheritance: Titanium.Proxy

A `Buffer` works like a resizable array of byte values.

Use the [Titanium.createBuffer](https://titaniumsdk.com/docs/sdk/Titanium.md#createBuffer) method to create a buffer.

## Properties

### byteOrder

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Default: `OS native byte order.`

Byte order of this buffer.

Specify either [Titanium.Codec.BIG_ENDIAN](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md#BIG_ENDIAN) or [Titanium.Codec.LITTLE_ENDIAN](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md#LITTLE_ENDIAN).

### length

- Type: Number
- Platforms: Android, iPhone, iPad, macOS
- Default: `0 unless `value` is specified, in which case the length of the encoded data is used.`

Length of the buffer in bytes.

When `length` is modified the buffer will be re-sized while preserving existing data where
possible. If the new buffer length is smaller than the length of the existing data then the
existing data will be truncated down to the size of the new buffer.

Resizing a buffer by setting its length should be used sparingly due to the associated overhead.

### type

- Type: String
- Platforms: Android, iPhone, iPad, macOS
- Default: `For string values, defaults to <Titanium.Codec.CHARSET_UTF8>.`

The type of data encoding to use with `value`.

Required when `value` is a Number. For numbers, specify a `TYPE` constant
from [Titanium.Codec](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md), for example, [Titanium.Codec.TYPE_DOUBLE](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md#TYPE_DOUBLE).

When `value` is a String, specify a `CHARSET` constant from [Titanium.Codec](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md).

### value

- Type: Number | String
- Platforms: Android, iPhone, iPad, macOS

Data to be encoded.

If `value` is a Number, `type` must also be set. (This is simply a convenient way of
calling [Titanium.Codec.encodeString](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md#encodeString) or [Titanium.Codec.encodeNumber](https://titaniumsdk.com/docs/sdk/Titanium.Codec.md#encodeNumber).)

## Methods

### append

- Platforms: Android, iPhone, iPad, macOS

Appends `sourceBuffer` to the this buffer.

This buffer is grown dynamically to accommodate the additional data if need be.

If `sourceOffset` and `sourceLength` are specified, data from `sourceBuffer` is
copied starting at position `sourceOffset`, and no more than `sourceLength` bytes
are copied.

Parameters:

- `sourceBuffer` (Titanium.Buffer): Buffer to append to this buffer.
- `sourceOffset` (Number, optional): Offset to start reading buffer data from. If specified, `sourceLength` must also be specified. Default: `0`.
- `sourceLength` (Number, optional): Number of bytes to read from `sourceBuffer`. If specified, `sourceOffset` must also be specified. Default: `Length of `sourceBuffer`.`.

Returns: Number - The number of bytes appended.

### clear

- Platforms: Android, iPhone, iPad, macOS

Clears this buffer's contents but does not change the size of the buffer.

### clone

- Platforms: Android, iPhone, iPad, macOS

Creates a complete or partial copy of this buffer.

If called with no arguments, retuns a complete copy of the current buffer.

If `offset` and `length` are specified, creates a new buffer from the original
buffer contents starting at `offset` and ending at `offset`+`length`-1.

Parameters:

- `offset` (Number, optional): Offset of first byte of this buffer to clone. If specified, `length` must be specified as well. Default: `0`.
- `length` (Number, optional): Number of bytes of data to clone. Default: `Length of this buffer.`.

Returns: Titanium.Buffer

### copy

- Platforms: Android, iPhone, iPad, macOS

Copies data from `sourceBuffer` into the current buffer at `offset`.

Does not expand this buffer if there is not enough room  to accomodate the data
from `sourceBuffer`.

If `sourceOffset` and `sourceLength` are specified, bytes are copied from
`sourceBuffer` starting at `sourceOffset`, and a maximum of `sourceLength` bytes
are copied.

Parameters:

- `sourceBuffer` (Titanium.Buffer): Buffer to copy data from.
- `offset` (Number): Offset in this buffer for the first byte of the copied data.
- `sourceOffset` (Number, optional): Offset in `sourceBuffer` to copy data from. If specified, `sourceLength` must be specified as well. Default: `0`.
- `sourceLength` (Number, optional): Number of bytes to copy from `sourceBuffer`. If specified, `sourceOffset` must be specified as well. Default: `Length of source buffer.`.

Returns: Number - The number of bytes copied.

### fill

- Platforms: Android, iPhone, iPad, macOS

Fills this buffer with the specified byte value.

Fills the buffer by setting byte values to `fillByte`.

If `offset` and `length` are specified, fills the buffer starting at
`offset` and ending at `offset`+`length`-1.

If `offset` and `length` are omitted, fills the entire buffer.

Parameters:

- `fillByte` (Number): Byte to fill buffer with.
- `offset` (Number, optional): Offset of the first byte to set to `fillByte`. Default: `0`.
- `length` (Number, optional): Number of bytes to fill. Default: `Buffer length.`.

### insert

- Platforms: Android, iPhone, iPad, macOS

Inserts data from `sourceBuffer` into this buffer at `offset`.

Buffer is grown to accommodate the new data.

If `sourceOffset` and `sourceLength` are specified, `sourceLength` bytes
of data are copied from `sourceBuffer`, with the first byte at `sourceOffset`.

Parameters:

- `sourceBuffer` (Titanium.Buffer): Buffer holding data to insert into this buffer.
- `offset` (Number): Offset in this buffer to insert the first byte copied from `sourceBuffer`.
- `sourceOffset` (Number, optional): Offset in `sourceBuffer` of the first byte to insert. If specified, `sourceLength` must be specified as well. Default: `0`.
- `sourceLength` (Number, optional): Number of bytes to insert from `sourceBuffer`. If specified, `sourceOffset` must be specified as well. Default: `Length of source buffer.`.

Returns: Number - The number of bytes inserted.

### release

- Platforms: Android, iPhone, iPad, macOS

Releases the space allocated to the buffer, and sets its length to 0.

This is effectively the same as `buffer.length = 0`.

### toBlob

- Platforms: Android, iPhone, iPad, macOS

Converts this buffer to a [Titanium.Blob](https://titaniumsdk.com/docs/sdk/Titanium.Blob.md).

Returns: Titanium.Blob

### toString

- Platforms: Android, iPhone, iPad, macOS

Converts this buffer to a String.

Returns: String

## Inherited members

7 members are inherited. Each is documented on the type that declares it.

- [Titanium.Proxy](https://titaniumsdk.com/docs/sdk/12.6.0/Titanium.Proxy.md): `addEventListener`, `apiName`, `applyProperties`, `bubbleParent`, `fireEvent`, `lifecycleContainer`, `removeEventListener`

---

- This corpus documents Titanium SDK 13.4.1. Anything here that does not name a version describes 13.4.1.
- Titanium is maintained by TiDev. The Appcelerator toolchain is retired: there is no `appc` CLI, no Appcelerator Studio and no hosted build service. The supported CLI is `titanium` on npm, invoked as `ti`.

