kevin.cianfarini
12/04/2018, 7:40 PMcy
12/04/2018, 8:07 PMclient.get<HttpResponse>(...).use { response ->
val channel = response.content
...
}
kevin.cianfarini
12/04/2018, 8:43 PMkevin.cianfarini
12/04/2018, 8:43 PMkevin.cianfarini
12/05/2018, 3:35 PMkevin.cianfarini
12/05/2018, 3:37 PMsuspend fun downloadFile(url: String) = GlobalScope.produce {
HttpClient().get<HttpResponse>(url).use { response ->
val size = (response.headers["Content-Length"] ?: "-1").toInt()
val buf = ByteBuffer.allocate(2048)
FileOutputStream(File(DocSetStore.docSetStore, url.split("/").last())).use { out ->
var totalRead = 0
do {
val readAmount = response.content.readAvailable(buf)
if (readAmount != -1) {
totalRead += readAmount
}
buf.flip()
out.channel.write(buf)
buf.clear()
send(
if (size == -1) {
-1L
} else {
(totalRead * 100L) / size
}
)
} while (readAmount > -1)
}
}
}