Experimental
Provides streams for reading and writing bytes.
Globals
Constructors
ByteReader(inputStream)
Creates a binary input stream that reads bytes from a backing stream.
Parameters
inputStream : stream
The backing stream, an nsIInputStream
.
ByteWriter(outputStream)
Creates a binary output stream that writes bytes to a backing stream.
Parameters
outputStream : stream
The backing stream, an nsIOutputStream
.
Functions
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.
Sample
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; };
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.
Sample
function writeBinaryDataToFile (data, filename) { var fileIO = require("sdk/io/file"); var ByteWriter = fileIO.open(filename, "wb"); if (!ByteWriter.closed) { ByteWriter.write(data); ByteWriter.close(); } };