Wahoo! AIR 2.0.2 and Flash Player 10.1 have officially been released. These two releases have been a long time coming.
The AIR 2 update makes AIR a much more viable option as a desktop software platform.
Flash 10.1 seems like it is actually a whole new beginning for Flash because it has been cut up, optimized, and expanded on a ton. Exciting times in the Flash world.
With so much going on with the new releases and the cool features they have (multi-touch/gesture, mobile, networking, etc) it is easy to over look the Native Process. I definitely overlooked it when I was looking at the AIR 2 Beta 1 release notes. But since then I have been playing with Native Process in a few different projects. And it is FAST! In one project, Flex is calling a shell script and receiving/parsing 200k + line items in … oh around 10 SECONDS!
So lets take a look at an examples of what I am talking about and a stumbling block that I came upon.
Example 1 – AIR to Shell Commands
For the last two years I have learned to love my favorite Application on my Mac — Terminal. I love it because it is so powerful, and already has so much functionality built into it. And even if it doesn’t do something, there is probably a Linux or OSX user out there that has created a script to do just what I am looking for. So being able to tap into shell commands directly is… well…really cool and opens up a lot of possibilities.
A great example of this is already pre-baked and packaged for us by Adobe. SearchCentral is an app that uses some native file listing and shell commands to search for files on your computer. Not a revolutionary app — it basically just mimics Spotlight. But it does show some good examples of how to interact with a Native Process. If you want to tinker with NativeProcesses, that is the place to start.
The Stumbling Block – Chunked Up Data
A major issue came along when AIR started requesting large data sets over Standard In/Out. The issues is that the system only sends a chuck of data at a time. And this chunk may not be a complete object, line item, or whatever you are sending. For example, if we have a response with 300 characters, maybe only 200 of those will get put into the output. If so, the last one hundred will come across on the next event cycle. How do we know if we have a complete object? The solution I found was to separate each object with a defined separator and hold onto data until we have its full data-load. To you networking guru’s this may sound very familiar, because it is the same concept as packet splitting on the OSI layer. Here is a simple way to handle this “chunked up” data on a STANDARD_OUTPUT_DATA event handler:
// Read our bytes from the native process into the buffer
np.standardOutput.readBytes(processBuffer, processBuffer.length);
// Keep a remainingOutput class variable for orphaned data
// append that to a temp holder and clear remainingOutput
var tempOutput:String = remainingOutput + new String(processBuffer);
remainingOutput = "";
// Orphaned data + whats new in our buffer
var l:Number = tempOutput.length;
// Look for our bufferSeperator class variable’s last index in out output
var lastIndex:Number = tempOutput.lastIndexOf(bufferSeperator) + bufferSeperator.length;
// If the end of our buffer doesn’t end on the end of our current buffer string
// then we have some orphaned data that we need to hold onto
if(lastIndex != tempOutput.length) {
// Grab the orphaned data
remainingOutput = tempOutput.slice(lastIndex + 1);
// Grab everything before the orphaned data
tempOutput = tempOutput.slice(0, tempOutput.lastIndexOf(bufferSeperator) + 1);
}
// Clear out our buffer so we don’t get an overflow
processBuffer.clear();
// the tempOutput var is our final set of data that includes any previously
// orphaned data and the rest of what we could find in this latest buffer
// load. So we can dispatch out an event or set a var to keep track of this
// tmpOutput; // Do something with this
}
In conclusion, AIR 2.0 is awesome. And Native Process, while not as sexy as some of the other new features, is a game changer for what AIR Developers can build!

