长话短说: 如何使用Firefox扩展中的nsIArrayBufferInputStream将ArrayBuffer直接异步写入文件? 似乎MDN没有关于nsIArrayBufferInputStream的任何文档。
我知道我可以使用nsIStringInputStream并将BufferArray转换为String,但这会带来很大的性能影响 还使用以下代码将ArrayBuffer转换为字符串:
String.fromCharCode.apply(null, new Uint16Array(buf));
如果缓冲区为500 KB或更大,则不起作用,因此我们必须一次循环一个char:
for (let i = 0; i < buf.length; i++){
s += String.fromCharCode(buf16[i]);
}
或者,我可以使用nsIBinaryOutputStream.writeByteArray,但它不能与NetUtil.asyncCopy一起使用(或者可以吗?)
//this works ok, but is synchronous :-(
function writeBinFile(aFile, data){
Components.utils.import("resource://gre/modules/FileUtils.jsm");
let nsFile = Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath");
if(typeof aFile === 'string') aFile = nsFile(aFile);
var stream = FileUtils.openSafeFileOutputStream(aFile, FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE);
var binaryStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream);
binaryStream.setOutputStream(stream);
binaryStream.writeByteArray(data, data.length);
FileUtils.closeSafeFileOutputStream(stream);
}
长话故事是......
我一直在尝试使用nsIArrayBufferInputStream http://dxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsIArrayBufferInputStream.idl 但没有成功。我试过的代码:
function fileWrite(file, data, callback) {
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
let nsFile = Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath");
if (typeof file == 'string') file = new nsFile(file);
let ostream = FileUtils.openSafeFileOutputStream(file)
let istream = Cc["@mozilla.org/io/arraybuffer-input-stream;1"].createInstance(Ci.nsIArrayBufferInputStream);
istream.setData(data, 0, data.length);
let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
bstream.setInputStream(istream);
//NetUtil.asyncCopy(istream, ostream,
NetUtil.asyncCopy(bstream, ostream,
function(status) {
if (callback) callback(Components.isSuccessCode(status));
}
);
}
ArrayBuffer data
参数是来自XMLHttpRequest的响应:
function getBinFile(url, dir) {
let nsFile = Components.Constructor("@mozilla.org/file/local;1", Ci.nsILocalFile, "initWithPath");
let oReq = new XMLHttpRequest();
oReq.open("GET", url, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
//let byteArray = new Uint8Array(arrayBuffer);
let byteArray = arrayBuffer;
dir = /\\$/.test(dir) ? dir: dir + '\\';
let file = nsFile(dir + decodeURIComponent(url.split('/').pop()));
fileWrite(file, byteArray);
}
};
oReq.send(null);
}
这样打电话:
getBinFile( 'http://....', 'c:\\demo\\');
创建了一个文件但没有内容!
答案 0 :(得分:1)
我正在回答自己以防万一有人偶然发现这个问题......
在Josh Matthews(Mozilla)的帮助下,我找到了答案: 使用byteLength
代替length
istream.setData(data, 0, data.byteLength);