13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with byte arrays<br />

The ByteArray class includes several methods that make it easier to read from and write to a data stream. Some of these<br />

methods include readBytes() and writeBytes(), readInt() and writeInt(), readFloat() and writeFloat(),<br />

readObject() and writeObject(), and readUTFBytes() and writeUTFBytes(). These methods <strong>en</strong>able you to<br />

read data from the data stream into variables of specific data types and write from specific data types directly to the<br />

binary data stream.<br />

For example, the following code reads a simple array of strings and floating-point numbers and writes each elem<strong>en</strong>t<br />

to a ByteArray. The organization of the array allows the code to call the appropriate ByteArray methods<br />

(writeUTFBytes() and writeFloat()) to write the data. The repeating data pattern makes it possible to read the<br />

array with a loop.<br />

// The following example reads a simple Array (groceries), made up of strings<br />

// and floating-point numbers, and writes it to a ByteArray.<br />

import flash.utils.ByteArray;<br />

// define the grocery list Array<br />

var groceries:Array = ["milk", 4.50, "soup", 1.79, "eggs", 3.19, "bread" , 2.35]<br />

// define the ByteArray<br />

var bytes:ByteArray = new ByteArray();<br />

// for each item in the array<br />

for (var i:int = 0; i < groceries.l<strong>en</strong>gth; i++) {<br />

bytes.writeUTFBytes(groceries[i++]); //write the string and position to the next item<br />

bytes.writeFloat(groceries[i]);// write the float<br />

trace("bytes.position is: " + bytes.position);//display the position in ByteArray<br />

}<br />

trace("bytes l<strong>en</strong>gth is: " + bytes.l<strong>en</strong>gth);// display the l<strong>en</strong>gth<br />

The position property<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The position property stores the curr<strong>en</strong>t position of the pointer that indexes the ByteArray during reading or writing.<br />

The initial value of the position property is 0 (zero) as shown in the following code:<br />

var bytes:ByteArray = new ByteArray();<br />

trace("bytes.position is initially: " + bytes.position); // 0<br />

Wh<strong>en</strong> you read from or write to a ByteArray, the method that you use updates the position property to point to the<br />

location immediately following the last byte that was read or writt<strong>en</strong>. For example, the following code writes a string<br />

to a ByteArray and afterward the position property points to the byte immediately following the string in the<br />

ByteArray:<br />

var bytes:ByteArray = new ByteArray();<br />

trace("bytes.position is initially: " + bytes.position); // 0<br />

bytes.writeUTFBytes("Hello World!");<br />

trace("bytes.position is now: " + bytes.position);// 12<br />

Likewise, a read operation increm<strong>en</strong>ts the position property by the number of bytes read.<br />

Last updated 6/6/2012<br />

778

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!