37 lines
1.0 KiB
Kotlin
37 lines
1.0 KiB
Kotlin
package com.leos.droidify.utility
|
|
|
|
import java.io.InputStream
|
|
|
|
fun InputStream.getProgress(callback: (Long) -> Unit): InputStream =
|
|
ProgressInputStream(this, callback)
|
|
|
|
private class ProgressInputStream(
|
|
private val inputStream: InputStream,
|
|
private val callback: (Long) -> Unit
|
|
) : InputStream() {
|
|
private var count = 0L
|
|
|
|
private inline fun <reified T : Number> notify(one: Boolean, read: () -> T): T {
|
|
val result = read()
|
|
count += if (one) 1L else result.toLong()
|
|
callback(count)
|
|
return result
|
|
}
|
|
|
|
override fun read(): Int = notify(true) { inputStream.read() }
|
|
override fun read(b: ByteArray): Int = notify(false) { inputStream.read(b) }
|
|
override fun read(b: ByteArray, off: Int, len: Int): Int =
|
|
notify(false) { inputStream.read(b, off, len) }
|
|
|
|
override fun skip(n: Long): Long = notify(false) { inputStream.skip(n) }
|
|
|
|
override fun available(): Int {
|
|
return inputStream.available()
|
|
}
|
|
|
|
override fun close() {
|
|
inputStream.close()
|
|
super.close()
|
|
}
|
|
}
|