Flash
  Home arrow Flash arrow Page 4 - A Closer Look at Apollo`s File System API
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

A Closer Look at Apollo`s File System API
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-20

    Table of Contents:
  • A Closer Look at Apollo`s File System API
  • Copying and Moving Files and Directories
  • Reading and Writing Files
  • The open() and openAsync() Methods
  • File Open Modes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    A Closer Look at Apollo`s File System API - The open() and openAsync() Methods


    (Page 4 of 5 )

    Your application needs to open a file before it can read from or write to the file.

    When you open a file with theFileStream.openAsync()method, the file is opened for asynchronous operations, and you’ve registered event listeners to monitor progress.

    TheFileStream.open()method opens the file for synchronous operations. If your application opens the file using this synchronous method, all subsequent calls to methods that read or write to the file will be done synchronously as well. In the following example, each of the calls tostream.open(),stream.writeUTFBytes(), andstream.close()will complete before the next call is made.

      var newFile:File = File.documentsDirectory;
      file = file.resolve("ApolloTest/test.txt");

      var stream:FileStream = new FileStream()
      stream.open(file, FileMode.WRITE);
      stream.writeUTFBytes("This is some sample text.");
      stream.close();

    The advantage of opening a file for synchronous operations is that you can write less code to complete a task. The disadvantage is that execution of other ActionScript code can be delayed if the file operations take a while. As a result, if you are working with large files or opening files that are shared on slow networks, you should consider using theFileStream.openAsync()method instead.

    When you use theopenAsync()method, the following processes are all handled asynchronously:

    Closing the file

    TheFileStream object dispatches acloseevent when the file is closed.

    Reading data into the read buffer

    TheFileStreamobject dispatchesprogressevents as data is read, and it dispatches acompleteevent once all the data is read. However, once data is read, calling a read method (such asreadBytes()) to read data is a synchronous process.

    I/O errors

    TheFileStream object dispatches anioErrorevent upon encountering an error. This may occur for a number of reasons, such as attempting to open a file that doesn’t exist or attempting to write to a file that is locked. However, some errors, such as attempting to read from a file that has not been opened, throw exceptions (rather than dispatchioErrorevents) because the Apollo runtime can detect the error condition instantly.

    Before calling theFileStream.openAsync()method, your application should set up event listener functions to handle those events in which it is interested.

    The following example opens a file in asynchronous read mode. After the file has been opened, the complete event will be dispatched (unless there is an error, in which case theioErrorevent will be dispatched instead). ThecompleteHandler()method then calls theFileStream.readBytes()method, which starts reading data from the file as an array of bytes, in asynchronous mode. When all the bytes have been read from the file, thecompleteevent will be dispatched:

      var file:File = File.documentsDirectory.
      resolve("ApolloTest/test.txt");
      var stream:FileStream = new FileStream();

      stream.addEventListener(ProgressEvent.PROGRESS,
      progressHandler);
      stream.addEventListener(Event.COMPLETE, completeHandler);
      stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
      stream.addEventListener(Event.CLOSE, closeHandler);

      stream.openAsync(file, FileMode.READ);

      var data:ByteArray = new ByteArray();

      private function progressHandler(event:ProgressEvent):void {
         
    trace(stream.bytesAvailable, "bytes read.");
      }
      private function completeHandler(event: Event):void {
         
    data = stream.readBytes(stream.bytesAvailable);
         
    stream.close();
      }
      private function ioErrorHandler(event:IOErrorEvent):void {
         
    trace("An I/O error was encountered.");
      }
      private function closeHandler(event: Event):void {
         
    trace("File closed.");
      }

    More Flash Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Apollo for Adobe Flex Developer's Pocket...
     

    Buy this book now. This article is excerpted from chapter four of the Apollo for Adobe Flex Developer's Pocket Guide, written by Mike Chambers, Rob Dixon and Jeff Swartz (O'Reilly, 2007; ISBN: 0596513917). Check it out today at your favorite bookstore. Buy this book now.

    FLASH ARTICLES

    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks
    - Building Preloaders
    - Fun Things to Do with Movie Clips in Flash MX
    - Referencing Movie Clips in Flash MX







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway