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

commonMain.transport.ktor.ByteChannel.kt Maven / Gradle / Ivy

The newest version!
package ch.softappeal.yass2.transport.ktor

import ch.softappeal.yass2.transport.BytesReader
import ch.softappeal.yass2.transport.Transport
import io.ktor.utils.io.ByteReadChannel
import io.ktor.utils.io.ByteWriteChannel
import io.ktor.utils.io.readFully
import io.ktor.utils.io.writeFully
import io.ktor.utils.io.writeInt

internal suspend fun ByteWriteChannel.write(transport: Transport, value: Any?) {
    val writer = transport.createWriter()
    transport.write(writer, value)
    writeInt(writer.current)
    writeFully(writer.buffer, 0, writer.current)
}

internal suspend fun ByteReadChannel.read(transport: Transport, length: Int): Any? {
    val buffer = transport.readBytes(length) { bytes, offset, size -> readFully(bytes, offset, offset + size) }
    val reader = BytesReader(buffer)
    return transport.read(reader).apply {
        check(reader.isDrained)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy