writeString

fun Sink.writeString(string: String, startIndex: Int = 0, endIndex: Int = string.length)(source)

Encodes the characters at startIndex up to endIndex from string in UTF-8 and writes it to this sink.

Parameters

string

the string to be encoded.

startIndex

the index (inclusive) of the first character to encode, 0 by default.

endIndex

the index (exclusive) of a character past to a last character to encode, string.length by default.

Throws

when startIndex or endIndex is out of range of string indices.

when startIndex > endIndex.

when the sink is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val buffer = Buffer()

buffer.writeString("hello", startIndex = 1, endIndex = 4)
assertContentEquals(
    byteArrayOf(
        'e'.code.toByte(),
        'l'.code.toByte(),
        'l'.code.toByte()
    ), buffer.readByteArray()
)

buffer.writeString("Δ")
assertContentEquals(byteArrayOf(0xce.toByte(), 0x94.toByte()), buffer.readByteArray()) 
   //sampleEnd
}

fun Sink.writeString(chars: CharSequence, startIndex: Int = 0, endIndex: Int = chars.length)(source)

Encodes the characters at startIndex up to endIndex from chars in UTF-8 and writes it to this sink.

Parameters

chars

the string to be encoded.

startIndex

the index (inclusive) of the first character to encode, 0 by default.

endIndex

the index (exclusive) of a character past to a last character to encode, chars.length by default.

Throws

when startIndex or endIndex is out of range of chars indices.

when startIndex > endIndex.

when the sink is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val buffer = Buffer()

buffer.writeString(StringBuilder("hello"), startIndex = 1, endIndex = 4)
assertContentEquals(
    byteArrayOf(
        'e'.code.toByte(),
        'l'.code.toByte(),
        'l'.code.toByte()
    ), buffer.readByteArray()
)

buffer.writeString(StringBuilder("Δ"))
assertContentEquals(byteArrayOf(0xce.toByte(), 0x94.toByte()), buffer.readByteArray()) 
   //sampleEnd
}
fun Sink.writeString(string: String, charset: Charset, startIndex: Int = 0, endIndex: Int = string.length)(source)

Encodes substring of string starting at startIndex and ending at endIndex using charset and writes into this sink.

Parameters

string

the string to encode into this sink.

charset

the Charset to use for encoding.

startIndex

the index of the first character to encode, inclusive, 0 by default.

endIndex

the index of the last character to encode, exclusive, string.length by default.

Throws

when startIndex or endIndex is out of range of string indices.

when startIndex > endIndex.

when the sink is closed.

when some I/O error occurs.

Samples

import kotlinx.io.*
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.nio.ByteBuffer
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
import kotlin.test.*

fun main() { 
   //sampleStart 
   val buffer = Buffer()

buffer.write(byteArrayOf(0, 0, 0, 0x68, 0, 0, 0, 0x69))
assertEquals("hi", buffer.readString(Charsets.UTF_32BE))

buffer.writeString("hi", Charsets.UTF_16BE)
assertContentEquals(byteArrayOf(0, 0x68, 0, 0x69), buffer.readByteArray()) 
   //sampleEnd
}