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 />

bytes.position = 0;//reset position to beginning<br />

bytes.compress(CompressionAlgorithm.DEFLATE);// compress ByteArray<br />

outFile("order", bytes);<br />

myLabel.text = "Wrote order file to desktop!";<br />

// for Flex: } // <strong>en</strong>d of init()function outFile(fileName:String, data:ByteArray):void {<br />

var outFile:File = File.desktopDirectory; // dest folder is desktop<br />

outFile = outFile.resolvePath(fileName); // name of file to write<br />

var outStream:FileStream = new FileStream();<br />

// op<strong>en</strong> output file stream in WRITE mode<br />

outStream.op<strong>en</strong>(outFile, FileMode.WRITE);<br />

// write out the file<br />

outStream.writeBytes(data, 0, data.l<strong>en</strong>gth);<br />

// close it<br />

outStream.close();<br />

}<br />

/* Add the following lines for Flex, minus comm<strong>en</strong>t characters:<br />

* ]]><br />

* <br />

* <br />

*/<br />

The readObject() method reads an object in serialized AMF from a ByteArray and stores it in an object of the<br />

specified type. The following example reads the order file from the desktop into a ByteArray (inBytes),<br />

uncompresses it, and calls readObject() to store it in the XML object orderXML. The example uses a for each()<br />

loop construct to add each node to a text area for display. The example also displays the value of the objectEncoding<br />

property along with a header for the cont<strong>en</strong>ts of the order file.<br />

/* The following lines, minus comm<strong>en</strong>t characters, are for Flex version:<br />

* <br />

* <br />

* <br />

*

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

Saved successfully!

Ooh no, something went wrong!