TextStream

Moduleejs.io
Namespace"ejs.io"
Definition class TextStream
SpecifiedEjs-11.
InheritanceTextStream inherit Object

TextStreams interpret data as a stream of Unicode characters.

They provide methods to read and write data in various text encodings and to read/write lines of text appending appropriate system dependent new line terminators. TextStreams can be stacked upon other Streams such as files, byte arrays, sockets, or Http objects.


Properties

QualifiersPropertyTypeDescription
public static const LATIN1StringText encoding formats for use with $encoding.
public static const UTF_16StringText encoding formats for use with $encoding.
public static const UTF_8StringText encoding formats for use with $encoding.

TextStream Methods

QualifiersMethod
public available(): Number
 Return the number of bytes available to read.
public close(graceful: Boolean): Void
 Close the input stream and free up all associated resources.
public get encoding(): String
 Get the current text encoding.
public set encoding(encoding: String): Void
 Set the current text encoding.
public flush(): Void
 Flush the stream and the underlying file data.
public read(buffer: ByteArray, offset: Number, count: Number): Number
 Read characters from the stream into the supplied byte array.
public readLine(): String
 Read a line from the stream.
public readLines(numLines: Number): Array
 Read a required number of lines of data from the stream.
public readString(count: Number): String
 Read a string from the stream.
public skip(n: Number): Void
TextStream(stream: Stream)
 Create a text filter stream.
public write(data: Array): Number
 Write characters to the stream.
public writeLine(lines: Array): Number
 Write text lines to the stream.

Method Detail

public available(): Number

Return the number of bytes available to read.

Returns
A the number of available bytes.

public close(graceful: Boolean): Void

Close the input stream and free up all associated resources.

Parameters
graceful: Boolean [default: true]

get public encoding(): String

Get the current text encoding.

Returns
The current text encoding as a string.

set public encoding(encoding: String): Void

Set the current text encoding.

Parameters
encoding: String string containing the current text encoding. Supported encodings are: utf-8. [default: UTF_8]

public flush(): Void

Flush the stream and the underlying file data.

Description
Will block while flushing. Note: may complete before the data is actually written to disk.

public read(buffer: ByteArray, offset: Number, count: Number): Number

Read characters from the stream into the supplied byte array.

Description
This routine is used by upper streams to read data from the text stream as raw bytes.
Parameters
buffer: ByteArray Destination byte array for the read data.
offset: Number Offset in the byte array to place the data. If < 0, then read to the write position.
count: Number Number of bytes to read.
Returns
A count of characters actually written.

Throws

IOError: if an I/O error occurs.


public readLine(): String

Read a line from the stream.

Returns
A string containing the next line without the newline character. Return null on eof.

Throws

IOError: if an I/O error occurs.


public readLines(numLines: Number): Array

Read a required number of lines of data from the stream.

Parameters
numLines: Number of lines to read. Defaults to read all lines.
Returns
Array containing the read lines.

Throws

IOError: if an I/O error occurs.


public readString(count: Number): String

Read a string from the stream.

Parameters
count: Number of bytes to read. Returns the entire stream contents if count is -1.
Returns
A string.

Throws

IOError: if an I/O error occurs.


public skip(n: Number): Void

TextStream(stream: Stream)

Create a text filter stream.

Description
A Text filter stream must be stacked upon a stream source such as a File.
Parameters
stream: Stream stream data source/sink to stack upon.

public write(data: Array): Number

Write characters to the stream.

Description
The characters will be encoded using the current TextStream encoding. endpoint can accept more data.
Parameters
data: Array String to write.
Returns
The total number of elements that were written.

Throws

IOError: if there is an I/O error.


public writeLine(lines: Array): Number

Write text lines to the stream.

Description
The text line is written after appending the system text newline character or characters.
Parameters
lines: Array Text lines to write.
Returns
The number of characters written or -1 if unsuccessful.

Throws

IOError: if the file could not be written.