A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: [F9/AS3] byteArray example

Threaded View

  1. #1
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223

    [F9/AS3] byteArray example

    Lets say you have swf which (for the sake of our example) simply has 1 line of code:

    PHP Code:
    var testing="1234567890"
    Of course the file may contain pretty much anything, functions, graphics etc like any other swf. Small file allows just to list code better here.

    The swf is then exported under name "loadedswf.swf". Now instead of loading it directly, we will convert it into byteArray object to be used in our larger project.

    PHP Code:
    bytes = new ByteArray ();
    var 
    ldr:URLLoader = new URLLoader();
    ldr.dataFormat URLLoaderDataFormat.BINARY;
    var 
    req:URLRequest = new URLRequest("loadedswf.swf");
    ldr.addEventListener(Event.COMPLETEcompleteHandler);
    ldr.load(req);
    function 
    completeHandler(e:Event):void {
        
    bytes=ldr.data;
        var 
    s:String="";
        for (var 
    int 0bytes.lengthj++){
            var 
    n:String=bytes.readUnsignedByte().toString(16);
            if(
    n.length<2){
                
    n="0"+n;
            }
            
    s+=n;
        }
        
    trace(s);

    This code loads file "loadedswf.swf" and traces its bytes. You now need to copy/paste the data from trace window and save it. I have attached file ba.txt containing the traced data.

    Now you need to load byteArray from text file and convert it into usable swf file again in your main movie:

    PHP Code:
    var ldr1:URLLoader = new URLLoader();
    var 
    req:URLRequest = new URLRequest("ba.txt");
    ldr1.addEventListener(Event.COMPLETEcompleteHandler1);
    ldr1.load(req);
    function 
    completeHandler1(e:Event):void {
        var 
    bA =new ByteArray ();
        var 
    data1:Array=ldr1.data.split("");
        var 
    data2:Array=[];
        for (var 
    int 0data1.length+=2){
            
    data2.push("0x"+data1 [i]+data1[i+1]);
        }
        for (var 
    int 0data2.length++){
            
    bA[j] = data2[j];
        }
        
    ldr = new Loader ();
        
    ldr.contentLoaderInfo.addEventListener (Event.COMPLETEcompleteHandler);
        
    ldr.loadBytes (bA);
    }
    function 
    completeHandler(e:Event):void {
        
    trace(ldr.content.testing);

    This code first loads txt file, once its loaded the data is converted into byteArra object and Loader object is used to load swf from byteArray. The example finally traces value of variable "testing" from loaded swf ("1234567890").

    Instead of loading byteArra from external file, you could also include it straight into main file:

    PHP Code:
    var loadData:String="(please copy the data from txt file)";
    bytes = new ByteArray ();
    var 
    data1:Array=loadData.split("");
    var 
    data2:Array=[];
    for (var 
    int 0data1.length+=2){
        
    data2.push("0x"+data1 [i]+data1[i+1]);
    }
    for (var 
    int 0data2.length++){
        
    bytes[j] = data2[j];
    }
    ldr = new Loader ();
    ldr.contentLoaderInfo.addEventListener (Event.COMPLETEcompleteHandler);
    ldr.loadBytes (bytes);
    function 
    completeHandler(e:Event):void {
        
    trace(ldr.content.testing);

    Here the variable "loadData" holds exactly same data as saved in text file in previous example. Because the string is so long, FK wont display it properly so copy/paste the data from attached file into code example. The Loader object and loadBytes method is used same way.

    For anyone decompiling the main swf or looking at the loaded text file, the content of loaded.swf remains only visible as bunch of unhelpful bytes.

    Sorry for not using proper classes and FCS3 here, examples are made with F9Alpha, you can simply copy/paste the code into Flash and test the movie.
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center