commonMain.io.ktor.client.utils.ByteChannelUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktor-client-core-jvm Show documentation
Show all versions of ktor-client-core-jvm Show documentation
Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.
/*
* 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