简体   繁体   中英

How to Asynchronously write an ArrayBuffer directly to file using nsIArrayBufferInputStream in Firefox extension

To make a long story short: How to Asynchronously write an ArrayBuffer directly to file using nsIArrayBufferInputStream in Firefox extension ? It seems that MDN does not have any documentation on nsIArrayBufferInputStream.

I know I can use nsIStringInputStream and convert the BufferArray to String, but this poses a big performance hit also converting ArrayBuffer to string using this code:

String.fromCharCode.apply(null, new Uint16Array(buf));

Does not work if the buffer is 500 KB or bigger, so we must loop over it one char at a time:

for (let i = 0; i < buf.length; i++){
    s += String.fromCharCode(buf16[i]);
}

Or, I can use nsIBinaryOutputStream.writeByteArray but it cannot be used with NetUtil.asyncCopy (or can it?)

//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);
}

And the long story is...

I have been trying to use nsIArrayBufferInputStream http://dxr.mozilla.org/mozilla-central/source/netwerk/base/public/nsIArrayBufferInputStream.idl but with no success. the code I tried:

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));
      }
    );
}

The ArrayBuffer data param is the responce from 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);
}

calling like this:

  getBinFile( 'http://....', 'c:\\demo\\');

A file is created but with no contents!

I'm answering myself in case anyone stumbles upon this question...

with help from Josh Matthews (of Mozilla) i found the answer: use byteLength instead of length

istream.setData(data, 0, data.byteLength);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM