mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Copy stuff with closure progress
This commit is contained in:
parent
cae28c515c
commit
042f184d80
1 changed files with 27 additions and 2 deletions
|
@ -15,6 +15,7 @@ public class Kurl @Inject constructor(@Assisted val url: String, val http: Http)
|
||||||
val connection : URLConnection by lazy {
|
val connection : URLConnection by lazy {
|
||||||
URL(url).openConnection()
|
URL(url).openConnection()
|
||||||
}
|
}
|
||||||
|
|
||||||
val exists : Boolean
|
val exists : Boolean
|
||||||
get() {
|
get() {
|
||||||
if (url.contains("android")) {
|
if (url.contains("android")) {
|
||||||
|
@ -37,9 +38,33 @@ public class Kurl @Inject constructor(@Assisted val url: String, val http: Http)
|
||||||
private val estimatedSize: Int
|
private val estimatedSize: Int
|
||||||
get() = if (url.contains("kotlin-compiler")) 18000000 else 1000000
|
get() = if (url.contains("kotlin-compiler")) 18000000 else 1000000
|
||||||
|
|
||||||
fun toOutputStream(os: OutputStream) = ByteStreams.copy(connection.inputStream, os)
|
fun toOutputStream(os: OutputStream, progress: (Long) -> Unit) = copy(connection.inputStream, os, progress)
|
||||||
|
|
||||||
fun toFile(file: File) = toOutputStream(FileOutputStream(file))
|
fun toFile(file: File, progress: (Long) -> Unit = {}) = toOutputStream(FileOutputStream(file), progress)
|
||||||
|
|
||||||
|
private fun copy(from: InputStream, to: OutputStream, progress: (Long) -> Unit = {}) : Long {
|
||||||
|
val estimate =
|
||||||
|
if (connection is HttpURLConnection) {
|
||||||
|
(connection as HttpURLConnection).let {
|
||||||
|
it.contentLength
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
estimatedSize
|
||||||
|
}
|
||||||
|
|
||||||
|
val buf = ByteArray(estimatedSize)
|
||||||
|
var total: Long = 0
|
||||||
|
while (true) {
|
||||||
|
val r = from.read(buf)
|
||||||
|
if (r == -1) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
to.write(buf, 0, r)
|
||||||
|
total += r.toLong()
|
||||||
|
progress(total * 100 / estimate)
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
val string: String
|
val string: String
|
||||||
get() {
|
get() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue