readString

Removes all bytes from this source, decodes them as UTF-8, and returns the string.

Returns the empty string if this source is empty.

Throws

when the source is closed.

Samples

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

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

buffer.write("hello world".encodeToByteArray())
assertEquals("hello", buffer.readString(5))
assertEquals(" world", buffer.readString())

buffer.write(byteArrayOf(0xce.toByte(), 0x94.toByte()))
assertEquals("Δ", buffer.readString()) 
   //sampleEnd
}

Removes all bytes from this buffer, decodes them as UTF-8, and returns the string.

Returns the empty string if this buffer is empty.

Samples

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

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

buffer.write("hello world".encodeToByteArray())
assertEquals("hello", buffer.readString(5))
assertEquals(" world", buffer.readString())

buffer.write(byteArrayOf(0xce.toByte(), 0x94.toByte()))
assertEquals("Δ", buffer.readString()) 
   //sampleEnd
}

fun Source.readString(byteCount: Long): String(source)

Removes byteCount bytes from this source, decodes them as UTF-8, and returns the string.

Parameters

byteCount

the number of bytes to read from the source for string decoding.

Throws

when the source is exhausted before reading byteCount bytes from it.

when the source is closed.

Samples

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

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

buffer.write("hello world".encodeToByteArray())
assertEquals("hello", buffer.readString(5))
assertEquals(" world", buffer.readString())

buffer.write(byteArrayOf(0xce.toByte(), 0x94.toByte()))
assertEquals("Δ", buffer.readString()) 
   //sampleEnd
}

Decodes whole content of this stream into a string using charset. Returns empty string if the source is exhausted.

Parameters

charset

the Charset to use for string decoding.

Throws

when the source 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
}

fun Source.readString(byteCount: Long, charset: Charset): String(source)

Decodes byteCount bytes of this stream into a string using charset.

Parameters

byteCount

the number of bytes to read from the source for decoding.

charset

the Charset to use for string decoding.

Throws

when the source exhausted before byteCount bytes could be read from it.

when the source is closed.

if byteCount is negative or its value is greater than Int.MAX_VALUE.

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("h", buffer.readString(byteCount = 4, charset = Charsets.UTF_32BE)) 
   //sampleEnd
}