io/byte-streams

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.