0

I have this piece of javascript code

var file = Components.classes["@mozilla.org/file/local;1"]
           .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath( this.savefile );
if ( file.exists() == false ) {
   return null;
}
var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
           .createInstance( Components.interfaces.nsIFileInputStream );
is.init( file,0x01, 00004, null);
var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
           .createInstance( Components.interfaces.nsIScriptableInputStream );
sis.init( is );
output = sis.read( sis.available() );

sis.close();
is.close();
this.filterData = output;
return output;

Actually the file that i am reading is a binary file and has lets say 350 bytes. Now the 19 byte is "zero", so what happens is in the above code i get only 18 bytes in output.

when i tried debugging sis.available does return 350. But sis.read only reads upto Zero byte.

I want the way to read whole of 350 bytes in output.

Ashish Yadav
  • 1,667
  • 6
  • 20
  • 26
  • Just to be clear, savefile is local? And this is for a firefox add-on, right? – Jonathan Fingland Feb 15 '13 at 09:26
  • @JonathanFingland Right. – Ashish Yadav Feb 15 '13 at 09:28
  • 1
    This may or may not be related, but see https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIScriptableInputStream where read() comes with the warning: If the data contains a null byte, then this method will return a truncated string. You may want to use readBytes() instead. – Jonathan Fingland Feb 15 '13 at 09:34
  • @JonathanFingland readBytes() looks interesting (just what i want) but i am aware of its return type i.e ACString. Can i take that in var output and it will be a string. Actually i am new to this. – Ashish Yadav Feb 15 '13 at 09:42
  • when in doubt, console.log/debug the object, but you should be able to to do output.value to access the contents of the ACString (I say *should* because getting info on ACString is proving harder than expected) – Jonathan Fingland Feb 15 '13 at 10:01
  • The most useful references on ACString are https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIEffectiveTLDService where the remark at the bottom says "All returned strings are encoded in ASCII/ACE and normalized according to RFC 3454" and https://developer.mozilla.org/en-US/docs/nsACString which indicates that the = operator is overloaded (probably just returns a string) – Jonathan Fingland Feb 15 '13 at 10:07
  • @JonathanFingland Yes right. I simply took the return value of readBytes in a var and it worked. Thanks a lot !!! – Ashish Yadav Feb 15 '13 at 10:54

2 Answers2

1

EDIT

See https://developer.mozilla.org/en-US/docs/Reading_textual_data

Quote:

var charset = /* Need to find out what the character encoding is. Using UTF-8 for this example: */ "UTF-8";
var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
               .createInstance(Components.interfaces.nsIConverterInputStream);
// This assumes that fis is the nsIInputStream you want to read from
is.init(fis, charset, 1024, 0xFFFD);
is.QueryInterface(Components.interfaces.nsIUnicharLineInputStream);

if (is instanceof Components.interfaces.nsIUnicharLineInputStream) {
  var line = {};
  var cont;
  do {
      cont = is.readLine(line);

      // Now you can do something with line.value
  } while (cont);
}

This avoids the null byte problems, is unicode safe, and works with less esoteric object types.

Original:

As per my comment above, and in light of your edit,

See https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIScriptableInputStream where read() comes with the warning: If the data contains a null byte, then this method will return a truncated string. You may want to use readBytes() instead.

Alternatively, here's another way to do it:

var ph = Components.classes["@mozilla.org/network/protocol;1?name=file"]
          .createInstance(Components.interfaces.nsIFileProtocolHandler);

var file_to_read = ph.getURLSpecFromFile(file);

var req = new XMLHttpRequest();
req.onerror = function(e) {
        onError(e);
}


req.onreadystatechange = function() {
        if (log.readyState == 4) {
        //...
        }
}

req.open("GET", file_to_read, true);
Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
0

I may be wrong, but have you tried sending a simple GET request? In AJAX? Or do you strictly want to use JS?

EDIT: Refer to this - How do I load the contents of a text file into a javascript variable?

Community
  • 1
  • 1
Vlad
  • 795
  • 1
  • 12
  • 35