Titanium Mobile Intro Series: Buffer and Codec

Colors In Bloom series. Artistic background made of fractal color textures for use with projects on imagination, creativity and design

Welcome to the first in a series of articles about some of the new features we’re releasing in Titanium Mobile 1.7.0. These articles are introductory in scope and provide links to additional resources related to the APIs to help give you a broader understanding of the feature.

This article is concerned with Titanium Buffer, a container for bytes, and Titanium.Codec, methods to convert to and from binary data. These two API additions are primarily used in conjunction with Titanium.Network.Socket and Titanium.Stream which will be introduced over the next few days.


Buffers are created from the Titanium module with Titanium.createBuffer(). Buffers are similar to byte arrays in other programming languages but have the ability to be resized. Buffers may be grown by setting the length property to a larger value, or when using the insert() or append() methods. Their length may also be reduced by setting the length property to a smaller value or using the release() method.



Applications that read and manipulate binary data also require the ability to encode and decode that data into easily recognizable primitive types. The Titanium.Codec module addresses this issue by exposing encoding, decoding, and byte order operations. The Codec module supports conversion of two basic primitive types: Numbers and Strings.

Numeric Conversions

Numeric conversion is provided for the following types: byte, short, int, long, float, and double.


String Conversions

String conversion is provided using the following character sets: ISO_LATIN1, UTF8, UTF16, UTF16BE, and UTF16LE. Titanium.Codec also suports both Big Endian and Little Endian byte ordering.


Buffer Creation With Encode

Titanium.createBuffer() has convenience options for creating a buffer from existing data and encoding it into the requested representation in the returned Buffer.


Additional Resources

Buffer & Codec Documentation