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

commonMain.io.ktor.client.utils.ByteChannelUtils.kt Maven / Gradle / Ivy

Go to download

Ktor is a framework for quickly creating web applications in Kotlin with minimal effort.

There is a newer version: 2.2.4
Show newest version
/*
 * 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 - 2024 Weber Informatics LLC | Privacy Policy