commonMain.transport.Transport.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yass2-core-jvm Show documentation
Show all versions of yass2-core-jvm Show documentation
Yet Another Service Solution
The newest version!
package ch.softappeal.yass2.transport
import ch.softappeal.yass2.serialize.Serializer
public class Transport(
serializer: Serializer,
private val initialWriterCapacity: Int,
private val maxReadBytesInitialSize: Int = 500,
) : Serializer by serializer {
init {
require(initialWriterCapacity > 0)
require(maxReadBytesInitialSize > 0)
}
public fun createWriter(): BytesWriter = BytesWriter(initialWriterCapacity)
public suspend fun readBytes(
length: Int, readBytes: suspend (bytes: ByteArray, offset: Int, length: Int) -> Unit,
): ByteArray {
var buffer = ByteArray(minOf(length, maxReadBytesInitialSize))
var current = 0
while (current < length) { // prevents easy out-of-memory attack
if (current >= buffer.size) buffer = buffer.copyOf(minOf(length, 2 * buffer.size))
readBytes(buffer, current, buffer.size - current)
current = buffer.size
}
return buffer
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy