Last active
September 30, 2016 14:41
-
-
Save xleon/bfbc9f973b9960fdb35dbc0f79c805cf to your computer and use it in GitHub Desktop.
Write bytes to a FileStream in async mode and close stream when finished
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lba.utils | |
{ | |
import flash.events.IOErrorEvent; | |
import flash.events.OutputProgressEvent; | |
import flash.filesystem.File; | |
import flash.filesystem.FileMode; | |
import flash.filesystem.FileStream; | |
import flash.utils.ByteArray; | |
import org.osflash.signals.Signal; | |
import reborn.IDisposable; | |
/*** | |
* We use stream.openAsync(..) to avoid blocking the UI thread. | |
* After using openAsync(), stream.writeBytes() is not syncronous anymore | |
* and we must listen for OutputProgressEvent.OUTPUT_PROGRESS to know when | |
* all bytes have been written. | |
* | |
* Usage sample: | |
* | |
new FileStreamAsyncWritter(file) | |
.writeBytesAsync(byteArray) | |
.completed | |
.addOnce(function():void { trace("yeah"); }); | |
* Output sample: | |
[trace] output progress 1943736 2271416 | |
[trace] output progress 1878200 2271416 | |
[trace] output progress 1812664 2271416 | |
[trace] output progress 1747128 2271416 | |
[trace] output progress 1681592 2271416 | |
[trace] output progress 1616056 2271416 | |
[trace] output progress 1550520 2271416 | |
[trace] output progress 1484984 2271416 | |
[trace] output progress 1419448 2271416 | |
[trace] output progress 1353912 2271416 | |
[trace] output progress 1288376 2271416 | |
[trace] output progress 1222840 2271416 | |
[trace] output progress 1157304 2271416 | |
[trace] output progress 1091768 2271416 | |
[trace] output progress 1026232 2271416 | |
[trace] output progress 960696 2271416 | |
[trace] output progress 895160 2271416 | |
[trace] output progress 829624 2271416 | |
[trace] output progress 764088 2271416 | |
[trace] output progress 698552 2271416 | |
[trace] output progress 633016 2271416 | |
[trace] output progress 567480 2271416 | |
[trace] output progress 501944 2271416 | |
[trace] output progress 436408 2271416 | |
[trace] output progress 370872 2271416 | |
[trace] output progress 305336 2271416 | |
[trace] output progress 239800 2271416 | |
[trace] output progress 174264 2271416 | |
[trace] output progress 108728 2271416 | |
[trace] output progress 43192 2271416 | |
[trace] output progress 0 2271416 | |
[trace] yeah | |
*/ | |
public class FileStreamAsyncWritter implements IDisposable | |
{ | |
private var _onError:Signal; | |
public function get onError():Signal { return _onError; } | |
private var _completed:Signal; | |
public function get completed():Signal { return _completed; } | |
private var _stream:FileStream; | |
public function FileStreamAsyncWritter(file:File) | |
{ | |
_onError = new Signal(String); | |
_completed = new Signal(); | |
_stream = new FileStream(); | |
_stream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, outputProgressHandler); | |
_stream.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); | |
_stream.openAsync(file, FileMode.WRITE); | |
} | |
public function writeBytesAsync(bytes:ByteArray):FileStreamAsyncWritter | |
{ | |
_stream.writeBytes(bytes, 0, bytes.length); | |
return this; | |
} | |
private function outputProgressHandler(event:OutputProgressEvent):void | |
{ | |
trace("output progress", event.bytesPending, event.bytesTotal); | |
if(event.bytesPending == 0) | |
{ | |
_completed.dispatch(); | |
dispose(); | |
} | |
} | |
private function errorHandler(event:IOErrorEvent):void | |
{ | |
trace("error", event.text); | |
_onError.dispatch(event.text); | |
dispose(); | |
} | |
public function dispose():void | |
{ | |
_stream.close(); | |
_stream.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, outputProgressHandler); | |
_stream.removeEventListener(IOErrorEvent.IO_ERROR, errorHandler); | |
_stream = null; | |
_onError.removeAll(); | |
_onError = null; | |
_completed.removeAll(); | |
_completed = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment