jvmMain.com.divpundir.mavlink.connection.udp.ByteChannelSource.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connection-udp-jvm Show documentation
Show all versions of connection-udp-jvm Show documentation
A modern MAVLink library for the JVM written in Kotlin.
package com.divpundir.mavlink.connection.udp
import okio.Buffer
import okio.Source
import okio.Timeout
import java.io.IOException
import java.nio.ByteBuffer
import java.nio.channels.ReadableByteChannel
import kotlin.math.min
internal class ByteChannelSource(
private val channel: ReadableByteChannel,
private val timeout: Timeout = Timeout.NONE
) : Source {
private val cursor: Buffer.UnsafeCursor = Buffer.UnsafeCursor()
@Throws(IOException::class)
override fun read(sink: Buffer, byteCount: Long): Long {
if (!channel.isOpen) {
throw IOException("The channel is closed")
}
sink.readAndWriteUnsafe(cursor).use { _ ->
timeout.throwIfReached()
val oldSize: Long = sink.size
val length = min(8192, byteCount).toInt()
cursor.expandBuffer(length)
val read = channel.read(ByteBuffer.wrap(cursor.data, cursor.start, length))
return if (read == -1) {
cursor.resizeBuffer(oldSize)
-1
} else {
cursor.resizeBuffer(oldSize + read)
read.toLong()
}
}
}
override fun timeout(): Timeout = timeout
@Throws(IOException::class)
override fun close() {
channel.close()
}
}