io/byte-streams

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Experimental

Provides streams for reading and writing bytes.

function readBinaryDataFromFile (filename) {
  var fileIO = require("sdk/io/file");
  var data = null;
  if (fileIO.exists(filename)) {
    var ByteReader = fileIO.open(filename, "rb");
    if (!ByteReader.closed) {
      data = ByteReader.read();
      ByteReader.close();
    }
  }
  return data;
}
function writeBinaryDataToFile(data, filename) {
  var fileIO = require("sdk/io/file");
  var ByteWriter = fileIO.open(filename, "wb");
  if (!ByteWriter.closed) {
    ByteWriter.write(data);
    ByteWriter.close();
  }
}

Globals

Constructors

ByteReader(inputStream)

Creates a binary input stream that reads bytes from a backing stream.

You can also create ByteReader objects using io/file's open() function.

Parameters

inputStream : stream
The backing stream, an nsIInputStream.

ByteWriter(outputStream)

Creates a binary output stream that writes bytes to a backing stream.

You can also create ByteWriter objects using io/file's open() function.

Parameters

outputStream : stream
The backing stream, an nsIOutputStream.

ByteReader

Methods

close()

Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.

read(numBytes)

Reads a string from the stream. If the stream is closed, an exception is thrown.

Parameters

numBytes : number
The number of bytes to read. If not given, the remainder of the entire stream is read.

Returns

string : A string containing the bytes read. If the stream is at the end, returns the empty string.

Properties

closed

True if the stream is closed.

ByteWriter

Methods

close()

Closes both the stream and its backing stream. If the stream is already closed, an exception is thrown.

write(str)

Writes a string to the stream. If the stream is closed, an exception is thrown.

Parameters

str : string
The string to write.

Properties

closed

True if the stream is closed.

Document Tags and Contributors

 Contributors to this page: wbamberg, jmh, backy0175
 Last updated by: wbamberg,