84 | Chapter 4: AIR Mini-Cookbook
// Append the most recent content
document.getElementById( "editor" ).value += data;
// Close the file after the entire contents have been
read
if( event.bytesLoaded == event.bytesTotal )
{
stream.close( );
}
}
Registering for events takes place using the
addEventListener( ) API:
stream.addEventListener( air.ProgressEvent.PROGRESS,
doProgress );
Opening a stream for asynchronous access is accomplished
using the FileStream.openAsync( ). The FileStream.
openAsync( )method takes two arguments that specify the
file being accessed and the type of access being performed.
TheFileModeobject serves no other purpose but to provide
constants for the types of file access that can be performed.
These operations are FileMode.READ, FileMode.WRITE,
FileMode.UPDATE, andFileMode.APPEND:
stream.openAsync( file, air.FileMode.READ );
As soon as the file is opened and new data is available in the
stream, the ProgressEvent.PROGRESS event is triggered.
Depending on the size of the file, as well as machine and net-
work characteristics, not all the bytes may be read in a single
pass. In many cases, additional read operations take place,
raising aProgressEvent.PROGRESS event for each iteration.
Once all of the data has been read from the file, anEvent.
COMPLETE event is broadcast.
Once the file has been read, it is important to close the file
stream in order to ensure that other applications can access
it:
stream.close( );