1

Say, I have a file that is 500 bytes in size on my local hard drive and I want to read first 100 bytes from it without loading the whole file into memory. How to accomplish that in JavaScript with the help of UniversalXPConnect? In Firefox only, of course.

4
  • Just verifying that you know this is impossible unless you are writing a Firefox extension? Commented Jul 9, 2011 at 22:45
  • @Brock Adams: UniversalXPConnect gives one full access - same as a Firefox extension. Commented Jul 11, 2011 at 6:47
  • @Wladimir, UniversalXPConnect only runs, with the necessary permissions, in chrome JS. That is it has to be an add-on or plugin. IE, an extension. Commented Jul 11, 2011 at 7:34
  • @Brock: Or an intranet website, given suitable browser configuration - it needs to call enablePrivilege() then. Commented Jul 11, 2011 at 8:55

1 Answer 1

0

Assuming that you want to read ASCII text data (no character set conversion):

var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/foo/bar");
var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                        .createInstance(Components.interfaces.nsIFileInputStream);
fstream.init(file, -1, 0, 0);
var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
                        .createInstance(Components.interfaces.nsIScriptableInputStream);
sstream.init(fstream);
var data = sstream.read(100);
sstream.close();

Further information: https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.