
commonMain.io.ktor.client.utils.ByteChannelUtils.kt Maven / Gradle / Ivy
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.client.utils
import io.ktor.client.content.*
import io.ktor.utils.io.*
import io.ktor.utils.io.pool.*
import kotlinx.coroutines.*
import kotlin.coroutines.*
@OptIn(DelicateCoroutinesApi::class)
internal fun ByteReadChannel.observable(
context: CoroutineContext,
contentLength: Long?,
listener: ProgressListener
) = GlobalScope.writer(context, autoFlush = true) {
ByteArrayPool.useInstance { byteArray ->
val total = contentLength ?: -1
var bytesSend = 0L
while ([email protected]) {
val read = [email protected](byteArray)
channel.writeFully(byteArray, offset = 0, length = read)
bytesSend += read
listener(bytesSend, total)
}
val closedCause = [email protected]
channel.close(closedCause)
if (closedCause == null && bytesSend == 0L) {
listener(bytesSend, total)
}
}
}.channel
© 2015 - 2025 Weber Informatics LLC | Privacy Policy