Do I need to call close() on a ByteReadPacket afte...
# ktor
r
Do I need to call close() on a ByteReadPacket after reading all of its data? I will still be using the underlying source (ByteReadChannel) after consuming the packet. The code sample I found seems to rely on closing the underlying source:
Copy code
val 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#streaming
a
The overridden
closeSource
method for the
ByteReadPacket
class does nothing so the underlying source won't be closed when the packet is closed.
r
Got it. Thank you!