mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-16 06:43:23 -08:00
Stream and process module zips
This commit is contained in:
@@ -95,10 +95,12 @@ fun File.provide(context: Context = get()): Uri {
|
||||
}
|
||||
|
||||
fun File.mv(destination: File) {
|
||||
inputStream().copyTo(destination)
|
||||
inputStream().writeTo(destination)
|
||||
deleteRecursively()
|
||||
}
|
||||
|
||||
fun String.toFile() = File(this)
|
||||
|
||||
fun Intent.chooser(title: String = "Pick an app") = Intent.createChooser(this, title)
|
||||
fun Intent.chooser(title: String = "Pick an app") = Intent.createChooser(this, title)
|
||||
|
||||
fun Context.cachedFile(name: String) = File(cacheDir, name)
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.topjohnwu.magisk.utils
|
||||
|
||||
import android.net.Uri
|
||||
import androidx.core.net.toFile
|
||||
import org.kamranzafar.jtar.TarInputStream
|
||||
import org.kamranzafar.jtar.TarOutputStream
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
@@ -18,12 +16,10 @@ fun ZipInputStream.forEach(callback: (ZipEntry) -> Unit) {
|
||||
}
|
||||
}
|
||||
|
||||
fun Uri.copyTo(file: File) = toFile().copyTo(file)
|
||||
fun InputStream.copyTo(file: File) =
|
||||
withStreams(this, file.outputStream()) { reader, writer -> reader.copyTo(writer) }
|
||||
fun Uri.writeTo(file: File) = toFile().copyTo(file)
|
||||
|
||||
fun File.tarInputStream() = TarInputStream(inputStream())
|
||||
fun File.tarOutputStream() = TarOutputStream(this)
|
||||
fun InputStream.writeTo(file: File) =
|
||||
withStreams(this, file.outputStream()) { reader, writer -> reader.copyTo(writer) }
|
||||
|
||||
inline fun <In : InputStream, Out : OutputStream> withStreams(
|
||||
inStream: In,
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
import android.content.Context
|
||||
import com.topjohnwu.superuser.internal.UiThreadHandler
|
||||
import okhttp3.ResponseBody
|
||||
import java.io.File
|
||||
import java.io.FilterInputStream
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
@@ -32,8 +34,6 @@ inline fun InputStream.writeTo(output: OutputStream, progress: (Long) -> Unit =
|
||||
|
||||
fun ResponseBody.writeToString() = string()
|
||||
|
||||
fun Context.cachedFile(name: String) = File(cacheDir, name)
|
||||
|
||||
inline fun InputStream.copyToWithProgress(
|
||||
out: OutputStream,
|
||||
progressEmitter: (Long) -> Unit,
|
||||
@@ -49,4 +49,32 @@ inline fun InputStream.copyToWithProgress(
|
||||
progressEmitter(bytesCopied)
|
||||
}
|
||||
return bytesCopied
|
||||
}
|
||||
|
||||
class ProgInputStream(base: InputStream,
|
||||
val progressEmitter: (Long) -> Unit = {}) : FilterInputStream(base) {
|
||||
|
||||
private var bytesRead : Long = 0
|
||||
|
||||
override fun read(): Int {
|
||||
val b = read()
|
||||
if (b >= 0) {
|
||||
bytesRead++
|
||||
UiThreadHandler.run { progressEmitter(bytesRead) }
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray): Int {
|
||||
return read(b, 0, b.size)
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray, off: Int, len: Int): Int {
|
||||
val sz = super.read(b, off, len)
|
||||
if (sz > 0) {
|
||||
bytesRead += sz
|
||||
UiThreadHandler.run { progressEmitter(bytesRead) }
|
||||
}
|
||||
return sz
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user