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');
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!