import ballerina/io;
import ballerina/log;
function copy(io:ReadableByteChannel src,
              io:WritableByteChannel dst) returns error? {
    while (true) {
        byte[]|io:Error result = src.read(1000);
        if (result is io:EofError) {
            break;
        } else if (result is error) {
            return <@untained>result;
        } else {
            int i = 0;
            while (i < result.length()) {
                var result2 = dst.write(result, i);
                if (result2 is error) {
                    return result2;
                } else {
                    i = i + result2;
                }
            }
        }
    }
    return;
}
function close(io:ReadableByteChannel|io:WritableByteChannel ch) {
    abstract object {
        public function close() returns error?;
    } channelResult = ch;
    var cr = channelResult.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", cr);
    }
}public function main() returns @tainted error? {
    string srcPath = "./files/ballerina.jpg";
    string dstPath = "./files/ballerinaCopy.jpg";
    io:ReadableByteChannel srcCh = check io:openReadableFile(srcPath);
    io:WritableByteChannel dstCh = check io:openWritableFile(dstPath);
    io:println("Start to copy files from " + srcPath + " to " + dstPath);
    var result = copy(srcCh, dstCh);
    if (result is error) {
        log:printError("error occurred while performing copy ", result);
    } else {
        io:println("File copy completed. The copied file is located at " +
                    dstPath);
    }
    close(srcCh);
    close(dstCh);
}# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run byte_io.bal
Start to copy files from ./files/ballerina.jpg to ./files/ballerinaCopy.jpg
File copy completed. The copied file could be located in ./files/ballerinaCopy.jpg

Byte I/O

This example demonstrates how bytes can be read and written through the I/O API.

import ballerina/io;
import ballerina/log;
function copy(io:ReadableByteChannel src,
              io:WritableByteChannel dst) returns error? {

Copies the content from the source channel to a destination channel.

    while (true) {

The below example shows how to read all the content from the source and copy it to the destination.

        byte[]|io:Error result = src.read(1000);
        if (result is io:EofError) {
            break;
        } else if (result is error) {
            return <@untained>result;
        } else {

The operation attempts to read a maximum of 1000 bytes and returns with the available content, which could be < 1000.

            int i = 0;
            while (i < result.length()) {
                var result2 = dst.write(result, i);
                if (result2 is error) {
                    return result2;
                } else {
                    i = i + result2;
                }
            }
        }
    }
    return;
}

The operation writes the given content into the channel.

function close(io:ReadableByteChannel|io:WritableByteChannel ch) {
    abstract object {
        public function close() returns error?;
    } channelResult = ch;
    var cr = channelResult.close();
    if (cr is error) {
        log:printError("Error occurred while closing the channel: ", cr);
    }
}

Closes a given readable or writable byte channel.

public function main() returns @tainted error? {
    string srcPath = "./files/ballerina.jpg";
    string dstPath = "./files/ballerinaCopy.jpg";
    io:ReadableByteChannel srcCh = check io:openReadableFile(srcPath);

Initializes the readable byte channel.

    io:WritableByteChannel dstCh = check io:openWritableFile(dstPath);
    io:println("Start to copy files from " + srcPath + " to " + dstPath);

Initializes the writable byte channel.

    var result = copy(srcCh, dstCh);
    if (result is error) {
        log:printError("error occurred while performing copy ", result);
    } else {
        io:println("File copy completed. The copied file is located at " +
                    dstPath);
    }

Copies the source byte channel to the target byte channel.

    close(srcCh);
    close(dstCh);
}

Closes the connections.

# To run this sample, navigate to the directory that contains the
# `.bal` file, and execute the `ballerina run` command below.
ballerina run byte_io.bal
Start to copy files from ./files/ballerina.jpg to ./files/ballerinaCopy.jpg
File copy completed. The copied file could be located in ./files/ballerinaCopy.jpg