Flash / Flex / ActionScript/Development/ByteArray
Версия от 09:19, 26 мая 2010; (обсуждение)
Содержание
By default, private properties are not written when an object is serialized
package {
import flash.display.Sprite;
import flash.net.*;
import flash.utils.ByteArray;
public class CustomTypeExample extends Sprite {
public function CustomTypeExample() {
registerClassAlias("ExampleType", ExampleType);
var example1:ExampleType = new ExampleType(1, 2);
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(example1);
byteArray.position = 0;
var example2:ExampleType = byteArray.readObject() as ExampleType;
trace(example2.getA());
trace(example2.getB());
}
}
}
class ExampleType implements flash.utils.IExternalizable {
private var _a:Number;
private var _b:Number;
public function ExampleType(a:Number = -1, b:Number = -1) {
if(a != -1) {
_a = a;
}
if(b != -1) {
_b = b;
}
}
public function getA():Number {
return _a;
}
public function getB():Number {
return _b;
}
public function writeExternal(output:flash.utils.IDataOutput):void {
output.writeFloat(_a);
output.writeFloat(_b);
}
public function readExternal(input:flash.utils.IDataInput):void {
_a = input.readFloat();
_b = input.readFloat();
}
}
Common Uses of Byte Arrays
package {
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.*;
import flash.net.URLRequest;
import flash.events.Event;
import flash.system.Security;
public class Main extends Sprite {
private var _channel:SoundChannel;
private var _graph:Sprite;
public function Main() {
Security.loadPolicyFile("http://wbex.ru/crossdomain.xml");
var sound:Sound = new Sound();
sound.load(new URLRequest("http://wbex.ru/Demo.mp3"), new SoundLoaderContext(1000, true));
_channel = sound.play();
_graph = new Sprite();
_graph.y = 200;
addChild(_graph);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void {
var bytes:ByteArray = new ByteArray();
SoundMixer.ruputeSpectrum(bytes);
_graph.graphics.clear();
_graph.graphics.lineStyle(0, 0, 1);
var plotX:Number = 0;
for(var i:Number = 0; i < 256; i++) {
_graph.graphics.lineTo(plotX, bytes.readFloat() * stage.stageHeight / 2);
plotX += stage.stageWidth / 256;
}
}
}
}
Creating a Byte Array
package{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class Main extends Sprite{
public function Main(){
var bytes:ByteArray = new ByteArray();
bytes[0] = 256;
trace(bytes[0]); // Outputs 0
bytes.writeInt(1);
trace(bytes[0] + ""+ bytes[1] + ""+ bytes[2] + ""+ bytes[3]);
}
}
}
Embedding Files as Binary Data
package {
import mx.core.ByteArrayAsset;
[Embed(source="f.swf", mimeType="application/octet-stream")]
public class FP9BinaryData extends ByteArrayAsset {
}
}
Embedding XML at compile time
package {
import flash.display.*;
import flash.events.*;
import flash.utils.ByteArray;
public class EmbedXML extends Sprite {
[Embed(source="embeds/data.xml", mimeType="application/octet-stream")]
private var BinaryData:Class;
public function EmbedXML ( ) {
var byteArray:ByteArray = new BinaryData( );
var data:XML = new XML(byteArray.readUTFBytes(byteArray.length));
trace(data.toXMLString( ));
}
}
}
Write array to ByteArray
package{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class Main extends Sprite{
public function Main(){
var arrayA:Array = new Array("a", "b", "c", "d");
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(arrayA);
byteArray.position = 0;
var arrayB:Array = byteArray.readObject() as Array;
arrayB.push("e", "f", "g", "h");
trace(arrayA.length); // Outputs 4
trace(arrayB.length); // Outputs 8
}
}
}