text-streams

The text-streams module provides streams for reading and writing text using particular character encodings.

API Reference

Classes

TextReader

Constructors
TextReader(inputStream, charset)

Creates a buffered input stream that reads text from a backing stream using a given text encoding.

inputStream : stream

The backing stream, an nsIInputStream. It must already be opened.

[ charset : string ]

inputStream is expected to be in the character encoding named by this value. If not specified, "UTF-8" is assumed. See nsICharsetConverterManager.idl for documentation on how to determine other valid values for this.

Methods
close()

Closes both the stream and its backing stream.

read(numChars)

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

[ numChars : number ]

The number of characters to read. If not given, the remainder of the stream is read.

Returns: string

The string read. If the stream is at the end, the empty string is returned.

Properties
closed : boolean

True if the stream is closed.

TextWriter

Constructors
TextWriter(outputStream, charset)

Creates a buffered output stream that writes text to a backing stream using a given text encoding.

outputStream : stream

The backing stream, an nsIOutputStream. It must already be opened.

[ charset : string ]

Text will be written to outputStream using the character encoding named by this value. If not specified, "UTF-8" is assumed. See nsICharsetConverterManager.idl for documentation on how to determine other valid values for this.

Methods
close()

Flushes the backing stream's buffer and closes both the stream and the backing stream. If the stream is already closed, an exception is thrown.

flush()

Flushes the backing stream's buffer.

write(str)

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

str : string

The string to write.

writeAsync(str, callback)

Writes a string on a background thread. After the write completes, the backing stream's buffer is flushed, and both the stream and the backing stream are closed, also on the background thread. If the stream is already closed, an exception is thrown immediately.

str : string

The string to write.

[ callback : callback ]

callback, if given, must be a function. It's called as callback(error) when the write completes. error is an Error object or undefined if there was no error. Inside callback, this is the TextWriter object.

Properties
closed : boolean

True if the stream is closed.