Category Archives: JavaScript

BinaryStringView.js

For my work on FirefoxOS, I recently had to use XMLHttpRequest to download binary content in multipart/mixed encoding, like the encoding used for attachments to email messages. I wanted the binary file in the middle, but first had to strip away the ASCII boundary strings before and after it. To make things more complicated, the server was not correctly sending the boundary string as part of the Content-Type header, so I had to actually parse the file to determine the correct boundary string.

Because simple parsing and other string manipulation was involved, the easiest way to do what I needed was to use binary strings. That is, I used code like this:

xhr.responseType = 'string';
xhr.overrideMimeType('text\/plain; charset=x-user-defined');

This is, of course, a horrible thing to do, now that XHR supports array buffers. (Read more about this at MDN.) When we use binary strings, each byte of the binary content is expanded to a two-byte JavaScript character. Yuck. These files I was dealing with have the potential to be megabytes long, and this is for a mobile device, so I really need to pay attention to memory consumption. But I needed string methods like trim(), slice(), indexOf() and lastIndexOf(). So I ignored my better judgement and went with binary strings.

Today, I came up with an alternative: my code now downloads the file as an array buffer, and then I wrap the ArrayBuffer with a custom BinaryStringView class (modeled after other ArrayBufferView types) that implements the String methods I need on top of Uint8Array. I had to write indexOf() and lastIndexOf() by hand, but I get the string-manipulation methods I need and each character only occupies a byte of memory. I’m probably trading off performance for memory here, but for mobile, it is worth it.

I’ve seen a lot of legacy code using binary strings, and this BinaryStringView wrapper seems like it could be a generally-useful class to help migrate that code, so I’ve put it on github. The caveat is that I’ve only implemented a few string methods. Maybe someone will fork this and finish it for me!