Local disk IO write performance in MDM Zinc

UPDATE: MDM Zinc’s recent update of 3.0.10 added a new method for writing binary data from a ByteArray directly (windows only), please read this post for the latest info. NOTE that the below is still relevant for other platforms (Mac/Linux)

I have recently been working on local disk IO in MDM Zinc using mdmScript as well as in Adobe AIR, and wow are there quite some differences. Mainly and most notably write performance….. which is very slow in the Zinc runtime.

mdmScript appears to have mainly been developed for simple Flash apps that do basic IO centered around reading and writing text files vs. binary files (images, other large files etc). This is particularly evident when you need to write binary files.

Reading files is not a problem and it is not specific to mdmScript as you can just use standard AS3 such as in the simple example below when you are executing within the Zinc runtime. NOTE, reading in Adobe AIR is fairly similar, however you use the FileStream class. (and AIR is faster)


private var stream:URLStream;

function someReadFunction():void {
   // setup a url request for a local file
   var fileURL:URLRequest = new URLRequest("c:\somePath\to\my\file.jpg");

   // create and URLStream
   stream = new URLStream();

   // register for the complete event
   stream.addEventListener(Event.COMPLETE, handleReadComplete);

   // REMEMBER to also register for IOErrorEvent.IO_ERROR, 
   // and ProgressEvent.PROGRESS events! not here for brevity
   ...

   // fire the load of the file
   stream.load(fileURL);
}

// this method is called when the stream is finished loading
// your file 
private function handleReadComplete(e:Event):void {
   var data:ByteArray = new ByteArray();
   stream.readBytes(data,0,0);
   stream.close();
    
   // remove your event listeners to prevent memory leaks
   stream.removeEventListener(Event.COMPLETE,handleReadComplete);

  // do something with the data you just loaded
}

Writing binary files when running in Zinc is a different matter. You have to convert each byte to a HEX string and then call mdmScript’s mdm.FileSystem.BinaryFile.setData(hexString:String) method. This is painfully slow. Here is a basic sample:

var data:ByteArray = // some byte array of your data;

var hex:String = “”;
var byte:String;
var byteCount:int = 0; // total bytes converted, (start @ zero)
while (byteCount < data.length) { try { byte = data.readUnsignedByte().toString(16); hex += (byte.length == 1) ? "0" + byte : byte; byteCount++ } catch (e:EOFError) { break; // we are done } } // set the converted HEX data mdm.FileSystem.BinaryFile.setData(hex); // tell mdmScript to write the data to a file mdm.FileSystem.BinaryFile.writeData("c:\path\to\somefile"); [/sourcecode] Again this is just super slow once you start writing larger files... however until MDM adds better support for writing binary data, we have to live with this. In Adobe AIR to write files you use the FileStream class and it is orders of magnitude faster than writing with mdmScript under Zinc.

Advertisements

One comment

  1. Pingback: Local IO write performance in Zinc improved « bits.of.info

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s