13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Drag and drop in AIR<br />

}<br />

}<br />

}<br />

{<br />

}<br />

fileCont<strong>en</strong>ts.writeUTFBytes( 'S' );<br />

//Important: the ByteArray is read from the curr<strong>en</strong>t position<br />

fileCont<strong>en</strong>ts.position = 0;<br />

return fileCont<strong>en</strong>ts;<br />

public function close():void<br />

{<br />

//Nothing needs to be closed in this case.<br />

}<br />

public function reportError(e:ErrorEv<strong>en</strong>t):void<br />

{<br />

trace("Something w<strong>en</strong>t wrong: " + e.errorID + " - " + e.type + ", " + e.text );<br />

}<br />

In practice, synchronous file promises have limited utility. If the amount of data is small, you could just as easily create<br />

a file in a temporary directory and add a normal file list array to the drag-and-drop clipboard. On the other hand, if<br />

the amount of data is large or g<strong>en</strong>erating the data is computationally exp<strong>en</strong>sive, a long synchronous process is<br />

necessary. Long synchronous processes can block UI updates for a noticeable amount of time and make your<br />

application seem unresponsive. To avoid this problem, you can create an asynchronous data provider driv<strong>en</strong> by a<br />

timer.<br />

Using an asynchronous data provider in a file promise<br />

Adobe AIR 2 and later<br />

Wh<strong>en</strong> you use an asynchronous data provider object, the IFilePromise isAsync property must be true and the object<br />

returned by the op<strong>en</strong>() method must implem<strong>en</strong>t the IEv<strong>en</strong>tDispatcher interface. The runtime list<strong>en</strong>s for several<br />

alternative ev<strong>en</strong>ts so that differ<strong>en</strong>t built-in objects can be used as a data provider. For example, progress ev<strong>en</strong>ts are<br />

dispatched by FileStream and URLStream objects, whereas socketData ev<strong>en</strong>ts are dispatched by Socket objects. The<br />

runtime list<strong>en</strong>s for the appropriate ev<strong>en</strong>ts from all of these objects.<br />

The following ev<strong>en</strong>ts drive the process of reading the data from the data provider object:<br />

Ev<strong>en</strong>t.OPEN — Informs the runtime that the data source is ready.<br />

ProgressEv<strong>en</strong>t.PROGRESS — Informs the runtime that data is available. The runtime will read the amount of<br />

available data from the data provider object.<br />

ProgressEv<strong>en</strong>t.SOCKET_DATA — Informs the runtime that data is available. The socketData ev<strong>en</strong>t is dispatched<br />

by socket-based objects. For other object types, you should dispatch a progress ev<strong>en</strong>t. (The runtime list<strong>en</strong>s for<br />

both ev<strong>en</strong>ts to detect wh<strong>en</strong> data can be read.)<br />

Ev<strong>en</strong>t.COMPLETE — Informs the runtime that the data has all be<strong>en</strong> read.<br />

Ev<strong>en</strong>t.CLOSE — Informs the runtime that the data has all be<strong>en</strong> read. (The runtime list<strong>en</strong>s for both close and<br />

complete for this purpose.)<br />

IOErrorEv<strong>en</strong>t.IOERROR — Informs the runtime that an error reading the data has occurred. The runtime aborts<br />

file creation and calls the IFilePromise close() method.<br />

Last updated 6/6/2012<br />

626

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!