# 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;

// 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

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

// fire the load of the file
}

var data:ByteArray = new ByteArray();
stream.close();

// remove your event listeners to prevent memory leaks

// 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.