https://kotlinlang.org logo
#ktor
Title
# ktor
m

Michal Klimczak

04/04/2021, 6:15 PM
I'm losing a few bytes while using
ByteReadChannel
and I have no idea why.
Copy code
someCall()
            .execute { response: HttpResponse ->
                var offset = 0
                val channel = response.receive<ByteReadChannel>()
                val contentLen = response.contentLength()?.toInt() ?: 0
                val temp = ByteArray(1024)
                while (!channel.isClosedForRead) {
                    val read = channel.readAvailable(temp)
                    if (read <= 0) break
                    offset += read
                    println("$offset / $contentLen")
                }
                println("End: $offset / $contentLen")
Copy code
...
94331817 / 94336479
94332841 / 94336479
94333857 / 94336479
End: 94333857 / 94336479
The worst part is that every time the result is a tiny bit different. End: 94334201 / 94336479 End: 94334515 / 94336479 That's a simplified piece of code, normally I write to ios storage - it's a zip file and the checksum is wrong (although it seems to unzip fine, maybe one of many files is corrupt, dunno). When I download this file via browser, the checksum is fine. ktor 1.5.3. Any ideas what I might be doing wrong?
Oh, and I just checked that on Android and it works fine. Seems to only be messed in Kotlin Native. A bug then?
Any alternative to ByteReadChannel maybe?
45 Views