
commonMain.io.ktor.util.ByteChannels.kt Maven / Gradle / Ivy
/*
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.util
import kotlinx.coroutines.*
import kotlinx.coroutines.io.*
import kotlinx.io.core.*
private const val CHUNK_BUFFER_SIZE = 4096L
/**
* Split source [ByteReadChannel] into 2 new one.
* Cancel of one channel in split(input or both outputs) cancels other channels.
*/
@KtorExperimentalAPI
fun ByteReadChannel.split(coroutineScope: CoroutineScope): Pair {
val first = ByteChannel(autoFlush = true)
val second = ByteChannel(autoFlush = true)
coroutineScope.launch {
try {
while ([email protected]) {
[email protected](CHUNK_BUFFER_SIZE).use { chunk ->
listOf(
async { first.writePacket(chunk.copy()) },
async { second.writePacket(chunk.copy()) }
).awaitAll()
}
}
} catch (cause: Throwable) {
[email protected](cause)
first.cancel(cause)
second.cancel(cause)
} finally {
first.close()
second.close()
}
}
return first to second
}
/**
* Read channel to byte array.
*/
@KtorExperimentalAPI
suspend fun ByteReadChannel.toByteArray(): ByteArray = readRemaining().readBytes()
© 2015 - 2025 Weber Informatics LLC | Privacy Policy