jswin
jswin copied to clipboard
Safer ab2str
I encountered a problem where the buffer was to big (~34k) and using the normal code got me a Maximum call stack exceeded (think thats what it was) error. So I came up with the following.
function ab2str(buf) {
if (buf.byteLength == 0) return ""
var bufView = new Uint8Array(buf);
if (bufView[0]==0) return "";
var unis = [];
var step = 32768;
var str = "";
unis.push(bufView[0]);
for (var i = 1; i < bufView.length; i++) {
if (bufView[i] == 0) break;
unis.push(bufView[i]);
if (i % step == 0) {
str += String.fromCharCode.apply(null, unis);
unis = [];
}
}
str += String.fromCharCode.apply(null, unis);
return str;
}