title: AIR (As3) - Download large file trough iPad application
body: <p>First I want to specify that I can't post source code because the project is huge.
  I'm trying to download a large file (500+ MB) on iPad device. 
  Initially I tried with URLLoader, but than I realized that the iPad devices has a very limited resources regarding memory. Than I thought that the URLStream will download the file in chunks and with FileStream I can save this chunks on the device (like this <a href="https://stackoverflow.com/questions/3614199/as3-urlstream-saving-files-to-desktop">AS3: URLStream saving files to desktop?</a>), but I was wrong, the device crashes when I try to download a big file because the RAM of the device is not enough (more precisely this becomes too big: System.privateMemory)
  Does anyone have any idea how to download a file in chunks and is it possible without using "socket connection"?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:
  Here is the code that I use (the commented lines are the version in which the FileStream las closed only after the file is downloaded.</p>
  
  <pre>
  <code>
  
      package components.streamDownloader
      {
          import flash.events.Event;
          import flash.events.EventDispatcher;
          import flash.events.IOErrorEvent;
          import flash.events.OutputProgressEvent;
          import flash.events.ProgressEvent;
          import flash.events.SecurityErrorEvent;
          import flash.filesystem.File;
          import flash.filesystem.FileMode;
          import flash.filesystem.FileStream;
          import flash.net.URLRequest;
          import flash.net.URLStream;
          import flash.system.System;
          import flash.utils.ByteArray;
  
  
      /**
       * 
       */
      public class StreamDownloader extends EventDispatcher
      {
  
          [Event(name="DownloadComplete", type="com.tatstyappz.net.DownloadEvent")]
  
          [Event(name="Error", type="com.tatstyappz.net.DownloadEvent")]
  
  
          //--------------------------------------------------------------------------
          //
          //  Constructor
          //
          //--------------------------------------------------------------------------
  
          public function StreamDownloader()
          {
  
          }
  
  
          //--------------------------------------------------------------------------
          //
          //  Variables
          //
          //--------------------------------------------------------------------------
  
          private var file:File;
  
          //private var fileStream:FileStream;
  
          private var urlRequest:URLRequest;
  
          private var urlStream:URLStream;
  
          private var waitingForDataToWrite:Boolean = false;
  
  
          //--------------------------------------------------------------------------
          //
          //  API
          //
          //--------------------------------------------------------------------------
  
          public function download(urlRequest:URLRequest, file:File):void {
  
  
              init();
  
              this.urlRequest = urlRequest;
              this.file = file;
              //fileStream.open(file, FileMode.WRITE);
              urlStream.load(urlRequest);
          }   
  
  
          //--------------------------------------------------------------------------
          //
          //  Event handlers
          //
          //--------------------------------------------------------------------------    
  
          //----------------------------------
          //  urlStream events
          //----------------------------------
  
          protected function urlStream_openHandler(event:Event):void
          {
              waitingForDataToWrite = false;
              dispatchEvent(event.clone());
          }
  
          protected function urlStream_progressHandler(event:ProgressEvent):void
          {
  
  
              trace("MEMORY:", System.totalMemoryNumber / 1024 / 1024, "MEMORY P:", System.privateMemory / 1024 / 1024, "FREE MEMORY:", System.freeMemory / 1024 / 1024, "PROGRESS:", event.bytesLoaded / event.bytesTotal );
  
  
  
  
              if(waitingForDataToWrite){
                  writeToDisk();
              }       
          }
  
          protected function urlStream_completeHandler(event:Event):void
          {
              if(urlStream.bytesAvailable > 0)
              {
                  writeToDisk();
              }
              //fileStream.close();
  
              destory();
  
              dispatchEvent(event.clone());
  
              // dispatch additional DownloadEvent
              dispatchEvent(new StreamDownloadEvent(StreamDownloadEvent.DOWNLOAD_COMPLETE, urlRequest, file));        
          }
  
          protected function urlStream_securityErrorHandler(event:SecurityErrorEvent):void
          {
              dispatchEvent(new StreamDownloadEvent(StreamDownloadEvent.ERROR, urlRequest, file, event.errorID.toString()));
              destory();
          }
  
          protected function urlStream_ioErrorHandler(event:IOErrorEvent):void
          {
              dispatchEvent(new StreamDownloadEvent(StreamDownloadEvent.ERROR, urlRequest, file, event.errorID.toString()));
              destory();
          }   
  
  
          //----------------------------------
          //  fileStream events
          //----------------------------------
  
          protected function fileStream_outputProgressHandler(event:OutputProgressEvent):void
          {
              waitingForDataToWrite = true;
          }   
  
          protected function fileStream_ioErrorHandler(event:IOErrorEvent):void
          {
              dispatchEvent(new StreamDownloadEvent(StreamDownloadEvent.ERROR, urlRequest, file, event.errorID.toString()));
              destory();
          }   
  
  
          //--------------------------------------------------------------------------
          //
          //  Utils
          //
          //--------------------------------------------------------------------------
  
          private function init():void
          {
              urlStream = new URLStream();
              //fileStream = new FileStream();
  
              urlStream.addEventListener(Event.OPEN, urlStream_openHandler);
              urlStream.addEventListener(ProgressEvent.PROGRESS, urlStream_progressHandler); 
              urlStream.addEventListener(Event.COMPLETE, urlStream_completeHandler);
              urlStream.addEventListener(IOErrorEvent.IO_ERROR, urlStream_ioErrorHandler);
              urlStream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, urlStream_securityErrorHandler);
  
              //fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, fileStream_outputProgressHandler)
              //fileStream.addEventListener(IOErrorEvent.IO_ERROR, fileStream_ioErrorHandler);        
          }
  
          private function destory():void
          {
              urlStream.removeEventListener(Event.OPEN, urlStream_openHandler);
              urlStream.removeEventListener(ProgressEvent.PROGRESS, urlStream_progressHandler); 
              urlStream.removeEventListener(Event.COMPLETE, urlStream_completeHandler);
              urlStream.removeEventListener(IOErrorEvent.IO_ERROR, urlStream_ioErrorHandler);
              urlStream.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, urlStream_securityErrorHandler);
  
              //fileStream.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, fileStream_outputProgressHandler)
              //fileStream.removeEventListener(IOErrorEvent.IO_ERROR, fileStream_ioErrorHandler); 
  
              urlStream = null;
              //fileStream = null;
          }
  
          private function writeToDisk():void {
              /*var fileData:ByteArray = new ByteArray();
              urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
              fileStream.writeBytes(fileData,0,fileData.length);
              waitingForDataToWrite = false;*/
  
              var bytes:ByteArray = new ByteArray();
              urlStream.readBytes( bytes );
  
              var fs:FileStream = new FileStream();
              fs.open( file, FileMode.APPEND );
              fs.writeBytes( bytes );
              fs.close();
          }
  
  
  
  
      }
      }
  
  </code>
  </pre>