Skip to content

Instantly share code, notes, and snippets.

@delagoya
Created February 11, 2009 15:35
Show Gist options
  • Save delagoya/62072 to your computer and use it in GitHub Desktop.
Save delagoya/62072 to your computer and use it in GitHub Desktop.
package mz
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.Dictionary;
public class MzXml
{
/**
* A random access parser for mzXML files.
*
*/
public var mz:Array;
public var mzi:Array;
private var f:File;
private var fs:FileStream;
private var fsize:int;
private var index:Dictionary; // [[id, offset, length] , [id, offset,length],...]
public function MzXml(file:File)
{
this.f = file;
this.fs = new FileStream();
// open the file to get the size
fs.open(this.f,FileMode.READ);
this.fsize = fs.bytesAvailable;
fs.close()
parseIndex();
}
private function parseIndex():void
{
this.index = new Dictionary;
// find index offset
fs.open(f,FileMode.READ);
fs.position = new Number(fsize - 1000);
var tmp:String = fs.readUTFBytes(fs.bytesAvailable);
//<indexOffset>33573953</indexOffset>
var result:Array = tmp.match(/<indexOffset>(\d+)<\/indexOffset>/);
var iof:Number = new Number(result[1]);
// OK, now read in the index
fs.position = iof;
tmp = fs.readUTFBytes(fs.bytesAvailable);
fs.close();
// make a valid XML string to use by matching only the index structure
var xml:XML = XML(tmp.match(/(<index>.+<\/index>)/m)[0]);
// iterate through the index to get the offsets
var i:int = 0;
for each (var offset:XML in xml.offset) {
index[i] = [offset.@id, new int(offset.toString()),null];
if (i > 0) {
index[i-1][2] = index[i][1] - index[i-1][1];
}
i += 1;
}
// fix the last index length
index [i][2] = iof - index[i][1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment