Ktor HttpClient unable to download files larger than 4096 bytes
See original GitHub issueKtor 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:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top GitHub Comments
@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.
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.