Class: ReadableCharacterChannel

Represents a channel, which could be used to read characters through a given ReadableByteChannel.

Constructor

Constructs an io:ReadableCharacterChannel from a given io:ReadableByteChannel and Charset.

init (ReadableByteChannel byteChannel, string charset)
  • byteChannel ReadableByteChannel
  • The io:ReadableByteChannel, which would be used to read the characters

  • charset string
  • The character set, which is used to encode/decode the given bytes to characters

Methods

read

Reads a given number of characters.

readString

Read the entire channel content as a string.

readAllLines

Read the entire channel content as a list of lines.

readJson

Reads a JSON from the given channel.

readXml

Reads an XML from the given channel.

readProperty

Reads a property from a .properties file with a default value.

lineStream

Return a stream of lines that can be used to read all the lines in a file as a stream.

readAllProperties

Reads all properties from a .properties file.

close

Closes the character channel.

read

function read(int numberOfChars) returns string | Error
Isolated Function

Reads a given number of characters. This will attempt to read up to the numberOfChars characters of the channel. An io:EofError will return once the channel reaches the end.

1string|io:Error result = readableCharChannel.read(1000);

Parameters

  • numberOfChars int
  • Number of characters, which should be read

Return Type

(string | Error)

The characters that read as a string, an io:EofError once the channel reaches the end, or else an io:Error if something went wrong while reading

readString

function readString() returns string | Error
Isolated Function

Read the entire channel content as a string.

1string|io:Error content = readableCharChannel.readString();

Return Type

(string | Error)

The content that read as a string or an io:Error

readAllLines

function readAllLines() returns string[ ] | Error
Isolated Function

Read the entire channel content as a list of lines.

1string[]|io:Error content = readableCharChannel.readAllLines();

Return Type

(string[ ] | Error)

The content that read as an array of lines (separated by the \n character) or an io:Error

readJson

function readJson() returns json | Error
Isolated Function

Reads a JSON from the given channel.

1json|io:Error result = readableCharChannel.readJson();

Return Type

(json | Error)

The content that is read as a JSON or else an io:Error

readXml

function readXml() returns xml | Error
Isolated Function

Reads an XML from the given channel.

1json|io:Error result = readableCharChannel.readXml();

Return Type

(xml | Error)

The content that is read as an XML or else an io:Error

readProperty

function readProperty(string key, string defaultValue) returns string | Error
Isolated Function

Reads a property from a .properties file with a default value.

1string|io:Error result = readableCharChannel.readProperty(key, defaultValue);

Parameters

  • key string
  • The property key, which needs to be read

  • defaultValue string (default "")
  • The default value to be returned

Return Type

(string | Error)

The property value related to the given key or else an io:Error

lineStream

function lineStream() returns stream<string, Error?> | Error
Isolated Function

Return a stream of lines that can be used to read all the lines in a file as a stream.

1stream<string, io:Error>|io:Error? result = readableCharChannel.lineStream();

Return Type

(stream<string, Error?> | Error)

A stream of strings (lines) or an io:Error

readAllProperties

function readAllProperties() returns map<string> | Error
Isolated Function

Reads all properties from a .properties file.

1map<string>|io:Error result = readableCharChannel.readAllProperties();

Return Type

(map<string> | Error)

A map of strings that contains all properties

close

function close() returns Error?
Isolated Function

Closes the character channel. After a channel is closed, any further reading operations will cause an error.

1io:Error? err = readableCharChannel.close();

Return Type

(Error?)

An error if something goes wrong while closing