Vincent Barthelemy
11/24/2024, 2:59 PMwriteChannel
and bodyAsChannel().copyAndClose
: destination file does not exist yet when copyAndClose
returns.Vincent Barthelemy
11/24/2024, 2:59 PMval client = HttpClient(CIO)
repeat(10) {
val dest = File("tmp_$it.html").apply { delete() }
println("Iteration $it")
val response = client.get("<https://example.com/>")
val nbByteCopied = response.bodyAsChannel().copyAndClose(dest.writeChannel())
println("Copied $nbByteCopied bytes - Dest file exists? ${dest.exists()} with length ${dest.length()}")
}
Iteration 0
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 1
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 2
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 3
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 4
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 5
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 6
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 7
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 8
Copied 1256 bytes - Dest file exists? false with length 0
Iteration 9
Copied 1256 bytes - Dest file exists? false with length 0
Vincent Barthelemy
11/24/2024, 3:02 PM3.0.1
here.
Also copyAndClose
is marked with @OptIn(InternalAPI::class)
so it might be a better / recommended way to copy a response to a file?Vincent Barthelemy
11/24/2024, 3:06 PMresponse.bodyAsChannel().copyAndClose(dest.writeChannel())
returns the underlying file should exist and contain all the copied bytes?Aleksei Tirman [JB]
11/25/2024, 7:54 AMAlsoThat means this method uses an internal Ktor API for its implementation. For the client, theis marked withcopyAndClose
so it might be a better / recommended way to copy a response to a file?@OptIn(InternalAPI::class)
copyAndClose
method isn't internal.
Am I doing a mistake assuming/thinking that whenYour assumption is correct. This is a bug, so I've filed an issue.returns the underlying file should exist and contain all the copied bytes?response.bodyAsChannel().copyAndClose(dest.writeChannel())
Vincent Barthelemy
11/25/2024, 8:18 AMVincent Barthelemy
11/25/2024, 8:31 AMval nbByteCopied = response.bodyAsChannel().copyAndClose(destFile.writeChannel())
withTimeout(1000L) {
while (isActive && destFile.length() != nbByteCopied) delay(50L)
}
It might be naive but it works in my use case.