Recipe 19.5. Accessing Data Being Downloaded


Problem

You want to access data as it downloads.

Solution

Use a flash.net.URLStream instance to load binary data that you can read as the data loads into your movie.

Discussion

Recipe 19.4 outlines how to check on the progress of data as it is downloaded. However, due to the nature of the URLLoader class, the data cannot be read until the download is complete. To read data as it arrives, you must use the URLStream class instead.

The URLStream class allows you to read data in a binary format as it loads into the Flash Player, similar to using a URLLoader with its dataFormat property set to DataFormat.BINARY. The interface of URLStream is very similar to URLLoader. You use the same events described in Recipe 19.1 to check for loading success and failure in both URLStream and URLLoader. The key difference is how the progress event is handled.

In the case of URLLoader, the progress event is only useful to indicate the amount of data that has been received and perhaps display the percentage loaded somewhere onscreen. In the case of URLStream, the progress event allows you to inspect the data by using the bytesAvailable property and one of the various read methods, such as readInt( ), readByte( ), readBoolean( ), etc.

Using URLStream is an advanced feature and is recommended only if URLLoader is not sufficient and if you have a good understanding of data on a byte level.


The following is some example code that uses a URLStream to load a .txt file, accessing the bytes of the file as it downloads into the Flash Player:

package {   import flash.display.*;   import flash.events.*   import flash.net.*;      public class CheckProgressExample extends Sprite {        public function CheckProgressExample(  ) {       var streamer:URLStream = new URLStream(  );              // Listen for the progress event to act on data        // as it is received       streamer.addEventListener( ProgressEvent.PROGRESS, handleProgress );                               streamer.load( new URLRequest( "example.txt" ) );     }          private function handleProgress( event:ProgressEvent ):void {       // Cast the target of the vent as a URLStream       var streamer:URLStream = URLStream( event.target );              // Loop over all of the bytes that are available       while ( streamer.bytesAvailable > 0 ) {         // Read a byte value and output it to the console window         trace( "Read byte: " + streamer.readByte(  ) );           }     }   } }

In the preceding code example, it is important to look at how many bytes are available via the bytesAvailable property before attempting a read operation. Trying to read more bytes than what is available in the input buffer results in an EOFError exception being thrown.


See Also

Recipes 19.1 and 19.4




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net