Module io

Stream and TextStream classes as per CommonJS IO/A as well as a ByteArray based in memory MemoryStream.

Class MemoryStream

Instance Properties

Class Stream

Instance Methods

Instance Properties

Class TextStream

Instance Methods

Instance Properties


MemoryStream (bufferOrCapacity)

In binary stream that reads from and/or writes to an in-memory byte array. If the stream is writable, its internal buffer will automatically on demand.

Parameters

Binary|number bufferOrCapacity the buffer to use, or the capacity of the buffer to allocate . If this is a number, a ByteArray with the given length is allocated. If this is a ByteArray, the resulting stream is both readable, writable, and seekable. If this is a ByteString, the resulting stream is readable and seekable but not writable. If undefined, a ByteArray of length 1024 is allocated as buffer.

MemoryStream.prototype.close ()

Closes the stream, freeing the resources it is holding.


MemoryStream.prototype.closed ()

Returns true if the stream is closed, false otherwise.


MemoryStream.prototype.content


MemoryStream.prototype.flush ()


MemoryStream.prototype.length


MemoryStream.prototype.position


MemoryStream.prototype.read ()


MemoryStream.prototype.readInto ()


MemoryStream.prototype.readable ()


MemoryStream.prototype.seekable ()


MemoryStream.prototype.skip ()


MemoryStream.prototype.writable ()


MemoryStream.prototype.write ()


Stream (stream)

This class implements an I/O stream used to read and write raw bytes.

Parameters

stream a [Node.js Stream](http://nodejs.org/docs/v0.4.8/api/streams.html#streams) that can be readable, writeable or both

Stream.prototype.stream


Stream.prototype.close ()

Closes the stream, freeing the resources it is holding.


Stream.prototype.closed ()

Returns true if the stream has been closed, false otherwise.


Stream.prototype.copy (output)

Reads all data available from this stream and writes the result to the given output stream, flushing afterwards. Note that this function does not close this stream or the output stream after copying.

Parameters

Stream output The target Stream to be written to.

Stream.prototype.flush ()

Flushes the bytes written to the stream to the underlying medium.


Stream.prototype.forEach (fn, [thisObj])

Read all data from this stream and invoke function fn for each chunk of data read. The callback function is called with a ByteArray as single argument. Note that the stream is not closed after reading.

Parameters

Function fn the callback function
Object [thisObj] optional this-object to use for callback

Stream.prototype.read (n)

Read up to n bytes from the stream, or until the end of the stream has been reached. If n is null, a block is read with a block-size specific to the underlying device. If n is not specified, the full stream is read until its end is reached. Reading from a stream where the end has been reached returns an empty ByteString.

Parameters

Number n

Returns

ByteString

Stream.prototype.readInto (buffer, begin, end)

Read bytes from this stream into the given buffer. This method does not increase the length of the buffer.

Parameters

ByteArray buffer
Number begin
Number end

Returns

Number The number of bytes read or -1 if the end of the stream has been reached

Stream.prototype.readable ()

Returns true if the stream supports reading, false otherwise.


Stream.prototype.seekable ()

Returns true if the stream is randomly accessible and supports the length and position properties, false otherwise.


Stream.prototype.skip (num)

Try to skip over num bytes in the stream. Returns the number of acutal bytes skipped or throws an error if the operation could not be completed.

Parameters

Number num bytes to skip

Returns

Number actual bytes skipped

Stream.prototype.writable ()

Returns true if the stream supports writing, false otherwise.


Stream.prototype.write (source, begin, end)

Write bytes from b to this stream. If begin and end are specified, only the range starting at begin and ending before end is written.

Parameters

Binary source The source to be written from
Number begin optional
Number end optional

TextStream (io, options)

A TextStream implements an I/O stream used to read and write strings. It wraps a raw Stream and exposes a similar interface.

Parameters

Stream io The raw Stream to be wrapped.
Object options the options object. Supports the following properties:
  • charset: string containing the name of the encoding to use. Defaults to "utf8".
  • newline: string containing the newline character sequence to use. Defaults to "\n".
  • delimiter: string containing the delimiter to use in print(). Defaults to " ".

TextStream.prototype.close ()

See Stream.prototype.close.


TextStream.prototype.content


TextStream.prototype.copy (output)

Reads from this stream with readLine, writing the results to the target stream and flushing, until the end of this stream is reached.

Parameters

output

TextStream.prototype.flush ()

See Stream.prototype.flush.


TextStream.prototype.forEach (callback, thisObj)

Parameters

callback
thisObj

TextStream.prototype.iterator ()

Returns this stream (which also is an Iterator).


TextStream.prototype.next ()

Returns the next line of input without the newline. Throws StopIteration if the end of the stream is reached.


TextStream.prototype.print ()

Writes all argument values as a single line, delimiting the values using a single blank.


TextStream.prototype.raw


TextStream.prototype.read ()

Read the full stream until the end is reached and return the data read as string.

Returns

String

TextStream.prototype.readInto (buffer)

Not implemented for TextStraim. Calling this method will raise an error.

Parameters

buffer

TextStream.prototype.readLine ()

Reads a line from this stream. If the end of the stream is reached before any data is gathered, returns an empty string. Otherwise, returns the line including the newline.

Returns

String

TextStream.prototype.readLines ()

Returns an Array of Strings, accumulated by calling readLine until it returns an empty string. The returned array does not include the final empty string, but it does include a trailing newline at the end of every line.


TextStream.prototype.readable ()

See Stream.prototype.readable.


TextStream.prototype.seekable ()

Always returns false, as a TextStream is not randomly accessible.


TextStream.prototype.writable ()

See Stream.prototype.writable.


TextStream.prototype.write ()


TextStream.prototype.writeLine (line)

Writes the given line, followed by a newline.

Parameters

line

TextStream.prototype.writeLines (lines)

Writens the given lines, terminating each line with a newline.

This is a non-standard extension, not part of CommonJS IO/A.

Parameters

lines