Class: ReadableByteChannel

ReadableByteChannel represents an input resource (i.e file). which could be used to source bytes. A file path or an in-memory byte array can be used to obtain a io:ReadableByteChannel. A io:ReadableByteChannel do not support initilization, and it should be obtained using the following methods or implement natively. io:openReadableFile("./files/sample.txt") - used to obtain a io:ReadableByteChannel from a given file path io:createReadableChannel(byteArray) - used to obtain a io:ReadableByteChannel from a given byte array

Methods

read

Source bytes from a given input resource.

readAll

Read all content of the channel as a byte array and return a read only byte array.

blockStream

Return a block stream that can be used to read all byte blocks as a stream.

base64Encode

Encodes a given ReadableByteChannel using the Base64 encoding scheme.

base64Decode

Decodes a given Base64 encoded io:ReadableByteChannel.

close

Closes a given ReadableByteChannel.

read

function read(int nBytes) returns byte[ ] | Error
Isolated Function

Source bytes from a given input resource. This operation will be asynchronous in which the total number of required bytes might not be returned at a given time. An io:EofError will return once the channel reaches the end.

1byte[]|io:Error result = readableByteChannel.read(1000);

Parameters

  • nBytes int
  • A positive integer. Represents the number of bytes, which should be read

Return Type

(byte[ ] | Error)

Content (the number of bytes) read, an EofError once the channel reaches the end or else an io:Error

readAll

function readAll() returns readonly & byte[ ] | Error
Isolated Function

Read all content of the channel as a byte array and return a read only byte array.

1byte[]|io:Error result = readableByteChannel.readAll();

Return Type

(readonly & byte[ ] | Error)

Either a read only byte array or else an io:Error

blockStream

function blockStream(int blockSize) returns stream<Block, Error?> | Error
Isolated Function

Return a block stream that can be used to read all byte blocks as a stream.

1stream<io:Block, io:Error>|io:Error result = readableByteChannel.blockStream();

Parameters

  • blockSize int
  • A positive integer. Size of the block.

Return Type

(stream<Block, Error?> | Error)

Either a block stream or else an io:Error

base64Encode

function base64Encode() returns ReadableByteChannel | Error
Isolated Function

Encodes a given ReadableByteChannel using the Base64 encoding scheme.

1io:ReadableByteChannel|Error encodedChannel = readableByteChannel.base64Encode();

Return Type

(ReadableByteChannel | Error)

An encoded ReadableByteChannel or else an io:Error

base64Decode

function base64Decode() returns ReadableByteChannel | Error
Isolated Function

Decodes a given Base64 encoded io:ReadableByteChannel.

1io:ReadableByteChannel|Error encodedChannel = readableByteChannel.base64Decode();

Return Type

(ReadableByteChannel | Error)

A decoded ReadableByteChannel or else an io:Error

close

function close() returns Error?
Isolated Function

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

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

Return Type

(Error?)

Will return () if there is no error