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:
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:
length: uint - The number of bytes to read from the stream
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.
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.
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.
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.
privatefunction onCreationComplete():void// Fired when the application has been created
{
var myFile:File = File.appResourceDirectory;
myFile = myFile.resolve("mySampleFile.txt");
_fileStream = new FileStream();
}
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.
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.
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.
The ALPHA release of Apollo, Adobe's cross-operating system runtime allows for some pretty sweet functionality:
Drag-and-drop support
Rich clipboard access
Desktop and system
shortcuts
What I was excited about was the File API. The ability to access, read and write to files is something that is completely necessary in a desktop applications, not to mention, exciting to just play with in ActionScript. Its also something that allows for those Web applications that will be ported to desktop applications, to break out of the constraints of the Web (browser security etc.).
I'd like to give a simple example of the File API and get you started on the how's. Its not that difficult, so lets get started. I'll show you the code...then explain.
Next, we'll need to create our File object so we can let our application know where the file we want to read exists. We'll use the file.appResourceDeirectory to locate the file in relation to our application, then the resolve method of the File object myFile to locate the actual file.
Now we'll need to actually open the file using the FileStream object. So, we create a new FileStream object, then call the open() method and pass it the File object as well as the FileMode of READ.
We have our file open, so lets go ahead and read the contents of the file. The file is a text file, so we'll simple read the file contents into a String variable using the readUTFBytes() method of out FileStream object. Depending on the type of file and the contents of that file, you will want to use a different read method. Like I said, we'll keep it simple for now. The readUTFBytes() method requires the number of bytes to read. We'll pass the bytesAvailable property of the FileStream object for this parameter. The bytesAvailable prooperty, in this case, is the total bytes for the file. Finally set the value returned from the readUTFBytes() call equal to our String variable.
var fileContents:String = fileStream.readUTFBytes(fileStream.bytesAvailable);
Alright! Now we have our String variable that contains the contents of our text file. From this point you can do anything you want with it. In this example, I've just set the text property of a TextArea to the value of our String variable
There is one final thing to do, and that is clean up. Make sure you call the close() method of the FileStream object once you are done reading the file.
So that is all you need to get started with the File API. There are a few more things that I'd like to go into detail about, so I'll make sure to post about reading file types other than plain text. I'd also like to create an example of opening a file asynchronously, so look for posts on these subjects soon.
Lots of cool stuff with JavaScript and HTML integration. The HTML component, being able to load in both Google and Yahoo maps at the same time. Nice stuff, but it seems to me like it is just asking for spaghetti code. Of course that is up to the code management of the developer. Pretty cool presentation, but not a whole lot of meat as far as what I'm interested in.
Mike Chambers got up and gave a great introduction to the Apollo team and let everyone know about the great swag that was given out to all the attendees:
Adobe really pumped up the swag for Apollo Camp:
Flex Builder with charting
Apollo CD with goodies and docs
Apollo t-shirt
Apollo Book
Kevin Lynch dropped in to present a good overview about why Apollo and why Adobe might present the bits and bytes of Apollo early. Adobe revolutionizes how the work engages with ideas and information across multiple platforms (This is a huge push...cross platform) and devices.
Client tchnolgies
Server technologies
Applications
Pretty much the standard pitch about Adobe technolgies, but hey it's Kevin Lynch, he's been doing this for 10 years. One of the big pushes is for PDF, PDF is being released to ISO.
Apollo is trying toenable web to the desktop. There are a couple differenct engines that are inside of Apollo based on what people are trying to create. Mostly what is happening is people are trying to create tools that bring in more desktop functionality into the web space, also includein gthe mobile space.
- Establishing a first class presence for your application.
Desktop Rich Internet Applications (dRIAs) is the direction Adobe is looking.
SWF and HTML - top level to build applications in Apollo.
SWF - AS, FLex, XM, Video, Audio,
HTML - JS, CSS ,XML
Local Fiel access, netowork status, drag and drop, clipboard access, background processin, mutiple window support, custom window crome.
Examples:
Companion applicaiton for amazon watchlist
Feed reader with HTML, Scriptaclous (Web app running as a desktop app)
Flex word processor (Buzzword) desktop companion to web application.
PDF Support sample applicaiton - finnancial data applicaiton, PDF support will be supplied via the Acrobat plugin (so if you don't have it you'll need to install it)
Timeline:
H1 - 1st 1/2 of the year (labs next week), Moxie Alpha release, creative suite 3
H2 - Flex (Moxie), "Phil0" (Apollo - internet TV app), Flash Media Servier
Q&A:
No plans to support QuickTime
Centralized distribution and installation
Installation super smooth and clean
Migration from Flex 2 into Moxie...flex team says "Very easy!"
Kevin Lynch says "Whats Moxie?" really, someone asked a question about migrating current Flex applications to Moxie and he asked what it was...hehe.
Apollo, most likey will not be the name of the product, someone else has taken it. So they are look at another "cool" name, well just have to wait and see what it is I guess.
We're in San Francisco for Adobe's Apollo camp. Just had lunch with a couple of the guys from the Flash Player QA team. Definitely a fun bunch to to meet and talk with.
I'll be sure to update with any nifty things that I get out of the Apollo presentations. only a couple of hours of waiting left.
I attended the Adobe Developer Week session for Builder Killer Desktop Apps.
What is Apollo?
A Cross-OS run-time that allows a developer to leverage existing assets and knowledge to build and deploy desktop applications.
Apollo will allow you to leverage Flash, HTML, PDF, & Rich Media. With that you have the ability to create RIA's containing video and other rich media such as 3D. You are also able to create applications that are not constrained by the "Square Box". So you can create applications with transparencies, rounded corners and all the other fun stuff.
Some of the other details are listed below:
It will be free!
You get to use your existing knowledge to build applications
You can build applications in many technologies (Flex, Flash, AJAX, HTML...)
Access to many of the usually expected OS level operations (File management etc.)
It is cross platform:
Windows OS (2K, XP & Vista)
Mac OS (Tiger)
Linux (No Specifics)
A developer version will be released on labs.macromedia.com before the end of the year
The 1.0 release is planned for the first half of 2007
It's not Central 2.0. Completely different code base
The look and feel of the application is completely controlled by the developer
Command line tools will be provided to build and package the application
The initial release it targeted to the desktop with future releases supporting additional devices
Here are some screen shots of actual Apollo apps (notice these are not running in a browser yay!):
Audio player with visualizer.
Audio player with visualizer.
Desktop RIA example. Notice the transparency and rounded corners!
Enterprise level travel application for salesforce.com to connect sales people.
I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.