How do you combine two bytearrays in AS3? - actionscript-3

I'm trying to combine two ByteArrays to send it out as post data using URLRequest.. Whenever I try to simply add them up, the request becomes GET instead of POST and the data for some reason doesn't get included.

create a total ByteArray by adding other ByteArray objects to it via the writeBytes() public method of the ByteArray class.
more info here: Reading and writing a ByteArray

Combining / Concatination Two Byte Arrays
Var Data:ByteArray = new ByteArray();
Var Salt:ByteArray = new ByteArray();
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);//Defines the **length of Resultant Array**
//Array Copy Method(VB)/ Concate the ByteArray(ActionScript) one After another
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
I will Show here Conversion of String into Byte Array and Merging Them (concate /combining) them into single byte array
// In Detail
var HashOut:ByteArray = new ByteArray();
var byterrData:ByteArray = new ByteArray();
var byterrSalt:ByteArray = new ByteArray();
//conversion of string Data and Salt in respective (UTF-8 and Default) Byte Array
var Data:String = "password";
var Salt:String ="‰ô©³¶í"; //Using Special Characters in a String variable
byterrData.writeMultiByte(Data, "iso-8859-1");
byterrSalt.writeMultiByte(Salt,Salt);
var DataAndSalt:ByteArray = new ByteArray();
DataAndSalt.length = (Data.length + Salt.length);
// Concate the ByteArray
DataAndSalt.writeBytes(Data);
DataAndSalt.writeBytes(Salt);
//Now You can Trace It by using
trace(DataAndSalt[0]);
trace(DataAndSalt[index Number]);

Not sure what your code looks like... the GET/POST issue is very weird.
However, use the below instead of trying to "add them up" (whatever that means).
array3 = array1.concat(array2);

Related

Why does (myMC.myArray = myOtherMC.myOtherArray;) cause any changes to myMC.myArray to also change myOtherMC.myOtherArray?

var myArray:Array = new Array();
var myMC:MovieClip = new MovieClip();
myMC.myArray = myArray;
trace(myMC.myArray[10]); //Output: undefined
var newMC:MovieClip = new MovieClip();
newMC.myOtherArray = myMC.myArray;
newMC.myOtherArray[10] = [];
newMC.myOtherArray[10][0] = 100;
trace(myMC.myArray[10]); //Output: 100
Why does that happen, and is there any way to avoid it?
EDIT:
Found a function that can clone associative arrays here.
Here is the function (from the above link):
function clone(source:Object):*
{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(source);
myBA.position = 0;
return(myBA.readObject());
}
Does making the function return type "*" mean that it can be any type? or is it something specific to objects/arrays?
It's because you have passed a reference of the original array to the new clip. Arrays, as objects, are not primitives and therefore will always be referenced.
If you would like to clone an array, keeping the original intact, use this method:
var b:Array = a.concat();
b will be a new array. a can modified without changing b.

How to take a snapshot of a chart and save into Byte Array in flex?

I'm trying to capture snapshot of a chart and save that image as a Byte Array in flex. currently i'm using this method to save a snapshot but now i want to save image in a Byte array. Is this possible ?
this is my current method
private function takeSnapshot():void{
var image:ImageSnapshot = ImageSnapshot.captureImage(chart);
var file:FileReference = new FileReference();
var fileName:String = "chart.png";
file.save(image.data,fileName);
This is how I would do in AS3:
// displayObject being the object to render
var data:BitmapData = new BitmapData(displayObject.width, displayObject.height, true, 0);
data.draw(displayObject);
// the you can have either a raw bytearray or a png:
var bitmapBa:ByteArray = data.getPixels( new Rectangle(0, 0, displayObject.width, displayObject.height);
// or if you're saving to PNG:
var pngBA:ByteArray = PNGEncoder.encode( data );

loading saved bitmap

i had saved a bitmap and i want to load it in runtime.
here is my codes:
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1;
textClass = getDefinitionByName(textName) as
Class;
var tx:BitmapData = new textClass(); txtP[1] = new
Bitmap(tx);
saveDataTxt.data.txtArray[1] = txtP[1];
addChild(saveDataTxt.data.txtArray[n]);
but it gives me an error :
**TypeError: Error #1034: Type Coercion failed: cannot convert Object#384c2b1 to flash.display.DisplayObject.**
whats the solution?
To store a bitmap in a shared object, you would need to serialize it to a byte array first (see Is it possible to store images in the SharedObject of Flash?)
What you can do, is just store your custom BitmapData subclass in the shared object (if you don't want to bother with byte arrays)
//you need to register every class/subclass in your shared object
registerClassAlias("flash.display.BitmapData", BitmapData);
var saveDataTxt:SharedObject = SharedObject.getLocal("File");
var textName:String; var textClass:Class;
textName = "Text0" + 1; textClass = getDefinitionByName(textName) as Class;
registerClassAlias(textName,textClass); //need to register the custom class
var tx:BitmapData = new textClass(); txtP[1] = new Bitmap(tx);
saveDataTxt.data.txtArray[1] = tx; //just store the bitmap data
addChild(new Bitmap(saveDataTxt.data.txtArray[n] as BitmapData)); //you have to cast the object as bitmap data

create a JSON file and save into disk

how to create a JSON file for any object and save it on disk. after some days retrieve it back?
To write to a shared object
var so:SharedObject = SharedObject.getLocal("mySharedObject");
so.data.storedJSON = myJSON;
so.flush();
To retrieve it back elsewhere
var so:SharedObject = SharedObject.getLocal("mySharedObject");
myJSON = so.data.storedJSON;
convert to ByteArray
save
read a this documentation: registerClassAlias
registerClassAlias("com.myDomain", MyClass);
var myClass:MyClass = new MyClass();
var ba:ByteArray = new ByteArray();
ba.writeObject(myClass);
so.data.byteArray = ba;
ba.position = 0;
read
myClass = so.data.byteArray.readObject();
Use SharedObjects. Read up on them here.

URLLoader requests data format to be Text but it gets Binary issue?

I have a URLLoader which normally requesting the data to be in Text format, anyway on a specific conditions it might gets a Binary as result, which is actually just 1 integer number in binary format. Anyway the conversation :
var ba : ByteArray = ( e.currentTarget as URLLoader ).data; // failing on this row.
var r : int = ba.readInt();
doesnt passing anymore since the result data is in text forma due the request header...
How to convert the result data into integer ?
if You are sure that You recive int there , type :
var value:int= int(( e.currentTarget as URLLoader ).data);
Same thing You can do if You get string or XML .
But if You like to recive data as ByteArray , You should type before load on URLLoader line :
import flash.net.URLLoaderDataFormat;
urlloader.dataFormat = URLLoaderDataFormat.BINARY;
than Your data will be bytearray.
Simply, you need to change
urlLoader.dataFormat=URLLoaderDataFormat.BINARY
But before you send a request. After, there's no point doing that.
EDIT
To read text into a binary (ByteArray)
var ba:ByteArray=new ByteArray();
ba.writeUTFBytes(( e.currentTarget as URLLoader ).data);
ba.position=0;
var myInteger:int=ba.readInt();
function test(){
var bytes:ByteArray = new ByteArray();
bytes.writeInt(0x00DDAA99); //create my byte array with int 14527129
bytes.position = 0; //move the postion to the start
var newInt:int = bytes.readInt(); //read the bytes from starting position
trace("new num: "+newInt); //print out the number
}
Basic reading and writing an integer to byte array