SHARE
TWEET

Untitled

a guest Aug 27th, 2019 126 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package alternativa.startup
  2. {
  3.    import flash.display.Loader;
  4.    import flash.events.Event;
  5.    import flash.events.IOErrorEvent;
  6.    import flash.events.SecurityErrorEvent;
  7.    import flash.net.URLLoader;
  8.    import flash.net.URLLoaderDataFormat;
  9.    import flash.net.URLRequest;
  10.    import flash.system.LoaderContext;
  11.    import flash.utils.ByteArray;
  12.    import flash.utils.getDefinitionByName;
  13.    
  14.    public class CacheLoader extends Loader
  15.    {
  16.        
  17.      
  18.       private var encodedUrl:String;
  19.      
  20.       private var cacheDirectory:Object;
  21.      
  22.       private var context:LoaderContext;
  23.      
  24.       private var cached:Boolean;
  25.      
  26.       private var FileClass:Class;
  27.      
  28.       private var FileStreamClass:Class;
  29.      
  30.       private var FileModeClass:Class;
  31.      
  32.       public function CacheLoader()
  33.       {
  34.          super();
  35.          if(StartupSettings.isDesktop)
  36.          {
  37.             this.FileClass = getDefinitionByName("flash.filesystem.File") as Class;
  38.             this.FileStreamClass = getDefinitionByName("flash.filesystem.FileStream") as Class;
  39.             this.FileModeClass = getDefinitionByName("flash.filesystem.FileMode") as Class;
  40.             this.cacheDirectory = this.FileClass.applicationStorageDirectory.resolvePath("cache");
  41.             if(!this.cacheDirectory.exists)
  42.             {
  43.                this.cacheDirectory.createDirectory();
  44.             }
  45.             else if(!this.cacheDirectory.isDirectory)
  46.             {
  47.                throw new Error("Cannot create directory." + this.cacheDirectory.nativePath + " is already exists.");
  48.             }
  49.          }
  50.       }
  51.      
  52.       override public function load(param1:URLRequest, param2:LoaderContext = null) : void
  53.       {
  54.          if(!StartupSettings.isDesktop)
  55.          {
  56.             super.load(param1,param2);
  57.             return;
  58.          }
  59.          var _loc3_:URLRequest = param1;
  60.          this.context = param2;
  61.          this.encodedUrl = URLEncoder.encode(param1.url);
  62.          var _loc4_:Object = this.cacheDirectory.resolvePath(this.encodedUrl);
  63.          if(_loc4_.exists)
  64.          {
  65.             _loc3_ = new URLRequest(_loc4_.url);
  66.             this.cached = true;
  67.          }
  68.          var _loc5_:URLLoader = new URLLoader();
  69.          _loc5_.dataFormat = URLLoaderDataFormat.BINARY;
  70.          _loc5_.addEventListener(Event.COMPLETE,this.onBytesLoaded,false,0,true);
  71.          _loc5_.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError,false,0,true);
  72.          _loc5_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onSecurityError,false,0,true);
  73.          _loc5_.load(_loc3_);
  74.       }
  75.      
  76.       private function onIOError(param1:Event) : void
  77.       {
  78.          dispatchEvent(new IOErrorEvent("CacheLoader: IOError!"));
  79.       }
  80.      
  81.       private function onSecurityError(param1:Event) : void
  82.       {
  83.          dispatchEvent(new SecurityErrorEvent("CacheLoader: Security error!"));
  84.       }
  85.      
  86.       private function onBytesLoaded(param1:Event) : void
  87.       {
  88.          var file:Object = null;
  89.          var fileStream:Object = null;
  90.          var e:Event = param1;
  91.          var bytes:ByteArray = URLLoader(e.target).data as ByteArray;
  92.          if(!this.cached)
  93.          {
  94.             file = new this.FileClass(this.cacheDirectory.resolvePath(this.encodedUrl).nativePath);
  95.             fileStream = new this.FileStreamClass();
  96.             try
  97.             {
  98.                fileStream.open(file,this.FileModeClass.WRITE);
  99.                fileStream.writeBytes(URLLoader(e.target).data as ByteArray);
  100.                fileStream.close();
  101.             }
  102.             catch(e:Error)
  103.             {
  104.                dispatchEvent(new IOErrorEvent("CacheLoader error! " + e.message + "url: " + encodedUrl));
  105.             }
  106.          }
  107.          super.loadBytes(bytes,this.context);
  108.       }
  109.    }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top