Use InputStream transfer

This commit is contained in:
vvb2060 2021-12-15 03:18:23 +08:00 committed by John Wu
parent f1a3ef9590
commit d3ade06421
3 changed files with 21 additions and 30 deletions

View File

@ -140,10 +140,7 @@ public class DownloadActivity extends Activity {
var is = new CipherInputStream(new ByteArrayInputStream(Bytes.res()), cipher);
var out = new FileOutputStream(apk);
try (is; out) {
byte[] buf = new byte[4096];
for (int read; (read = is.read(buf)) >= 0;) {
out.write(buf, 0, read);
}
APKInstall.transfer(is, out);
}
DynAPK.addAssetPath(getResources().getAssets(), apk.getPath());
} catch (Exception ignored) {

View File

@ -12,33 +12,24 @@ import android.net.Uri;
import android.os.Build;
import android.util.Log;
import com.topjohnwu.magisk.utils.APKInstall;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import io.michaelrocks.paranoid.Obfuscate;
@Obfuscate
@SuppressWarnings("ResultOfMethodCallIgnored")
public class DynLoad {
static Object componentFactory;
static final DynAPK.Data apkData = createApkData();
private static void copy(InputStream src, OutputStream dest) throws IOException {
try (InputStream s = src) {
try (OutputStream o = dest) {
byte[] buf = new byte[8192];
for (int read; (read = s.read(buf)) >= 0;) {
o.write(buf, 0, read);
}
}
}
}
// Dynamically load APK, inject ClassLoader into ContextImpl, then
// create the actual Application instance from the loaded APK
static Application inject(Context context) {
@ -55,7 +46,11 @@ public class DynLoad {
File external = new File(context.getExternalFilesDir(null), "magisk.apk");
if (external.exists()) {
try {
copy(new FileInputStream(external), new FileOutputStream(apk));
var in = new FileInputStream(external);
var out = new FileOutputStream(apk);
try (in; out) {
APKInstall.transfer(in, out);
}
} catch (IOException e) {
Log.e(DynLoad.class.getSimpleName(), "", e);
apk.delete();
@ -74,7 +69,10 @@ public class DynLoad {
try {
InputStream src = resolver.openInputStream(uri);
if (src != null) {
copy(src, new FileOutputStream(apk));
var out = new FileOutputStream(apk);
try (src; out) {
APKInstall.transfer(src, out);
}
}
} catch (IOException e) {
Log.e(DynLoad.class.getSimpleName(), "", e);
@ -148,6 +146,7 @@ public class DynLoad {
Field mInfo = context.getClass().getDeclaredField("mPackageInfo");
mInfo.setAccessible(true);
Object loadedApk = mInfo.get(context);
assert loadedApk != null;
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
mcl.setAccessible(true);
mcl.set(loadedApk, cl);

View File

@ -2,6 +2,8 @@ package com.topjohnwu.magisk.net;
import android.os.AsyncTask;
import com.topjohnwu.magisk.utils.APKInstall;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -24,7 +26,7 @@ import io.michaelrocks.paranoid.Obfuscate;
@Obfuscate
public class Request implements Closeable {
private HttpURLConnection conn;
private final HttpURLConnection conn;
private Executor executor = null;
private int code = -1;
@ -192,13 +194,9 @@ public class Request implements Closeable {
}
private File dlFile(File f) throws IOException {
try (InputStream in = getInputStream();
try (InputStream in = getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) {
int len;
byte[] buf = new byte[4096];
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
APKInstall.transfer(in, out);
}
return f;
}
@ -207,11 +205,8 @@ public class Request implements Closeable {
int len = conn.getContentLength();
len = len > 0 ? len : 32;
ByteArrayOutputStream out = new ByteArrayOutputStream(len);
try (InputStream in = getInputStream()) {
byte[] buf = new byte[4096];
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
try (InputStream in = getInputStream()) {
APKInstall.transfer(in, out);
}
return out.toByteArray();
}