Titanium.Codec
> Titanium.Codec

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.

  • 1.7
  • 1.7
  • 1.7
Defined By

Properties

Titanium.Codec
BIG_ENDIAN : Numberreadonly

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

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.

Titanium.Codec
: Stringreadonly
ASCII character encoding.. ...

ASCII character encoding..

Constant value:ascii

See also: ASCII on Wikipedia.

Titanium.Codec
: Stringreadonly
ISO 8859-1 (Latin-1) character encoding. ...

ISO 8859-1 (Latin-1) character encoding.

Constant value:iso-latin-1

See also: ISO/IEC 8859-1 on Wikipedia.

Titanium.Codec
: Stringreadonly
UTF-16 character encoding with default byte order. ...

UTF-16 character encoding with default byte order.

Constant value:utf16

See also: UTF-16/UCS2 on Wikipedia.

Titanium.Codec
: Stringreadonly
UTF-16 character encoding with big endian byte order. ...

UTF-16 character encoding with big endian byte order.

Constant value:utf16be

See also: UTF-16/UCS2 on Wikipedia.

Titanium.Codec
: Stringreadonly
UTF-16 character encoding with little endian byte order. ...

UTF-16 character encoding with little endian byte order.

Constant value:utf16le

See also: UTF-16/UCS2 on Wikipedia.

Titanium.Codec
: Stringreadonly
UTF-8 character encoding. ...

UTF-8 character encoding.

Constant value:utf8

See also: UTF-8 on Wikipedia.

Titanium.Codec
LITTLE_ENDIAN : Numberreadonly

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

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

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

Titanium.Codec
: Stringreadonly
8-bit integer encoding type. ...

8-bit integer encoding type.

Constant value:byte

Titanium.Codec
: Stringreadonly
64-bit double precision floating-point type. ...

64-bit double precision floating-point type.

Constant value:double

Titanium.Codec
: Stringreadonly
32-bit single precision floating-point type. ...

32-bit single precision floating-point type.

Constant value:float

Titanium.Codec
: Stringreadonly
32-bit integer encoding type. ...

32-bit integer encoding type.

Constant value:int

Titanium.Codec
: Stringreadonly
64-bit integer encoding type. ...

64-bit integer encoding type.

Constant value:long

Titanium.Codec
: Stringreadonly
16-bit integer encoding type. ...

16-bit integer encoding type.

Constant value:short

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

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

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

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Defined By

Methods

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

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

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Titanium.Codec
( options ) : Number
Decodes a number from the source buffer using the specified data type. ...

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

Examples

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

Returns

  • Number

    Number decoded from source.

Titanium.Codec
( options ) : String
Decodes the source buffer into a String using the supplied character set. ...

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.

Examples

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

Returns

  • String

    The decoded string

Titanium.Codec
( options ) : Number
Encodes a number and writes it to a buffer. ...

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.

Examples

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

Returns

  • Number

    Position after the encoded number in dest.

Titanium.Codec
( options ) : Number
Encodes a string into a series of bytes in a buffer using the specified character set. ...

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 numer 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.

Examples

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

Returns

  • Number

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

Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

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

Returns

  • void
Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Codec
( ) : Number
Get the OS native byte order (either BIG_ENDIAN or LITTLE_ENDIAN). ...

Get the OS native byte order (either BIG_ENDIAN or LITTLE_ENDIAN).

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

Returns

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

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

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

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

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void