jvmMain.kotlinx.io.nio.Input.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-io-jvm Show documentation
Show all versions of kotlinx-io-jvm Show documentation
IO support libraries for Kotlin
package kotlinx.io.nio
import kotlinx.io.core.*
import kotlinx.io.core.IoBuffer.*
import kotlinx.io.pool.*
import java.nio.*
import java.nio.channels.*
private class ChannelAsInput(private val channel: ReadableByteChannel, pool: ObjectPool) :
AbstractInput(pool = pool), Input {
init {
require(channel !is SelectableChannel || !channel.isBlocking) { "Non-blocking channels are not supported" }
}
override fun fill(): IoBuffer? {
val buffer: IoBuffer = pool.borrow()
buffer.reserveEndGap(IoBuffer.ReservedSize)
try {
var rc = -1
buffer.writeDirect(1) { bb: ByteBuffer ->
rc = channel.read(bb)
}
if (rc == -1) {
buffer.release(pool)
return null
}
return buffer
} catch (t: Throwable) {
buffer.release(pool)
throw t
}
}
override fun closeSource() {
channel.close()
}
}
fun ReadableByteChannel.asInput(pool: ObjectPool = IoBuffer.Pool): Input = ChannelAsInput(this, pool)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy