The readObject() and writeObject() methods
read an object from and write an object to a ByteArray, encoded
in serialized Action Message Format (AMF). AMF is a proprietary
message protocol created by Adobe and used by various ActionScript
3.0 classes, including Netstream, NetConnection, NetStream, LocalConnection,
and Shared Objects.
A one-byte type marker describes the type of the encoded data
that follows. AMF uses the following 13 data types:
value-type = undefined-marker | null-marker | false-marker | true-marker | integer-type |
double-type | string-type | xml-doc-type | date-type | array-type | object-type |
xml-type | byte-array-type
The encoded data follows the type marker unless the marker represents
a single possible value, such as null or true or false, in which
case nothing else is encoded.
There are two versions of AMF: AMF0 and AMF3. AMF 0 supports
sending complex objects by reference and allows endpoints to restore
object relationships. AMF 3 improves AMF 0 by sending object traits
and strings by reference, in addition to object references, and
by supporting new data types that were introduced in ActionScript
3.0. The ByteArray.objectEcoding property specifies
the version of AMF that is used to encode the object data. The flash.net.ObjectEncoding
class defines constants for specifying the AMF version: ObjectEncoding.AMF0 and ObjectEncoding.AMF3.
The following
example calls writeObject() to write an XML object
to a ByteArray, which it then compresses using the Deflate algorithm
and writes to the order file on the desktop. The
example uses a label to display the message “Wrote order file to desktop!”
in the AIR window when it is finished.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="init();">
<mx:Script>
<![CDATA[
import flash.filesystem.*;
import flash.utils.ByteArray;
import mx.controls.Label;
private function init():void {
var bytes:ByteArray = new ByteArray();
var myLabel:Label = new Label();
myLabel.move(150, 150);
myLabel.width = 200;
addChild(myLabel);
var myXML:XML =
<order>
<item id='1'>
<menuName>burger</menuName>
<price>3.95</price>
</item>
<item id='2'>
<menuName>fries</menuName>
<price>1.45</price>
</item>
</order>
// Write XML object to ByteArray
bytes.writeObject(myXML);
bytes.position = 0; //reset position to beginning
bytes.compress(CompressionAlgorithm.DEFLATE); // compress ByteArray
outFile("order", bytes);
myLabel.text = "Wrote order file to desktop!";
} // end of init()
function outFile(fileName:String, data:ByteArray):void {
var outFile:File = File.desktopDirectory; // dest folder is desktop
outFile = outFile.resolvePath(fileName); // name of file to write
var outStream:FileStream = new FileStream();
// open output file stream in WRITE mode
outStream.open(outFile, FileMode.WRITE);
// write out the file
outStream.writeBytes(data, 0, data.length);
// close it
outStream.close();
}
]]>
</mx:Script>
</mx:WindowedApplication>
The readObject() method
reads an object in serialized AMF from a ByteArray and stores it
in an object of the specified type. The following example reads
the order file from the desktop into a ByteArray (inBytes),
uncompresses it, and calls readObject() to store
it in the XML object orderXML. The example uses
a for each() loop construct to add each node to
a text area for display. The example also displays the value of
the objectEncoding property along with a header
for the contents of the order file.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="init();">
<mx:Script>
<![CDATA[
import flash.filesystem.*;
import flash.utils.ByteArray;
import mx.controls.TextArea;
private function init():void {
var inBytes:ByteArray = new ByteArray();
// define text area for displaying XML content
var myTxt:TextArea = new TextArea();
myTxt.width = 550;
myTxt.height = 400;
addChild(myTxt);
//display objectEncoding and file heading
myTxt.text = "Object encoding is: " + inBytes.objectEncoding + "\n\n" + "order file: \n\n";
readFile("order", inBytes);
inBytes.position = 0; // reset position to beginning
inBytes.uncompress(CompressionAlgorithm.DEFLATE);
inBytes.position = 0; //reset position to beginning
// read XML Object
var orderXML:XML = inBytes.readObject();
//for each node in orderXML
for each(var child:XML in orderXML) {
// append child node to text area
myTxt.text += child + "\n";
}
} // end of init()
// read specified file into byte array
function readFile(fileName:String, data:ByteArray) {
var inFile:File = File.desktopDirectory; // source folder is desktop
inFile = inFile.resolvePath(fileName); // name of file to read
var inStream:FileStream = new FileStream();
inStream.open(inFile, FileMode.READ);
inStream.readBytes(data, 0, data.length);
inStream.close();
}
]]>
</mx:Script>
</mx:WindowedApplication>