io package

public struct ByteChannel

Ballerina ByteChannel represents a channel which will allow I/O operations to be done

  • <ByteChannel> close() returns (IOError)

    Function to close a byte channel

    Return Variable Data Type Description
    IOError Returns if there's any error while performaing I/O operation
  • <ByteChannel> read(int nBytes) returns ((blob,int) | IOError)

    Function to read bytes

    Parameter Name Data Type Description
    nBytes int Number of bytes which should be read

    Return Variable Data Type Description
    (blob,int) | IOError The bytes which were read
  • <ByteChannel> write(blob content, int offset) returns (int | IOError)

    Function to write bytes

    Parameter Name Data Type Description
    content blob Bytes which should be written
    offset int If the bytes need to be written with an offset, the value of that offset

    Return Variable Data Type Description
    int | IOError Number of bytes written
  • <ByteChannel> ByteChannel.<init>()

public struct CharacterChannel

Ballerina CharacterChannel represents a channel which will allow to read/write characters

  • <CharacterChannel> closeCharacterChannel() returns (IOError)

    Function to close a character channel

    Return Variable Data Type Description
    IOError Returns if there's any error while performaing I/O operation
  • <CharacterChannel> readCharacters(int numberOfChars) returns (string | IOError)

    Function to read characters

    Parameter Name Data Type Description
    numberOfChars int Number of characters which should be read

    Return Variable Data Type Description
    string | IOError The character sequence which was read
  • <CharacterChannel> readJson() returns (json | IOError)

    Function to convert a character channel to a JSON

    Return Variable Data Type Description
    json | IOError Returns A JSON
  • <CharacterChannel> readXml() returns (xml | IOError)

    Function to convert a character channel to a XML

    Return Variable Data Type Description
    xml | IOError Returns A XML
  • <CharacterChannel> writeCharacters(string content, int startOffset) returns (int | IOError)

    Function to write characters

    Parameter Name Data Type Description
    content string Text content which should be written
    startOffset int If the content needs to be written with an offset, the value of that offset

    Return Variable Data Type Description
    int | IOError Number of characters written
  • <CharacterChannel> CharacterChannel.<init>()

public struct DelimitedRecordChannel

Ballerina DelimitedRecordChannel represents a channel which will allow to read/write text records

  • <DelimitedRecordChannel> closeDelimitedRecordChannel() returns (IOError)

    Function to close the text record channel

    Return Variable Data Type Description
    IOError Returns if there's any error while performaing I/O operation
  • <DelimitedRecordChannel> hasNextTextRecord() returns (boolean)

    Function to check whether next record is available or not

    Return Variable Data Type Description
    boolean True if the channel has more records; false otherwise
  • <DelimitedRecordChannel> nextTextRecord() returns (string[] | IOError)

    Function to read text records

    Return Variable Data Type Description
    string[] | IOError Fields listed in the record
  • <DelimitedRecordChannel> writeTextRecord(string[] records) returns (IOError)

    Function to write text records

    Parameter Name Data Type Description
    records string[] Fields which are included in the record

    Return Variable Data Type Description
    IOError Returns if there's any error while performaing I/O operation
  • <DelimitedRecordChannel> DelimitedRecordChannel.<init>()

public struct IOError

Represents an error which will occur while performing I/O operations

Field Name Data Type Description Default Value
message string
cause error[] []
  • <IOError> IOError.<init>()

public struct Socket

Represents a client socket connection. This can be used to communicate with a remote machine

Field Name Data Type Description Default Value
channel ByteChannel Connection stream that bridge the client and the server []
port int Remote server port
localPort int Client side port that open for the communication
address string Remote server domain/IP
localAddress string Client domain/IP
  • <Socket> Socket.<init>()

  • <Socket> closeSocket() returns (IOError)

    Close the socket connection with the remote server

    Return Variable Data Type Description
    IOError Returns an IOError if socket could not be closed

public struct SocketProperties

SocketProperties structs represents the properties which are used to configure TCP connection

Field Name Data Type Description Default Value
localPort int Client side port that open for the communication
keyStoreFile string File path to keystore file
keyStorePassword string The keystore password
trustStoreFile string File path to truststore file
trustStorePassword string The truststore password
certPassword string The certificate password
sslEnabledProtocols string SSL/TLS protocols to be enabled
ciphers string List of ciphers to be used
sslProtocol string The SSL protocol version
  • <SocketProperties> SocketProperties.<init>()

public function createCharacterChannel(ByteChannel byteChannel, string encoding) returns (CharacterChannel | IOError)

Function to create a CharacterChannel from ByteChannel

Parameter Name Data Type Description
byteChannel ByteChannel
encoding string The charset/encoding of the content (i.e UTF-8, ASCII)

Return Variable Data Type Description
CharacterChannel | IOError CharacterChannel converted from ByteChannel

public function createDelimitedRecordChannel(CharacterChannel channel, string recordSeparator, string fieldSeparator) returns (DelimitedRecordChannel | IOError)

Function to create a CharacterChannel to DelimitedRecordChannel

Parameter Name Data Type Description
channel CharacterChannel The CharacterChannel to be converted
recordSeparator string Terminating expression to distinguish between records
fieldSeparator string Terminating expression to distinguish between fields

Return Variable Data Type Description
DelimitedRecordChannel | IOError DelimitedRecordChannel converted from CharacterChannel

public function loadToTable(string filePath, string recordSeparator, string fieldSeparator, string encoding, boolean headerLineIncluded, typedesc structType) returns (table | IOError)

Function to load delimited records to in-memory table

Parameter Name Data Type Description
filePath string Path to delimited file
recordSeparator string Terminating expression to distinguish between records
fieldSeparator string Terminating expression to distinguish between fields
encoding string The charset/encoding of the content (i.e UTF-8, ASCII)
headerLineIncluded boolean To indicate whether given file included the header line or not
structType typedesc Name of the struct that each record need to populate

Return Variable Data Type Description
table | IOError table of delimited values

public function openFile(string path, string accessMode) returns (ByteChannel)

Opens a byte channel from a specified file location

Parameter Name Data Type Description
path string path to the file location
accessMode string whether the file should be opened for read,write or append

Return Variable Data Type Description
ByteChannel Channel which will allow to source/sink

public function openSecureSocket(string host, int port, SocketProperties options) returns (Socket | IOError)

Open a secure socket connection with a remote server

Parameter Name Data Type Description
host string Remote server domain/IP
port int Remote server port
options SocketProperties Connection stream that bridge the client and the server

Return Variable Data Type Description
Socket | IOError Socket which will allow to communicate with a remote server

public function openSocket(string host, int port, SocketProperties options) returns (Socket | IOError)

Opens a socket from a specified network location

Parameter Name Data Type Description
host string Remote server domain/IP
port int Remote server port
options SocketProperties Connection stream that bridge the client and the server

Return Variable Data Type Description
Socket | IOError Socket which will allow to communicate with a remote server

public function print(any a)

Prints a 'any' value to the STDOUT

Parameter Name Data Type Description
a any any value to be printed

public function println(any a)

Prints an any value to the STDOUT in a new line

Parameter Name Data Type Description
a any any value to be printed

public function sprintf(string format, any[] args) returns (string)

Returns a formatted string using the specified format string and arguments

Parameter Name Data Type Description
format string Format specifier
args any[] Arguments to be formatted, should match number of args in format specifier

Return Variable Data Type Description
string Formatted string