Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:43:50+00:00 2026-05-30T03:43:50+00:00

Hi: I’m writing a firefox extension (my first) that among other things, gets an

  • 0

Hi: I’m writing a firefox extension (my first) that among other things, gets an image url read into a byte Array, and then writes out that binary information to a file. I have used this example, which looks as if it works, but the file is not right, as if some sort of character conversion has been performed on it.

My version is as follows:

testWriteBinaryFile : function (dir, filename, data) {
    var aFile = Components.classes["@mozilla.org/file/local;1"].
        createInstance(Components.interfaces.nsILocalFile);

    aFile.initWithPath( "/home/mike/mypicture.gif" );
    aFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600);
    Firebug.Console.log("Binary writing: file set up");

    var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].
                 createInstance(Components.interfaces.nsIFileOutputStream);
    stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate
    Firebug.Console.log("Binary writing: stream set up");

    Firebug.Console.log("Data length: " + data.length);
    Firebug.Console.log("Data byte 0: " + data[0].toString(16));
    Firebug.Console.log("Data byte 1: " + data[1].toString(16));
    Firebug.Console.log("Data byte 2: " + data[2].toString(16));
    Firebug.Console.log("Data byte 3: " + data[3].toString(16));
    Firebug.Console.log("Data byte 4: " + data[4].toString(16));

    stream.write(data, data.length);
    Firebug.Console.log("Binary writing: stream written")
    if (stream instanceof Components.interfaces.nsISafeOutputStream) {
        stream.finish();
    } else {
        stream.close();
    }
    Firebug.Console.log("Binary writing: done.");
}

I know the data buffer itself is OK by printing out the first few bytes, but the file is completely different once on disk. Any ideas or indeed any other ways to write out a binary file?

I know this is a synchronous write, but the files are small, (20Kb)… but that’s the only example. If I can fix this I have the extension finished! Any and all help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:43:51+00:00Added an answer on May 30, 2026 at 3:43 am

    The code as you have it here works fine – e.g. it if data is "foo\0\n\rbar" that string is correctly written to disk. Maybe the issue is that nsIOutputStream.write() expects a string as parameter, you are talking about a byte array however. If your data variable is indeed a byte array then maybe it is easiest to convert it into a string first:

    data = String.fromCharCode.apply(String, data);
    

    After that operation data is a string corresponding to the original byte array and can be used with nsIOutputStream.write(). The more efficient (but probably unnecessary in this case) approach would be using nsIBinaryOutputStream, something like this:

    Components.utils.import("resource://gre/modules/FileUtils.jsm");
    
    var stream = FileUtils.openSafeFileOutputStream(aFile, FileUtils.MODE_WRONLY |
                                                           FileUtils.MODE_CREATE);
    
    var binaryStream = Components.classes["@mozilla.org/binaryoutputstream;1"]
                                 .createInstance(Components.interfaces.nsIBinaryOutputStream);
    binaryStream.setOutputStream(stream);
    binaryStream.writeByteArray(data, data.length);
    
    FileUtils.closeSafeFileOutputStream(stream);
    

    I am using FileUtils module in this example, it makes things a lot simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.