SoFunction
Updated on 2025-04-14

AS3 self-written class organizes notes: ByteLoader class

Class explanation:
Basic functions load pictures by bytes, swf, etc.

Constructor
public function ByteLoader(url:String = "")
If the parameter url is passed in, the loading is performed immediately!

load load method
public function load(_url:String):void
Start loading, _url is the loading address

update data update method
public function updata():void
Update the readable bytes of the buffer

close method
public function close():void
After the class is used, it clears all useless data, and can also be used to forcefully close the data stream and stop downloading.

data attributes
public var data:ByteArray
Returns loaded bytes

url attribute
public var url:String
Return the loaded url

isLoad attribute (read-only)
public function get isLoad():Boolean
Returns whether there is data loading

event
Scheduled during loading and with loading status

event
Loading schedule

example:


import ;

var bl:ByteLoader = new ByteLoader;
("http:///uploads/pro/");
(,completeFun);
(,progressFun);

function completeFun(e:Event):void{
  var loader:Loader = new Loader;
  ();
  addChild(loader);
  (,completeFun);
  (,progressFun);
  ();
  bl = null;
}

function progressFun(e:ProgressEvent):void{
  trace();
//If it is a jpeg image in progressive format, then when publishing this event, read bytes and loading, it can be loaded and displayed while loading.
}
source code:

Copy the codeThe code is as follows:

package { 

  import ; 
  import ; 
  import ; 
  import ; 
  import ; 
  import ; 

  public class ByteLoader extends EventDispatcher{ 

    public var url:String; 
    public var data:ByteArray; 
    private var stream:URLStream; 

    public function ByteLoader(url:String = ""){ 
      if(url != ""){ 
        load(url); 
      } 
    } 

//load
    public function load(_url:String):void{ 
      url = _url; 
      data = new ByteArray; 
      stream = new URLStream; 
      (new URLRequest(url)); 
      (,completeFun); 
      (,progressFun); 
    } 

//loading
    private function progressFun(e:ProgressEvent):void{ 
      if( == 0) return; 
      updata(); 
      dispatchEvent(e); 
    } 

//Loading completed
    private function completeFun(e:Event):void{ 
      (,completeFun); 
      (,progressFun); 
      updata(); 
      if(isLoad) (); 
      dispatchEvent(e); 
    } 

//Update data
    public function updata():void{ 
      if(isLoad) (data,); 
    } 

//Clear data
    public function close():void{ 
      if(isLoad) (); 
      stream = null; 
      data = null; 
    } 

//Get if there is any data loading
    public function get isLoad():Boolean{ 
      if(stream == null) return false; 
      return ; 
    } 
  }