ByteBuffer Example
The following is an example of writing data to and reading data from a stream using ByteBuffer.
import std.io.*
main(): Unit {
let arr1 = "test case".toArray()
let byteBuffer = ByteBuffer()
/* Write the data in arr1 to the stream. */
byteBuffer.write(arr1)
/* Read a piece of data of the first 4 bytes to arr2. */
let arr2 = Array<Byte>(4, repeat: 0)
byteBuffer.read(arr2)
println(String.fromUtf8(arr2))
/* Point the stream index to the start point. */
byteBuffer.seek(Begin(0))
/* Read all data in the stream. */
let arr3 = readToEnd(byteBuffer)
println(String.fromUtf8(arr3))
/* Point the stream index to the position of the letter 'c'. */
byteBuffer.seek(End(-4))
/* Read the remaining data in the stream. */
let str = readString(byteBuffer)
println(str)
}
Results:
test
test case
case