Write with AIR, Read with Flash Player 9
If you're like me, you are reworking an older Flex 2 application that provides address/street lookups to the public in order to comply with state regulations of one sort or another. Right now, just like I am, you're struggling to figure out how to write a file out of an AIR app, that can be read in a Flash Player 9 browser app -- and be identical to what you started with.
And if you are UNLIKE me, you are having some success. (Lucky bastard!)
The problem isn't reading or writing a file, per se. The problem is reading and writing a file that actually makes sense to both apps. Actually, truth be told, the problem is that the data that comprises the file amounts to more than 500k -- a bit large for a casual download, especially when compressing it in a ByteArray brings it down to about 27k.
But forget the compression a minute (I did), because the trouble happens even without compression. It just don't work.
I use symmetrical ByteArray setups (endian, etc.), and writeObject and writeInt to the thing. The first thing I try to read is an int (readInt), and it don't work. Something is fouled.
Suggestions? Recommendations? Ideas? Help?!?
The (simplified) code I'm using for writing it is:
protected function writeFile():void {
var theOutput:ByteArray = createStructure(theAddresses.list);
theStream = new FileStream();
theStream.endian = Endian.BIG_ENDIAN;
theStream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
theStream.open(outFile, FileMode.WRITE);
theStream.writeBytes(theOutputString);
theStream.close();
}protected function createStructure(theStreets:Dictionary):ByteArray {
var theBytes:ByteArray = new ByteArray();
theBytes.endian = Endian.BIG_ENDIAN;
theBytes.writeInt(DictionaryUtil.getKeys(theStreets).length);
for each (var theStreet:Street in theStreets)
theBytes.writeObject(theStreet);theBytes.position = 0;
return (theBytes);
}
and (simplified) for reading it:
private function init():void {
var theURL:URLRequest = new URLRequest("../addresspoints.xml");
var theLoader:URLLoader = new URLLoader(theURL);
theLoader.dataFormat = URLLoaderDataFormat.BINARY;
theLoader.addEventListener(Event.COMPLETE, fileLoadedHandler);
}private function fileLoadedHandler(theEvent:Event):void {
var theLoader:URLLoader = theEvent.target as URLLoader;
var theBytes:ByteArray = ByteArray(theLoader.data);
var theNumStreets:int = theBytes.readInt();
...
}
theNumStreets should be about 524, but is read to be -319993993.
Sigh.
about this entry
you’re currently skimming and ignoring “Write with AIR, Read with Flash Player 9,”
- published:
- 09.26.08 / 11pm
- category:
- actionscript, air, flex
someone cares
click to care. click to comment. | comments rss [?] | trackback uri [?]