should I roll my own copying like with manual cancellation check like this?
Copy code
body.use {
it.byteStream().use { iss ->
var bytesCopied: Long = 0
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = iss.read(buffer)
while (bytes >= 0 && coroutineContext.isActive) { <-------------
...
bytesCopied += bytes
bytes = iss.read(buffer)
}
bytesCopied
}
}
I do get that cancellation is cooperative etc etc — just looking for a idiomatic-retrofit way
y
yschimke
12/12/2022, 7:14 PM
You could launch a suspend cancellable coroutine, do the work on IO dispatcher, and close the input on cancellation.
u
ursus
12/12/2022, 7:16 PM
okay so basically manual coroutine adapter?
y
yschimke
12/12/2022, 7:17 PM
Jake has a good suggestion below also.
yschimke
12/12/2022, 7:17 PM
Probably simpler
u
ursus
12/12/2022, 7:18 PM
I mean if I remove @Streaming then cancellation works as expected by default, so I kinda think adapter makes it more elegant but sure ill try that one as well