question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ktor HttpClient unable to download files larger than 4096 bytes

See original GitHub issue

Ktor Version and Engine Used (client or server and name) Ktor Client 1.3.1 in Kotlin MPP context. Engines OkHTTP and iOS Native, but not explicitly set (resolved by gradle dependency).

Describe the bug Ktor HttpClient keeps suspending when requesting to read more than 4096 bytes from a Url, and never comes back.

To Reproduce Steps to reproduce the behavior:

suspend fun downloadFile(url: String): ByteArray {
    val httpClient = HttpClient{
        install(Logging) {
            this.logger = Logger.SIMPLE
            this.level = LogLevel.ALL
        }
    }
    val statement = httpClient.request<HttpStatement>(url) {
        range?.let {
            header(HttpHeaders.Range, range)
        }
    }
    return statement.execute {
        val contentLength = it.contentLength()?.lowInt ?: 0
        val byteArray = ByteArray(contentLength)
        var offset = 0
        do {
            val currentRead = it.content.readAvailable(byteArray, offset, byteArray.size)
            offset += currentRead
            logger.logDebug("Download in progress, offset: ${offset}, current read ${currentRead} / ${contentLength}")
        } while (offset < contentLength)
        logger.logDebug("Download done")
        return@execute byteArray
    }
}

Execute function above with a URL that contains a file larger than 4096 bytes. During my testing I’m downloading files from Amazon S3.

Expected behavior Expected to get a byte array containing the entire contents of the file.

Screenshots The logging in the function above generates this result:

Download in progress, offset: 4088, current read 4088 / 355835 Download in progress, offset: 4096, current read 8 / 355835

I know that using a ByteArray is not the most ideal solution to download a file into, but I’m working in a pure-kotlin Multi Platform Project, so any Java import is a no-go without using expect/actual which means I cannot directly write to files, or use a stream of some kind.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
gvdhovencommented, Feb 19, 2020

@e5l can you give an update on this bug? This is blocking a big feature for a customer since we are now limited in downloading in chunks of 4kb.

0reactions
oleg-larshincommented, Aug 10, 2020

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ktor client OutOfMemoryError while downloading large files
Bad idea to put such a large file in a variable like var response. Better find a way to directly save the bytes...
Read more >
WhatsNew 2.0 | Ktor Framework
"Client provided less bytes than content length" from ktor. ... Jetty: Content Length exception when body size is greater than 4096 bytes.
Read more >
WhatsNew 2.1 | Ktor Framework
Ktor client OutOfMemoryError while downloading large files.
Read more >
WhatsNew 1.6 | Ktor Framework
Darwin and Kotlin/JS: "List has more than one element" error when ... Unable to download file from Ktor server (okhttp3.internal.http2.
Read more >
Receiving responses | Ktor
HttpResponse exposes the API required to get a response body in various ways (raw bytes, JSON objects, etc.) and obtain response parameters, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found