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

jvmMain.com.divpundir.mavlink.connection.udp.ByteChannelSource.kt Maven / Gradle / Ivy

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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy