copyTo

fun Buffer.copyTo(out: OutputStream, startIndex: Long = 0, endIndex: Long = size)(source)

Copy bytes from this buffer's subrange, starting at startIndex and ending at endIndex, to out. This method does not consume data from the buffer.

Parameters

out

the destination to copy data into.

startIndex

the index (inclusive) of the first byte to copy, 0 by default.

endIndex

the index (exclusive) of the last byte to copy, buffer.size by default.

Throws

when startIndex or endIndex is out of this buffer bounds ([0..buffer.size)).

when startIndex > endIndex.

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.writeString("string")

val outputStream = ByteArrayOutputStream()
buffer.copyTo(outputStream, startIndex = 2, endIndex = 6)

assertEquals("string", buffer.readString())
assertEquals("ring", outputStream.toString("UTF-8")) 
   //sampleEnd
}