SHARE
TWEET

Custom URLLoader

ulfben Dec 15th, 2016 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package net {
  2.     import flash.events.Event;
  3.     import flash.events.EventDispatcher;
  4.     import flash.events.HTTPStatusEvent;
  5.     import flash.events.IOErrorEvent;
  6.     import flash.events.ProgressEvent;
  7.     import flash.events.SecurityErrorEvent;
  8.     import flash.net.URLRequest;
  9.     import flash.net.URLStream;
  10.     import flash.net.URLVariables;
  11.     import flash.utils.ByteArray;
  12.  
  13.     /*
  14.      * The native URLLoader has an annoying limitation: it does not offer a way
  15.      * to get the URL back from it!
  16.      *
  17.      * Luckily it is a just a thin wrapper around the URLStream, so I chose to
  18.      * implement my own URLLoader with added conveniences.
  19.      * */
  20.  
  21.     public class URLLoader extends EventDispatcher{
  22.         private var _urlRequest:URLRequest;
  23.         private var _stream:URLStream;
  24.         private var _dataFormat:String = "text";
  25.         private var _data:* = null;
  26.         private var _bytesLoaded:uint = 0;
  27.         private var _bytesTotal:uint = 0;
  28.  
  29.         public function get request():URLRequest    { return _urlRequest; }
  30.         public function get url():String            { return _urlRequest.url; }
  31.         public function get fileName():String       {
  32.             var parts:Array = _urlRequest.url.match(/(?:\\|\/)([^\\\/]*)$/);
  33.             return (parts && parts.length > 1) ? parts[1] : _urlRequest.url;
  34.         }
  35.         public function get dataFormat():String     { return _dataFormat;}
  36.         public function get data():*                { return _data; }
  37.         public function get bytesLoaded():uint      { return _bytesLoaded; }
  38.         public function get bytesTotal():uint       { return _bytesTotal; }
  39.         public function get loadProgress():Number   { return (_bytesTotal) ? _bytesLoaded / _bytesTotal : 0; }
  40.  
  41.         public function URLLoader(request:URLRequest = null){
  42.             super(null); //initiate the eventdispatcher
  43.             _stream = new URLStream();
  44.             _stream.addEventListener(Event.OPEN, onConnectionOpened, false, 0, false);
  45.             _stream.addEventListener(ProgressEvent.PROGRESS, onLoadProgress, false, 0, false);
  46.             _stream.addEventListener(Event.COMPLETE, onLoadComplete, false, 0, false);
  47.             _stream.addEventListener(IOErrorEvent.IO_ERROR, onInputOutputError, false, 0, false);
  48.             _stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError, false, 0, false);
  49.             _stream.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus, false, 0, false);
  50.             if (request != null){
  51.                 load(request);
  52.             };
  53.         }
  54.         public function load(request:URLRequest):void {
  55.             _urlRequest = request; //tadaa! saving the adress for future reference.
  56.             _stream.load(_urlRequest);
  57.         }
  58.         public function close():void{
  59.             _stream.close();
  60.         }
  61.  
  62.         private function onLoadProgress(event:ProgressEvent):void {
  63.             _bytesLoaded = event.bytesLoaded;
  64.             _bytesTotal = event.bytesTotal;
  65.             dispatchEvent(event);
  66.         }
  67.         private function onLoadComplete(event:Event):void{
  68.             var bytes:ByteArray = new ByteArray();
  69.             _stream.readBytes(bytes);
  70.             switch (_dataFormat){
  71.                 case "binary":
  72.                     _data = bytes;
  73.                     break;
  74.                 case "variables":
  75.                     if (bytes.length > 0){
  76.                         _data = new URLVariables(bytes.toString());
  77.                         break;
  78.                     };
  79.                 case "text":
  80.                 default:
  81.                     _data = bytes.toString();
  82.                     break;
  83.             };
  84.             dispatchEvent(event);
  85.         }
  86.         private function onConnectionOpened(event:Event):void {
  87.             dispatchEvent(event);
  88.         }
  89.  
  90.         private function onSecurityError(event:SecurityErrorEvent):void {
  91.             dispatchEvent(event);
  92.         }
  93.         private function onHTTPStatus(event:HTTPStatusEvent):void {
  94.             dispatchEvent(event);
  95.         }
  96.         private function onInputOutputError(event:IOErrorEvent):void {
  97.             dispatchEvent(event);
  98.         }
  99.     }
  100. }
RAW Paste Data
Top