Skip to content
Titanium SDK
unreleased
Versionmainunreleased

Titanium.Codec

moduleAndroidiPhoneiPadmacOSsince 1.7

Extends Titanium.ModuleTitanium.Proxy

A module for translating between primitive types and raw byte streams.

The Codec module can be used for encoding strings and numbers into Buffer objects, and decoding primitive types from buffers.

Byte Order

Multi-byte data can be stored in two different byte orders: big-endian or little-endian. In big-endian byte order, the most significant or highest-value byte is stored first. For example, the 4-byte integer 0xFEDCBA98 is made up of the bytes 0xFE, 0xDC, 0xBA and 0x98, from most-significant to least-significant.

If we represent a buffer as an array of byte values, a big-endian encoding of 0xFEDCBA98 would look like this:

[ 0xFE, 0xDC, 0xBA, 0x98 ]

In little-endian order, the bytes would be stored in this order:

[ 0x98, 0xBA, 0xDC, 0xFE ]

For 8-bit character encodings, including ASCII, Latin-1 and UTF-8, byte order is not significant: the text is a sequence of individual bytes.

For UTF-16, text is represented as a sequence of 16-bit values. For example, a capital T in UTF-16 is 0x0054, and lowercase i is 0x0069. If we encode the string "Ti" with UTF-16 in big-endian byte order, we get:

[ 0x00, 0x54, 0x00, 0x69 ]

In UTF-16 with little-endian byte order, "Ti" is encoded as:

[ 0x54, 0x00, 0x69, 0x00 ]

Note that the bytes for each character are stored least-significant byte first, but the order of the characters is unchanged.

Properties 17

apiName

Stringsince Android 3.2.0, iPhone 3.2.0, iPad 3.2.0, macOS 9.2.0read-only

Inherited from Titanium.Proxy

The name of the API that this proxy corresponds to.

BIG_ENDIAN

Numberread-only

Big endian (network) byte order -- that is, the most significant byte first.

See "Byte Order" in the main discussion of Titanium.Codec for more information.

bubbleParent

Booleansince Android 3.0.0, iPhone 3.0.0, iPad 3.0.0, macOS 9.2.0

Inherited from Titanium.Proxy

Indicates if the proxy will bubble an event to its parent.

Default: true

CHARSET_ASCII

Stringread-only

ASCII character encoding..

See also: ASCII on Wikipedia.

Value "ascii"

CHARSET_UTF16

Stringread-only

UTF-16 character encoding with default byte order.

See also: UTF-16/UCS2 on Wikipedia.

Value "utf16"

CHARSET_UTF16BE

Stringread-only

UTF-16 character encoding with big endian byte order.

See also: UTF-16/UCS2 on Wikipedia.

Value "utf16be"

CHARSET_UTF16LE

Stringread-only

UTF-16 character encoding with little endian byte order.

See also: UTF-16/UCS2 on Wikipedia.

Value "utf16le"

CHARSET_UTF8

Stringread-only

UTF-8 character encoding.

See also: UTF-8 on Wikipedia.

Value "utf8"

LITTLE_ENDIAN

Numberread-only

Little endian byte order -- that is, the least significant byte first.

See "Byte Order" in the main discussion of Titanium.Codec for more information.

TYPE_BYTE

Stringread-only

8-bit integer encoding type.

Value "byte"

TYPE_DOUBLE

Stringread-only

64-bit double precision floating-point type.

Value "double"

TYPE_FLOAT

Stringread-only

32-bit single precision floating-point type.

Value "float"

TYPE_INT

Stringread-only

32-bit integer encoding type.

Value "int"

TYPE_LONG

Stringread-only

64-bit integer encoding type.

Value "long"

TYPE_SHORT

Stringread-only

16-bit integer encoding type.

Value "short"

Methods 9

Inherited from Titanium.Proxy

Adds the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to invoke when the event is fired.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.

Decodes a number from the source buffer using the specified data type.

Takes a set of named parameters in the options argument.

Bytes are read from the source buffer and decoded as the specified data type, type.

Two optional parameters can also be specified in options:

  • If position is included in the options dictionary, reads data from the buffer starting at position.

  • If byteOrder is included in the options dictionary, the specified byte order is used -- otherwise, the native byte order is assumed.

Throws an exception if source is null, or position is greater than source.length

Decode a Double-Precision Floating Point Number

This sample decodes an 8-byte double-precision floating point number using the OS byte order.

var buffer = // a buffer containing (at least) one 8-byte floating-point number
var value = Ti.Codec.decodeNumber({
    source: buffer,
    type: Ti.Codec.TYPE_DOUBLE
});

Decode an Integer Specifying Start Position and Byte Order

This sample decodes a 4-byte integer starting at index position 10 in the buffer, using big endian encoding.

var buffer = // a buffer containing a 4-byte integer at position 10.
var value = Ti.Codec.decodeNumber({
      source: buffer,
      position: 10,
      type: Ti.Codec.TYPE_INT,
      byteOrder: Ti.Codec.BIG_ENDIAN
});
Parameters
ParametersTypeDescription
optionsDecodeNumberDict

Named parameters.

4 fields
byteOrder? Number

byte order to decode with.

position? Number

Index in the source buffer of the first byte of data to decode.

source Titanium.Buffer

Buffer to decode.

type String

The encoding type to use.

Returns Number

Number decoded from source.

Decodes the source buffer into a String using the supplied character set.

Takes a set of named parameters in the options argument.

Bytes are read from the source buffer and decoded as a string.

Several optional parameters can also be specified in options:

  • If position is specified, bytes are read from source starting at position.

  • If length is specified, no more than length bytes are read.

  • If charset is specified, it determines the character encoding used to decode the string. Otherwise, UTF-8 is assumed.

Throws an exception if charset is not a valid character set, source is null, or either position, length, or position+length is greater than source.length.

Decode a String

This sample decodes a string using the default character encoding.

var buffer = // a buffer containing an encoded string
var string = Ti.Codec.decodeString({ source: buffer });

Decode a String with a Specified Character Encoding

This sample decodes a string using the UTF-16 character encoding.

var buffer = // a buffer containing a UTF-16 encoded string
var string = Ti.Codec.decodeString({
  source: buffer,
  charset: Ti.Codec.CHARSET_UTF16
});
Parameters
ParametersTypeDescription
optionsDecodeStringDict

Named parameters.

4 fields
charset? String

Character set to use when encoding this string to bytes.

length? Number

Number of bytes to decode.

position? Number

Index in the source buffer of the first byte of data to decode.

source Titanium.Buffer

Buffer to decode.

Returns String

The decoded string

Encodes a number and writes it to a buffer.

Takes a set of named parameters passed in the options argument.

Encodes the number source into dest using the passed in data type.

Two optional parameters can also be specified in options:

  • If position is included in the options dictionary, writes the encoded number to the buffer starting at position.

  • If byteOrder is included in the options dictionary, the specified byte order is used -- otherwise, the native byte order is assumed.

Encode a Double-Precision Floating Point Value

This example encodes a double-precision floating point value in 8 bytes using the OS byte order.

var buffer = Ti.createBuffer({ length: 8 });
Ti.Codec.encodeNumber({
  source: 1.23456789,
  dest: buffer,
  type: Ti.Codec.TYPE_DOUBLE
});

Encode an Integer Specifying Start Position and Byte Order

This example encodes a 4-byte integer using big endian encoding, and writes it to the supplied buffer starting at index position 10.

var buffer = Ti.createBuffer({ length: 100 });
Ti.Codec.encodeNumber({
    source: 0x3456789a,
    dest: buffer,
    position: 10,
    type: Ti.Codec.TYPE_INT,
    byteOrder: Ti.Codec.BIG_ENDIAN
});
Parameters
ParametersTypeDescription
optionsEncodeNumberDict

Named parameters.

5 fields
byteOrder? Number

Byte order to encode with.

dest Titanium.Buffer

Destination buffer.

position? Number

Index in the dest buffer of the first byte of encoded data.

source Number

Number to encode.

type String

Encoding type to use.

Returns Number

Position after the encoded number in dest.

Encodes a string into a series of bytes in a buffer using the specified character set.

Takes a set of named parameters in the options argument.

The string is read from source and written to the buffer dest.

Several optional parameters can also be specified in options:

  • If charset is included, the string is encoded using the specified character encoding.

  • If destPosition is included, data is written into the buffer starting at the specified position.

  • If sourcePosition is included, a substring of the source string starting at the specified position is encoded.

  • If sourceLength is included, at most the specified number of characters are encoded.

Throws an exception if charset is not a valid character set, source is null, or either sourcePosition, sourceLength, or sourcePosition+sourceLength is greater than source.length.

Encode String with Default Character Encoding

This sample encodes a string using the default character encoding (UTF-8), then trims the buffer to the length of the encoded string.

var buffer = Ti.createBuffer({ length: 1024 });
var length = Ti.Codec.encodeString({
    source: "hello world",
    dest: buffer
});
buffer.length = length;

Encode Substring with Specified Character Encoding

This sample encodes the first 10 characters of a string using UTF-16 character encoding.

// (10 * 2) + BOM = 22
var buffer = Ti.createBuffer({ length: 22 });
Ti.Codec.encodeString({
    source: "jack jumped over the candle stick",
    sourceLength: 10,
    dest: buffer,
    charset: Ti.Codec.CHARSET_UTF16
});
Parameters
ParametersTypeDescription
optionsDictionary<EncodeStringDict>

Named parameters.

6 fields
charset? String

Character encoding to use when encoding this string to bytes.

dest Titanium.Buffer

Destination buffer.

destPosition? Number

Index in the dest buffer of the first byte of the encoded string.

source String

Source string to encode.

sourceLength? Number

Number of characters in source to encode.

sourcePosition? Number

Position in source to start encoding.

Returns Number

An index indicating the first byte in the destination buffer after the encoded string.

Inherited from Titanium.Proxy

Fires a synthesized event to any registered listeners.

Parameters
ParametersTypeDescription
nameString

Name of the event.

event?Dictionary

A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Inherited from Titanium.Proxy

Removes the specified callback as an event listener for the named event.

Parameters
ParametersTypeDescription
nameString

Name of the event.

callbackCallback<Titanium.Event>

Callback function to remove. Must be the same function passed to addEventListener.

4 fields
bubbles Boolean

True if the event will try to bubble up if possible.

cancelBubble Boolean

Set to true to stop the event from bubbling.

source Object

Source object that fired the event.

type String

Name of the event fired.