Class: ReadableCharacterChannel

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

Constructor

Constructs a ReadableCharacterChannel from a given ReadableByteChannel and Charset.

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

  • charset string
  • The character set, which would be 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 a given 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)

Content, which is read, an EofError once the channel reaches the end or else an io:Error

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)

Either a string or 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)

Either a string array or 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 read JSON string 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 read 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 needs to read.

  • defaultValue string (default "")
  • Default value to be return.

Return Type

(string | Error)

The read property value 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)

Either 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 that contains all properties

close

function close() returns Error?
Isolated Function

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

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

Return Type

(Error?)

If an error occurred while writing