home *** CD-ROM | disk | FTP | other *** search
/ Computer Active 2010 August / CA08.iso / Multimedija / shufflr.air / ShufflrClient.swf / scripts / air / update / net / FileDownloader.as
Encoding:
Text File  |  2010-06-23  |  6.3 KB  |  195 lines

  1. package air.update.net
  2. {
  3.    import air.update.events.DownloadErrorEvent;
  4.    import air.update.events.UpdateEvent;
  5.    import air.update.logging.Logger;
  6.    import air.update.utils.Constants;
  7.    import air.update.utils.NetUtils;
  8.    import flash.errors.EOFError;
  9.    import flash.errors.IOError;
  10.    import flash.events.ErrorEvent;
  11.    import flash.events.Event;
  12.    import flash.events.EventDispatcher;
  13.    import flash.events.HTTPStatusEvent;
  14.    import flash.events.IOErrorEvent;
  15.    import flash.events.ProgressEvent;
  16.    import flash.events.SecurityErrorEvent;
  17.    import flash.filesystem.File;
  18.    import flash.filesystem.FileMode;
  19.    import flash.filesystem.FileStream;
  20.    import flash.net.URLRequest;
  21.    import flash.net.URLStream;
  22.    import flash.utils.ByteArray;
  23.    
  24.    public class FileDownloader extends EventDispatcher
  25.    {
  26.       private static var logger:Logger = Logger.getLogger("air.update.net.FileDownloader");
  27.       
  28.       private var downloadedFile:File;
  29.       
  30.       private var urlStream:URLStream;
  31.       
  32.       private var fileURL:URLRequest;
  33.       
  34.       private var isInError:Boolean;
  35.       
  36.       private var fileStream:FileStream;
  37.       
  38.       public function FileDownloader(param1:URLRequest, param2:File)
  39.       {
  40.          super();
  41.          this.fileURL = param1;
  42.          this.fileURL.useCache = false;
  43.          this.downloadedFile = param2;
  44.          logger.finer("url: " + param1.url + " file: " + param2.nativePath);
  45.          this.urlStream = new URLStream();
  46.          this.urlStream.addEventListener(IOErrorEvent.IO_ERROR,this.onDownloadError);
  47.          this.urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onDownloadError);
  48.          this.urlStream.addEventListener(ProgressEvent.PROGRESS,this.onDownloadProgress);
  49.          this.urlStream.addEventListener(Event.OPEN,this.onDownloadOpen);
  50.          this.urlStream.addEventListener(Event.COMPLETE,this.onDownloadComplete);
  51.          this.urlStream.addEventListener(HTTPStatusEvent.HTTP_RESPONSE_STATUS,this.onDownloadResponseStatus);
  52.       }
  53.       
  54.       private function onDownloadComplete(param1:Event) : void
  55.       {
  56.          while(Boolean(this.urlStream) && Boolean(this.urlStream.bytesAvailable))
  57.          {
  58.             this.saveBytes();
  59.          }
  60.          if(Boolean(this.urlStream) && this.urlStream.connected)
  61.          {
  62.             this.urlStream.close();
  63.             this.urlStream = null;
  64.          }
  65.          this.fileStream.close();
  66.          this.fileStream = null;
  67.          if(!this.isInError)
  68.          {
  69.             dispatchEvent(new UpdateEvent(UpdateEvent.DOWNLOAD_COMPLETE));
  70.          }
  71.       }
  72.       
  73.       public function cancel() : void
  74.       {
  75.          try
  76.          {
  77.             if(Boolean(this.urlStream) && this.urlStream.connected)
  78.             {
  79.                this.urlStream.close();
  80.             }
  81.             if(this.fileStream)
  82.             {
  83.                this.fileStream.close();
  84.                this.fileStream = null;
  85.             }
  86.             if(Boolean(this.downloadedFile) && Boolean(this.downloadedFile.exists))
  87.             {
  88.                this.downloadedFile.deleteFile();
  89.             }
  90.          }
  91.          catch(e:Error)
  92.          {
  93.             logger.fine("Error during canceling the download: " + e);
  94.          }
  95.       }
  96.       
  97.       public function download() : void
  98.       {
  99.          this.urlStream.load(this.fileURL);
  100.       }
  101.       
  102.       private function onDownloadResponseStatus(param1:HTTPStatusEvent) : void
  103.       {
  104.          logger.fine("DownloadStatus: " + param1.status);
  105.          if(!NetUtils.isHTTPStatusAcceptable(param1.status))
  106.          {
  107.             this.dispatchErrorEvent("Invalid HTTP status code: " + param1.status,Constants.ERROR_INVALID_HTTP_STATUS,param1.status);
  108.          }
  109.       }
  110.       
  111.       public function inProgress() : Boolean
  112.       {
  113.          return this.urlStream.connected;
  114.       }
  115.       
  116.       private function dispatchErrorEvent(param1:String, param2:int = 0, param3:int = 0) : void
  117.       {
  118.          this.isInError = true;
  119.          if(Boolean(this.urlStream) && this.urlStream.connected)
  120.          {
  121.             this.urlStream.close();
  122.             this.urlStream = null;
  123.          }
  124.          dispatchEvent(new DownloadErrorEvent(DownloadErrorEvent.DOWNLOAD_ERROR,false,false,param1,param2,param3));
  125.       }
  126.       
  127.       private function saveBytes() : void
  128.       {
  129.          var bytes:ByteArray = null;
  130.          if(!this.fileStream || !this.urlStream || !this.urlStream.connected)
  131.          {
  132.             return;
  133.          }
  134.          try
  135.          {
  136.             bytes = new ByteArray();
  137.             this.urlStream.readBytes(bytes,0,this.urlStream.bytesAvailable);
  138.             this.fileStream.writeBytes(bytes);
  139.          }
  140.          catch(error:EOFError)
  141.          {
  142.             isInError = true;
  143.             logger.fine("EOFError: " + error);
  144.             dispatchErrorEvent(error.message,Constants.ERROR_EOF_DOWNLOAD,error.errorID);
  145.          }
  146.          catch(err:IOError)
  147.          {
  148.             isInError = true;
  149.             logger.fine("IOError: " + err);
  150.             dispatchErrorEvent(err.message,Constants.ERROR_IO_FILE,err.errorID);
  151.          }
  152.       }
  153.       
  154.       private function onDownloadError(param1:ErrorEvent) : void
  155.       {
  156.          if(param1 is IOErrorEvent)
  157.          {
  158.             this.dispatchErrorEvent(param1.text,Constants.ERROR_IO_DOWNLOAD,param1.errorID);
  159.          }
  160.          else if(param1 is SecurityErrorEvent)
  161.          {
  162.             this.dispatchErrorEvent(param1.text,Constants.ERROR_SECURITY,param1.errorID);
  163.          }
  164.       }
  165.       
  166.       private function onDownloadOpen(param1:Event) : void
  167.       {
  168.          var event:Event = param1;
  169.          this.fileStream = new FileStream();
  170.          try
  171.          {
  172.             this.fileStream.open(this.downloadedFile,FileMode.WRITE);
  173.          }
  174.          catch(e:Error)
  175.          {
  176.             logger.fine("Error opening file on disk: " + e);
  177.             isInError = true;
  178.             dispatchErrorEvent(e.message,Constants.ERROR_IO_FILE,e.errorID);
  179.             return;
  180.          }
  181.          dispatchEvent(new UpdateEvent(UpdateEvent.DOWNLOAD_START,false,false));
  182.       }
  183.       
  184.       private function onDownloadProgress(param1:ProgressEvent) : void
  185.       {
  186.          if(!this.isInError)
  187.          {
  188.             this.saveBytes();
  189.             dispatchEvent(param1);
  190.          }
  191.       }
  192.    }
  193. }
  194.  
  195.