3
\$\begingroup\$

I have the following function in a Service class

protected function writeToFile():void{
        var destinationFile:File = File.applicationStorageDirectory.resolvePath(_newPath);

        var fs:FileStream = new FileStream();

        fs.open(destinationFile, FileMode.WRITE);
        fs.writeBytes(_buffer, 0, _buffer.length);

        fs.close();         


    }

this Service class should dispatch a SuccessfulCreateLocalFileEvent when writeToFile is successful and an UnsuccessfulCreateLocalFileEvent if writeToFile is unsuccessful.

I know that FileStream functions open and writeBytes may throw IOError if there is any problems. And all these FileStream functions all have void as return types.

I believe I need try catch, but I am not entirely sure how to do so.

Please advise.

This question is replicated at http://knowledge.robotlegs.org/discussions/questions/924-what-is-best-way-to-write-dispatch-events-in-this-service-class-function Experimenting which site has better answers for such a question.

\$\endgroup\$
3
\$\begingroup\$

FileStream class has its own events which are dispatched when the file operation is successful or not.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html#eventSummary

A possible refactor of the code is

protected function writeToFile():void{
    _destinationFile = File.applicationStorageDirectory.resolvePath(_newPath);

    var fs:FileStream = new FileStream();

    fs.open(_destinationFile, FileMode.WRITE);
    fs.writeBytes(_buffer, 0, _buffer.length);

    fs.addEventListener(Event.CLOSE, dispatchFileCreatedEvent);

    fs.close(); 

}

Thanks to krasimir of knowledge Robotlegs Community for his help in arriving at this answer.

\$\endgroup\$
1
\$\begingroup\$

I'm not familiar with ActionScript but it seems to me that the usual try-catch-finally structure is available in this language too. A possible usage could be the following:

var fs:FileStream = new FileStream();
try {
    fs.open(destinationFile, FileMode.WRITE);
    fs.writeBytes(_buffer, 0, _buffer.length);
} finally {
    fs.close();
}

It ensures that the file will be closed even something throws an IOError or other exception.

I don't know how event dispatch works in ActionScript but maybe you could use something like this:

boolean successful = false;
try {
    var fs:FileStream = new FileStream();
    try {
        fs.open(destinationFile, FileMode.WRITE);
        fs.writeBytes(_buffer, 0, _buffer.length);
    } finally {
        fs.close();
    }
    successful = true;
} finally {
    if (successful) {
        // dispatch a SuccessfulCreateLocalFileEvent here
    } else {
        // dispatch an UnsuccessfulCreateLocalFileEvent here
    }
}

It sets the successful flag to true only if no file operation throws exception.

If you want to handle errors locally, you'll need a catch block too:

boolean successful = false;
try {
    var fs:FileStream = new FileStream();
    try {
        fs.open(destinationFile, FileMode.WRITE);
        fs.writeBytes(_buffer, 0, _buffer.length);
    } finally {
        fs.close();
    }
    successful = true;
} catch (IOError) {
    // handle IOError here
} finally {
    if (successful) {
        // dispatch a SuccessfulCreateLocalFileEvent here
    } else {
        // dispatch an UnsuccessfulCreateLocalFileEvent here
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.