writeString
Encodes the characters at startIndex up to endIndex from string in UTF-8 and writes it to this sink.
Parameters
the string to be encoded.
the index (inclusive) of the first character to encode, 0 by default.
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.
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
}
Encodes substring of string starting at startIndex and ending at endIndex using charset and writes into this sink.
Parameters
the string to encode into this sink.
the Charset to use for encoding.
the index of the first character to encode, inclusive, 0 by default.
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.
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.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
import kotlin.test.assertTrue
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
}