readAtMostTo

Writes up to ByteBuffer.remaining bytes from this buffer to the sink. Return the number of bytes written.

Parameters

sink

the sink to write data to.

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()
val nioByteBuffer = ByteBuffer.allocate(1024)

buffer.writeString("hello")
val bytesRead = buffer.readAtMostTo(nioByteBuffer)
assertEquals(5, bytesRead)
assertEquals(5, nioByteBuffer.capacity() - nioByteBuffer.remaining())

nioByteBuffer.position(0)
nioByteBuffer.limit(5)

val bytesWrite = buffer.write(nioByteBuffer)
assertEquals(5, bytesWrite)
assertEquals("hello", buffer.readString()) 
   //sampleEnd
}

Reads at most ByteBuffer.remaining bytes from this source into sink and returns the number of bytes read.

Parameters

sink

the sink to write the data to.

Throws

when the source 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()
val nioByteBuffer = ByteBuffer.allocate(1024)

buffer.writeString("hello")
val bytesRead = buffer.readAtMostTo(nioByteBuffer)
assertEquals(5, bytesRead)
assertEquals(5, nioByteBuffer.capacity() - nioByteBuffer.remaining())

nioByteBuffer.position(0)
nioByteBuffer.limit(5)

val bytesWrite = buffer.write(nioByteBuffer)
assertEquals(5, bytesWrite)
assertEquals("hello", buffer.readString()) 
   //sampleEnd
}