Michal Klimczak
06/16/2021, 7:36 AMjava.net.SocketException: Software caused connection abort
(thread)Michal Klimczak
06/16/2021, 7:37 AMvar hashingSink: HashingSink? = null
var writeSink: BufferedSink? = null
try {
hashingSink = HashingSink.md5(destinationFile.sink())
writeSink = hashingSink.buffer()
NetworkClient.client.get<HttpStatement>(url = Url(urlPath)).execute { response: HttpResponse ->
var offset = 0
val channel = response.receive<ByteReadChannel>()
val contentLen = response.contentLength()?.toInt() ?: 0
val byteBufferSize = 1024 * 100
val buffer = ByteArray(byteBufferSize)
do {
val currentRead = channel.readAvailable(buffer, 0, byteBufferSize)
if (currentRead <= 0) break
writeSink.write(buffer, 0, currentRead)
val progress = if (contentLen == 0) 0 else (offset / contentLen.toFloat()) * 100
emit(progress)
offset += currentRead
} while (!channel.isClosedForRead)
writeSink.close()
}
val downloadChecksum = hashingSink.hash.hex()
if (downloadChecksum != checksum) throw ChecksumException(downloadChecksum, checksum)
} finally {
writeSink?.close()
hashingSink?.close()
}
Michal Klimczak
06/16/2021, 7:38 AMjava.net.SocketException
- this is expected and fineMichal Klimczak
06/16/2021, 7:38 AMHttpClient
also fail with that exceptionMichal Klimczak
06/16/2021, 7:39 AMMichal Klimczak
06/16/2021, 7:48 AMMichal Klimczak
06/16/2021, 7:58 AMRustam Siniukov
06/16/2021, 11:01 AMMichal Klimczak
06/16/2021, 2:39 PMMichal Klimczak
06/16/2021, 2:45 PM