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 is = new CipherInputStream(new ByteArrayInputStream(Bytes.res()), cipher);
var out = new FileOutputStream(apk); var out = new FileOutputStream(apk);
try (is; out) { try (is; out) {
byte[] buf = new byte[4096]; APKInstall.transfer(is, out);
for (int read; (read = is.read(buf)) >= 0;) {
out.write(buf, 0, read);
}
} }
DynAPK.addAssetPath(getResources().getAssets(), apk.getPath()); DynAPK.addAssetPath(getResources().getAssets(), apk.getPath());
} catch (Exception ignored) { } catch (Exception ignored) {

View File

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

View File

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