Adobe Releases AIR and Flex 3

Adobe has released some AIR. No not that kinda air, The Adobe Integrated Runtime, this is an update and the new name of Apollo.

Also, released was Flex 3 .
Of course these are both on Labs, so they are both pre-releases.

Now that Ted got everyone so excited, it is time to play…wish I wasn’t so sleepy.

Posted in news | Tagged , , , , , | Leave a comment

If you were an X-Men Super Hero?

I’d be Beast

Posted in news | Tagged | Leave a comment

Handling Different File Types in Apollo

So, I’ve finally snagged some time to whip out a post about dealing with different file types with the File API in Apollo AIR.

The contents of a file can be read into a ByteArray and manipulated there. For example if we had a simple text file we could read in its contents to a ByteArray with the following code:
[as]
var bytes:ByteArray = new ByteArray(); // Create our ByteArray that will hold our file data

var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
myFile = myFile.resolve(“mySampleFile.txt”); // Point it to an actual file

var fileStream:FileStream = new FileStream(); // Create our file stream
fileStream.open(myFile, FileMode.READ);

fileStream.readBytes(bytes, 0, fileStream.bytesAvailable);

trace(ObjectUtil.toString(bytes));
[/as]
You should see something that looks like the following image when you run this code:

If we wanted to out put some of the data in the ByteArray we could add the following:
[as]
for(var i:Number = 0; i <= 9; i++)
{
trace(“Byte Data: ” + ObjectUtil.toString(bytes[i]));
}
[/as]
This would jsut output the first 9 items int he ByteArray. So, you’d see something like the following:

I asked myself, well what good is that? We’ll lets say the file you were reading was a PNG. You could figure out how to write that file data out to the PNG file format, or you could check out the AS3 libraries that has a PNG and JPEG encoder already.With that said you now have control…hold that, TOTAL control of the file as you read/write the bytes to/from a file.

In my previous posts (Reading a File & Reading a file Asynchronously), the file contents were read into a String variable. This was accomplished with the FileStream readMultiByte() method.

The readMultiByte(length:uint, charSet:String) method takes two (2) parameters:

  1. length: uint – The number of bytes to read from the stream
  2. charSet: String – The String specifying the character set to use to interpret the bytes read from the stream. I’m looking for a nice list of supported character sets, but have yet to find them. I’ll let you know when I do. For now we can use the livedoc examples of "shift-jis", "cn-gb" & "iso-8859-1".

We’ve checked out reading image data and text data, you could extend either of these to accommodate most situations. I’ll keep playing and if I run into anything I’ll be sure to add to this series.

Posted in news | Tagged , , , , , | Leave a comment

Moxie’s New Features – Flex Builder 3

So if you haven’t checked out Ted Patrick’s blog yet, do so now and keep an eye on it all week!.
He’s going to be posting new info on Moxie (Flex 3). So check it out, there are some really interesting additions to Flex Builder in the upcomming release.

Posted in news | Tagged , , , , | Leave a comment

Reading a file Asynchronously with ActionScript 3

Sometimes (okay, manytimes) files are large enough that the process of opening and reading its contents may take some time. To solve issues related to the extra time this may take AS3 allows you to open files asynchronously. I’d like to add to the information from the reading files with AS3 post. Here we’ll cover the code necessary to open a file asynchronously and read the file contents as they become available.

Because we are now doing something asynchronously, we will be dealing with events. As the file contents are being read The FileStream object broadcasts progress events that we can listen for and respond to. This ensures that when we are reading data when the data is available.

So, onto the code:
[as]
// Imports
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.events.ProgressEvent;
import flash.events.Event;

// Declare the FileStream and String variables
private var _fileStream:FileStream;
private var _fileContents:String;

private function onCreationComplete():void // Fired when the application has been created
{
var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
myFile = myFile.resolve(“mySampleFile.txt”); // Point it to an actual file

_fileStream = new FileStream(); // Create our file stream

_fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress); // Add our the progress event listener
_fileStream.addEventListener(Event.COMPLETE, onFileComplete); // Add our the complete event listener

_fileStream.openAsync(myFile, FileMode.READ); // Call the openAsync() method instead of open()
}

private function onFileProgress(p_evt:ProgressEvent):void // Event handler for the PROGRESS Event
{
_fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, “iso-8859-1″); // Read the contens of the file and add to the contents variable

fileContents_txt.text = _fileContents; // Display the contents. I’ve created a TextArea on the stage for display
}

private function onFileComplete(p_evt:Event):void // Event handler for the COMPLETE event
{
_fileStream.close(); // Clean up and close the file stream
}
[/as]

As for what all that code does – first off, import the required classes:
[as]
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.filesystem.File;
import flash.events.ProgressEvent;
import flash.events.Event;
[/as]

Then we’ll need to create a couple variables. First, the FileStream which we’ll use to read our file and second, a String variable that we’ll use to display the file contents.
[as]
private var _fileStream:FileStream;
private var _fileContents:String;
[/as]

Now we’ll create an onCreationComplete() method. This method will handle the creationComplete event of the application to get things rolling. Inside the onCreationComplete() method we’ll need to create out File object as well as the FileStream object.
[as]
private function onCreationComplete():void // Fired when the application has been created
{
var myFile:File = File.appResourceDirectory;
myFile = myFile.resolve(“mySampleFile.txt”);

_fileStream = new FileStream();
}
[/as]

Still in the onCreationComplete() method, we’ll need to handle those events that will be dispatched while reading the file asynchronously. The events that we will handle are:

  • ProgressEvent.PROGRESS – This will fire as the bytes are read from the file into the buffer.
  • Event.COMPLETE – This will fire when all the bytes of the file have been read into the buffer.

[as]
private function onCreationComplete():void // Fired when the application has been created
{
var myFile:File = File.appResourceDirectory;
myFile = myFile.resolve(“mySampleFile.txt”);

_fileStream = new FileStream();

_fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress)
_fileStream.addEventListener(Event.COMPLETE, onFileComplete);
}
[/as]

The final part of the onCreationComplete() method is to open the file asynchronously. Add the following line to the end of the method:
[as]
_fileStream.openAsync(myFile, FileMode.READ);
[/as]

Now to handle those events and read our file. First, we’ll deal with the PROGRESS event. Inside the onFileProgress() event handler method, we read the availableBytes from the file and add it to our String variable. Notice the second parameter of the readMultiByte() method – "iso-859-1". This specifies the content type of the file that we are reading. I’ll cover some of the other content types in a later post. So, keep an eye out for that one if you’re curious. Finally, we update the TextArea, to show we are actually reading the file.
[as]
private function onFileProgress(p_evt:ProgressEvent):void
{
_fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, “iso-8859-1″);

fileContents_txt.text = _fileContents;
}
[/as]

We still need to clean up after ourselves, so in the onFileComplete() method, we’ll need to close out FileStream object.
[as]
private function onFileComplete(p_evt:Event):void
{
_fileStream.close();
}
[/as]

A very simple example, and it is still only text that we are reading. Next time I’d like to cover how to read different file types. So, like I said, keep an eye our for that post.

Posted in news | Tagged , , , , , | Leave a comment