All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.kotlinx.io.streams.Output.kt Maven / Gradle / Ivy

There is a newer version: 0.1.16
Show newest version
package kotlinx.io.streams

import kotlinx.io.core.*
import kotlinx.io.pool.*
import java.io.*

private class OutputStreamAdapter(pool: ObjectPool, private val stream: OutputStream) : AbstractOutput(pool) {
    override fun flush(buffer: IoBuffer) {
        val array = ByteArrayPool.borrow()
        try {
            while (buffer.canRead()) {
                val rc = buffer.readAvailable(array)
                if (rc > 0) {
                    stream.write(array, 0, rc)
                }
            }
        } finally {
            ByteArrayPool.recycle(array)
        }
    }

    override fun closeDestination() {
        stream.close()
    }
}

fun OutputStream.asOutput(): Output = OutputStreamAdapter(IoBuffer.Pool, this)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy