# Titanium.Codec

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

Availability
1.7
1.7
9.2.0

# Overview

The Codec module can be used for encoding strings and numbers into Titanium.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.

# Methods

# decodeNumber

Availability
1.7
1.7
9.2.0
decodeNumber(options) Number

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

Parameters

Name Type Description
options DecodeNumberDict

Named parameters.

Returns

Number decoded from source.

Type
Number

# decodeString

Availability
1.7
1.7
9.2.0
decodeString(options) String

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.

Parameters

Name Type Description
options DecodeStringDict

Named parameters.

Returns

The decoded string

Type
String

# encodeNumber

Availability
1.7
1.7
9.2.0
encodeNumber(options) Number

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.

Parameters

Name Type Description
options EncodeNumberDict

Named parameters.

Returns

Position after the encoded number in dest.

Type
Number

# encodeString

Availability
1.7
1.7
9.2.0
encodeString(options) Number

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.

Parameters

Name Type Description
options Dictionary<EncodeStringDict>

Named parameters.

Returns

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

Type
Number

# getNativeByteOrder

Availability
1.7
1.7
9.2.0
getNativeByteOrder() Number

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

OS native byte order.

Type
Number

# Constants

# BIG_ENDIAN

Availability
1.7
1.7
9.2.0
BIG_ENDIAN :Number

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.


# CHARSET_ASCII

Availability
1.7
1.7
9.2.0
CHARSET_ASCII :String

ASCII character encoding..

See also: ASCII on Wikipedia.


# CHARSET_ISO_LATIN_1

Availability
1.7
1.7
9.2.0
CHARSET_ISO_LATIN_1 :String

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

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


# CHARSET_UTF16

Availability
1.7
1.7
9.2.0
CHARSET_UTF16 :String

UTF-16 character encoding with default byte order.

See also: UTF-16/UCS2 on Wikipedia.


# CHARSET_UTF16BE

Availability
1.7
1.7
9.2.0
CHARSET_UTF16BE :String

UTF-16 character encoding with big endian byte order.

See also: UTF-16/UCS2 on Wikipedia.


# CHARSET_UTF16LE

Availability
1.7
1.7
9.2.0
CHARSET_UTF16LE :String

UTF-16 character encoding with little endian byte order.

See also: UTF-16/UCS2 on Wikipedia.


# CHARSET_UTF8

Availability
1.7
1.7
9.2.0
CHARSET_UTF8 :String

UTF-8 character encoding.

See also: UTF-8 on Wikipedia.


# LITTLE_ENDIAN

Availability
1.7
1.7
9.2.0
LITTLE_ENDIAN :Number

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

Availability
1.7
1.7
9.2.0
TYPE_BYTE :String

8-bit integer encoding type.


# TYPE_DOUBLE

Availability
1.7
1.7
9.2.0
TYPE_DOUBLE :String

64-bit double precision floating-point type.


# TYPE_FLOAT

Availability
1.7
1.7
9.2.0
TYPE_FLOAT :String

32-bit single precision floating-point type.


# TYPE_INT

Availability
1.7
1.7
9.2.0
TYPE_INT :String

32-bit integer encoding type.


# TYPE_LONG

Availability
1.7
1.7
9.2.0
TYPE_LONG :String

64-bit integer encoding type.


# TYPE_SHORT

Availability
1.7
1.7
9.2.0
TYPE_SHORT :String

16-bit integer encoding type.