DownloadWorker: Initial implementation

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-10-25 18:09:03 +05:30
parent debb0bf920
commit 6f707b2a37
7 changed files with 304 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
package com.aurora.extensions
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import java.io.InputStream
import java.io.OutputStream
fun InputStream.copyTo(out: OutputStream, streamSize: Long): Flow<Int> {
return flow {
var bytesCopied: Long = 0
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = read(buffer)
while (bytes >= 0) {
out.write(buffer, 0, bytes)
bytesCopied += bytes
bytes = read(buffer)
// Emit stream progress in percentage
emit((bytesCopied * 100 / streamSize).toInt())
}
}.flowOn(Dispatchers.IO).distinctUntilChanged()
}