kotlinx.io.core.ByteReadPacketExtensions.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.core
import kotlinx.io.pool.*
import java.nio.*
actual inline fun ByteReadPacket(array: ByteArray, offset: Int, length: Int, crossinline block: (ByteArray) -> Unit): ByteReadPacket {
return ByteReadPacket(ByteBuffer.wrap(array, offset, length)) { block(array) }
}
fun ByteReadPacket(bb: ByteBuffer, release: (ByteBuffer) -> Unit = {}): ByteReadPacket {
val pool = poolFor(bb, release)
val view = pool.borrow().apply { resetForRead() }
return ByteReadPacket(view, pool)
}
private fun poolFor(bb: ByteBuffer, release: (ByteBuffer) -> Unit): ObjectPool {
return SingleByteBufferPool(bb, release)
}
private class SingleByteBufferPool(val instance: ByteBuffer, val release: (ByteBuffer) -> Unit) :
SingleInstancePool() {
override fun produceInstance(): IoBuffer {
return IoBuffer(instance)
}
override fun disposeInstance(instance: IoBuffer) {
release(this.instance)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy