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

jsMain.kotlinx.io.core.ByteReadPacket.kt Maven / Gradle / Ivy

There is a newer version: 0.1.16
Show newest version
package kotlinx.io.core

import kotlinx.io.core.internal.*
import kotlinx.io.pool.*
import org.khronos.webgl.*

@DangerousInternalIoApi
actual abstract class ByteReadPacketPlatformBase
protected actual constructor(head: IoBuffer, remaining: Long, pool: ObjectPool) : ByteReadPacketBase(head, remaining, pool), Input {

    override fun readFully(dst: Int8Array, offset: Int, length: Int) {
        if (remaining < length) throw IllegalArgumentException("Not enough bytes available ($remaining) to read $length bytes")
        var copied = 0

        takeWhile { buffer: IoBuffer ->
            val rc = buffer.readAvailable(dst, offset + copied, length - copied)
            if (rc > 0) copied += rc
            copied < length
        }
    }

    override fun readFully(dst: ArrayBuffer, offset: Int, length: Int) {
        if (remaining < length) throw IllegalArgumentException("Not enough bytes available ($remaining) to read $length bytes")
        var copied = 0

        takeWhile { buffer: IoBuffer ->
            val rc = buffer.readAvailable(dst, offset + copied, length - copied)
            if (rc > 0) copied += rc
            copied < length
        }
    }

    override fun readFully(dst: ArrayBufferView, offset: Int, length: Int) {
        if (remaining < length) throw IllegalArgumentException("Not enough bytes available ($remaining) to read $length bytes")
        var copied = 0

        takeWhile { buffer: IoBuffer ->
            val rc = buffer.readAvailable(dst, offset + copied, length - copied)
            if (rc > 0) copied += rc
            copied < length
        }
    }

    override fun readAvailable(dst: Int8Array, offset: Int, length: Int): Int {
        val remaining = remaining
        if (remaining == 0L) return -1
        val size = minOf(remaining, length.toLong()).toInt()
        readFully(dst, offset, size)
        return size
    }

    override fun readAvailable(dst: ArrayBuffer, offset: Int, length: Int): Int {
        val remaining = remaining
        if (remaining == 0L) return -1
        val size = minOf(remaining, length.toLong()).toInt()
        readFully(dst, offset, size)
        return size
    }

    override fun readAvailable(dst: ArrayBufferView, offset: Int, length: Int): Int {
        val remaining = remaining
        if (remaining == 0L) return -1
        val size = minOf(remaining, length.toLong()).toInt()
        readFully(dst, offset, size)
        return size
    }

}

actual class ByteReadPacket
    internal actual constructor(head: IoBuffer, remaining: Long, pool: ObjectPool) : ByteReadPacketPlatformBase(head, remaining, pool), Input {
    actual constructor(head: IoBuffer, pool: ObjectPool) : this(head, head.remainingAll(), pool)

    init {
        markNoMoreChunksAvailable()
    }

    final override fun fill() = null

    override fun closeSource() {
    }

    actual companion object {
        actual val Empty: ByteReadPacket
            get() = ByteReadPacket(IoBuffer.Empty, object : NoPoolImpl() {
                override fun borrow() = IoBuffer.Empty
            })

        @Deprecated("This implementation detail is going to become internal.")
        actual inline val ReservedSize get() = IoBuffer.ReservedSize
    }
}

actual fun ByteReadPacket(array: ByteArray, offset: Int, length: Int, block: (ByteArray) -> Unit): ByteReadPacket {
    val content = array.asDynamic() as Int8Array
    val sub = when {
        offset == 0 && length == array.size -> content.buffer
        else -> content.buffer.slice(offset, offset + length)
    }

    val pool = object : SingleInstancePool() {
        override fun produceInstance(): IoBuffer {
            return IoBuffer(sub, null)
        }

        override fun disposeInstance(instance: IoBuffer) {
            block(array)
        }
    }

    return ByteReadPacket(pool.borrow().apply { resetForRead() }, pool)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy