Vlad
05/24/2024, 10:08 AMval client = HttpClient(CIO)
val file = File.createTempFile("files", "index")
runBlocking {
client.prepareGet("<https://ktor.io/>").execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.body()
while (!channel.isClosedForRead) {
val packet = channel.readRemaining(DEFAULT_BUFFER_SIZE.toLong())
while (!packet.isEmpty) {
val bytes = packet.readBytes()
file.appendBytes(bytes)
println("Received ${file.length()} bytes from ${httpResponse.contentLength()}")
}
}
println("A file saved to ${file.path}")
}
}
What would be reasonable value for the DEFAULT_BUFFER_SIZE
, there are no such constant available.
I tried to load with 4096 limit but faced different issues:
• okhttp3.internal.http2.StreamResetException: stream was reset: CANCEL
• Sometimes says that read was successful but read bytes size is somewhere between 56-58 Mb when the file is 102Mb and the duration is around 330s. And we end up with corrupted file with success state.
• I thought debug build messes with me and tried release. No luck.
I try download from our server (we don't have any timeouts) and from the opensource https://link.testfile.org/PDF200MB
Not much in the google for the topic. Feels like the thing is simple and everything is working for everyone.
totalBytes += bytes
And that what was causing the delay (and hopefully all other issues).
Adding into the array without initial size was slowing it like 30 times 🤦♂️Aleksei Tirman [JB]
05/30/2024, 9:09 AMByteReadChannel.copyTo
method?
val client = HttpClient(CIO)
val file = File.createTempFile("files", "index")
client.prepareGet("<https://ktor.io/>").execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.body()
channel.copyTo(file.writeChannel())
}
Vlad
05/30/2024, 9:13 AMAleksei Tirman [JB]
05/30/2024, 9:14 AMFile.writeChannel
method creates a ByteWriteChannel for the file.Vlad
05/30/2024, 9:14 AM