Ryder Huggins
12/31/2023, 5:43 PMval 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}")
}
}
From: https://ktor.io/docs/response.html#streamingAleksei Tirman [JB]
01/02/2024, 10:47 AMcloseSource
method for the ByteReadPacket
class does nothing so the underlying source won't be closed when the packet is closed.Ryder Huggins
01/02/2024, 12:35 PM