Functions

func copy(InputStream, OutputStream)

public func copy(from: InputStream, to!: OutputStream): Int64

Description: Copies the data that is not read from an input stream to another output stream.

Parameters:

  • from: InputStream: input stream from which data is to be read
  • to!: OutputStream: output stream to which the data is copied

Returns:

  • Int64: number of bytes in the copied data

Examples:

import std.io.ByteBuffer
import std.io.copy

main(): Unit {
    let sourceStream = ByteBuffer()
    let targetStream = ByteBuffer()

    /* Write data to the source input stream. */
    let sourceData = "Hello, World!".toArray()
    sourceStream.write(sourceData)

    /* Use the copy function to copy data from the source input stream to the target output stream. */
    let copiedBytes = copy(sourceStream, to: targetStream)
    println("Copied ${copiedBytes} bytes.")

    /* Read data from the target output stream. */
    let targetData: Array<Byte> = Array<Byte>(sourceData.size, repeat: 0)
    targetStream.read(targetData)
    println("Data copied to target stream: ${String.fromUtf8(targetData)}")
}

Results:

Copied 13 bytes.
Data copied to target stream: Hello, World!

func readString<T>(T) where T <: InputStream & Seekable

public func readString<T>(from: T): String where T <: InputStream & Seekable

Description: Reads the remaining content in the input parameter and returns a string.

Parameters:

  • from: T: object from which data is to be read

Returns:

  • String: result character string read

Throws:

  • ContentFormatException - When the byte encoding method for the remaining content does not comply with UTF-8, this exception is thrown. Examples:
import std.io.ByteBuffer
import std.io.readString
import std.io.ContentFormatException

main(): Unit {
    let inputStream = ByteBuffer()

    /* Write data to the input stream. */
    let sourceData = "Hello, World!".toArray()
    inputStream.write(sourceData)

    /* Use the readString function to read the remaining content in the input stream. */
    try {
        let result = readString(inputStream)
        println("Read string: ${result}")
    } catch (e: ContentFormatException) {
        println("Error: ${e.message}")
    }

    /* Write an invalid UTF-8 character string to the input stream. */
    let sourceDataError: Array<UInt8> = [0xC3, 0x28, 0x48, 0x65, 0x6C, 0x6C, 0x6F]
    inputStream.write(sourceDataError)

    /* Use the readString function to read the remaining content in the input stream. */
    try {
        let result = readString(inputStream)
        println("Read string: ${result}")
    } catch (e: ContentFormatException) {
        println("Error: ${e.message}")
    }
}

Results:

Read string: Hello, World!
Error: Invalid unicode scalar value.

func readStringUnchecked<T>(T) where T <: InputStream & Seekable

public unsafe func readStringUnchecked<T>(from: T): String where T <: InputStream & Seekable

Description: Reads the remaining content in the input parameter and returns a string. This function does not check the validity of strings.

Parameters:

  • from: T: object from which data is to be read

Returns:

  • String: result character string read

Examples:

import std.io.ByteBuffer
import std.io.readStringUnchecked
import std.io.ContentFormatException

main(): Unit {
    let inputStream = ByteBuffer()

    /* Write data to the input stream. */
    let sourceData = "Hello, World!".toArray()
    inputStream.write(sourceData)

    /* Use the readString function to read the remaining content in the input stream. The output is "Read string: Hello, World!" */.
    unsafe {
        let result = readStringUnchecked(inputStream)
        println("Read string: ${result}")
    }

    /* Write an invalid UTF-8 character string to the input stream. */
    let sourceDataError: Array<UInt8> = [0xC3, 0x28, 0x48, 0x65, 0x6C, 0x6C, 0x6F]
    inputStream.write(sourceDataError)

    /* Use the readString function to read the remaining content in the input stream. Because the first character of inputStream is an invalid UTF-8 character, unpredictable output characters are generated. */
    unsafe {
        let result = readStringUnchecked(inputStream)
        println("Read string: ${result}")
    }
}

func readToEnd<T>(T) where T <: InputStream & Seekable

public func readToEnd<T>(from: T): Array<Byte> where T <: InputStream & Seekable

Description: Obtains the data that is not read from the input parameter.

Parameters:

  • from: T: object from which data is to be read

Returns:

  • Array<Byte>: copy of the data that is not read

Examples:

import std.io.ByteBuffer
import std.io.readToEnd

main(): Unit {
    let inputStream = ByteBuffer()

    /* Write data to the input stream. */
    let sourceData = "Hello, World!".toArray()
    inputStream.write(sourceData)

    /* Use the readToEnd function to read the remaining content in the input stream. */
    let data = readToEnd(inputStream)
    println("${String.fromUtf8(data)}")
}

Results:

Hello, World!