我听说使用charCodeAt将字符串转换为UInt8Array非常慢我试图用nsIStringInputStream
找出一种方法,我认为这种方式最简单?
所以我试图避免这个:
var str = atob('rawr');
var data = new Uint8Array(str.length);
for (var i = 0, e = str.length; i < e; ++i) {
data[i] = str.charCodeAt(i);
}
我不确定我是否做得对,请告知:
var {Constructor: CC} = Components;
var BinaryInputStream = CC('@mozilla.org/binaryinputstream;1', 'nsIBinaryInputStream', 'setInputStream');
var StringInputStream = CC('@mozilla.org/io/string-input-stream;1', 'nsIStringInputStream', 'setData');
var str = atob('rawr');
var _inputStreamScriptable = new StringInputStream(str, str.length);
var _inputStreamBinary = new BinaryInputStream(_inputStreamScriptable);
var data = new Uint8Array(str.length);
data.set(_inputStreamBinary.readByteArray(str.length));
感谢