I have a new project where I need to parse binary files in Javascript, and it would be very convenient to have the Actionscript ByteArray class. I have ported a big portion of the read() functions from the ByteArray class to a new Javascript class. I did not port any of the write() functions since my current project does not need them, but if there is enough demand I would consider porting them too. The class was written with very high performance in mind. Here is a benchmark against one of the only other Javascript binary decoders I could find:
To use it, just instantiate an a3d.ByteArray() and be sure to supply the two parameters: a string representing the binary data, and an enumeration for endianness of either a3d.Endian.BIG or a3d.Endian.LITTLE. You can fetch a binary file by overriding the mimeType on the XmlHttpRequest as demonstrated in Ilmari Heikkinen's binary parser. Here's an example that assumes jQuery is available:
function log(text) {console.log(text);}
var files = [{filename: 'bigendian.blob', endian: a3d.Endian.BIG, name: 'Big Endian'},
{filename: 'littleendian.blob', endian: a3d.Endian.LITTLE, name: 'Little Endian'}];
for (var fileIndex in files) {
var file = files[fileIndex];
$.ajax({async: false, url: file['filename'], beforeSend: function(xhr) {
xhr.overrideMimeType('text/plain; charset=x-user-defined');
}, success: function(data){
log(file['name']);
var ba = new a3d.ByteArray(data, file['endian']);
log(ba.readBoolean());
log(ba.readBoolean());
log(ba.readByte());
log(ba.readShort());
log(ba.readShort());
log(ba.readUnsignedInt());
log(ba.readInt());
log(ba.readInt());
log(ba.readFloat());
log(ba.readFloat());
log(ba.readDouble());
log(ba.readDouble());
log(ba.readDouble());
log(ba.readDouble());
log(ba.readDouble());
log(ba.readDouble());
log('');
}});
}
Parts of the source were originally taken from Ilmari Heikkinen's binary parser.
Here are full source and minified versions:
Comments
Buy cheap Links of London
Buy cheap Links of London jewellery at online jewellerybarnd UK store, including Links of London Necklaces, Links of London Charms, Links of London Earrings, links of london links london Hundreds of links of london jewellery in stock,Free Gift Wrapping, links of london Necklaces links of london Rings links of london Earrings Complete links of london jewellery collection,including necklaces,bracelets,earrings links of london Bracelets links of london Charms links london Sweetie Bracelets Best Discounted UK Tiffany Jewellery Sale Outlet provides designer Tiffany Ring, necklaces and other jewelry in wholesale price. UK Tiffany specializes in Tiffany jewellery Tiffany
I downloaded the sources for
I downloaded the sources for the AS3 jpeg encoder ( AS3 corelib) and analyzed the code. I'm not an expert in writing encoders, actually I think it would have taken me month to start from scratch. 642-359 The ActionScript code seemed tidy and my first step was to replace things not possible in javascript from the code. Then I wrote a little converter function to transform the 1-dimensional canvas image data array into a 2-dimensional array, 642-456 which the original encoder expects. To cut it short: In about 2 hours I had the first working version! It was surprising that it was that easy to get the first js-encoded jpeg displayed in the browser. Of course I didn't want to stop there.642-515 I wanted to optimize things as much as I could to make the encoder fast. This took me several days. I found optimized encoder versions for flash and haxe floating around the net (Faster JPEG Encoding with Flash Player 10) and tried the optimizations used there in my javascript version. 642-524 As you can seen in the benchmarks below I was quite successful.
write() implementation
Great work on this! I was looking for a JS port of the AS' ByteArray and voila! I didn't expect that someone has actually written one. :D
I was thinking about using binary data as a transport between a browser based app and the server, and I think I'll need both a reader/writer for that to work. Any chances of releasing the write() functions? Thanks!
very cool & good tip, thank
very cool & good tip, thank you very much for sharing.
Can I share this post on my JavaScript library?
Awaiting your response. Thank
I'm not sure what you're
I'm not sure what you're asking permission to do, but please feel free to link to this post from just about anywhere.
Post new comment