Pages

Tuesday, January 3, 2012

AS3: Single File Loader

Single file loader for type like txt, xml, etc.

SOURCE CODE
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
package
{
 import flash.net.URLLoader;
 import flash.net.URLRequest;
 import flash.events.Event;
 import flash.events.IEventDispatcher;
 import flash.events.ProgressEvent;
 import flash.events.SecurityErrorEvent;
 import flash.events.HTTPStatusEvent;
 import flash.events.IOErrorEvent;
 
 /**
  * @file CSingleFileLoader.as
  * @author Abhishek Kumar
  */
 public class CSingleFileLoader
 {
  public var prepare:Function;
  public var progress:Function;
  public var proceed:Function;
  
  public function CSingleFileLoader()
  {
   trace("CSingleFileLoader -> constructor");
  }
  
  public function retrieve(src:String):void
  {
   trace("CSingleFileLoader -> retrieve src:", src);   
   var loader:URLLoader = new URLLoader();
   attachListeners(loader);
   
   var request:URLRequest = new URLRequest(src);
   try
   {
    loader.load(request);
   }
   catch(error:Error)
   {
    trace("CSingleFileLoader -> retrieve:", error.message);
   }
  }
  
  private function attachListeners(dispatcher:IEventDispatcher):void
  {
   trace("CSingleFileLoader -> attachListeners");
   dispatcher.addEventListener(Event.COMPLETE, completeHandler);
   dispatcher.addEventListener(Event.OPEN, openHandler);
   dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
   dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  }
  
  private function detachListeners(dispatcher:IEventDispatcher):void
  {
   trace("CSingleFileLoader -> detachListeners");
   dispatcher.removeEventListener(Event.COMPLETE, completeHandler);
   dispatcher.removeEventListener(Event.OPEN, openHandler);
   dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
   dispatcher.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
   dispatcher.removeEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
   dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  }
  
  private function completeHandler(event:Event):void
  {
   trace("CSingleFileLoader -> completeHandler:", event, event.target);
   detachListeners(URLLoader(event.target));
   if(proceed != null) 
    proceed(event.target.data);   
  }
  
  private function openHandler(event:Event):void
  {
   trace("CSingleFileLoader -> openHandler:", event);
   if(prepare != null) 
    prepare();
  }
  
  private function progressHandler(event:ProgressEvent):void
  {
   trace("CSingleFileLoader -> progressHandler loaded:", event.bytesLoaded, "total:", event.bytesTotal);
   if(progress != null)
    progress(event.bytesLoaded, event.bytesTotal);
  }
  
  private function securityErrorHandler(event:SecurityErrorEvent):void
  {
   trace("CSingleFileLoader -> securityErrorHandler:", event);
  }
  
  private function httpStatusHandler(event:HTTPStatusEvent):void
  {
   trace("CSingleFileLoader -> httpStatusHandler:", event);
  }
  
  private function ioErrorHandler(event:IOErrorEvent):void
  {
   trace("CSingleFileLoader -> ioErrorHandler:", event);
  }
 }
}

No comments:

Post a Comment