kotlinx.io.streams.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.streams
import kotlinx.io.core.*
import kotlinx.io.pool.*
import java.io.*
internal class InputStreamAsInput(private val stream: InputStream, pool: ObjectPool) :
AbstractInput(pool = pool), Input {
override fun fill(): IoBuffer? {
val buffer = ByteArrayPool.borrow()
try {
val rc = stream.read(buffer)
val result = when {
rc >= 0 -> pool.borrow().also { it.writeFully(buffer, 0, rc) }
else -> null
}
ByteArrayPool.recycle(buffer)
return result
} catch (t: Throwable) {
ByteArrayPool.recycle(buffer)
throw t
}
}
override fun closeSource() {
stream.close()
}
}
fun InputStream.asInput(pool: ObjectPool = IoBuffer.Pool): Input = InputStreamAsInput(this, pool)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy