diff --git a/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java b/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java index 6c033d076..8319b4419 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java +++ b/stub/src/main/java/com/topjohnwu/magisk/DownloadActivity.java @@ -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) { diff --git a/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java b/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java index 15762ecc0..a921307e9 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java +++ b/stub/src/main/java/com/topjohnwu/magisk/DynLoad.java @@ -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); diff --git a/stub/src/main/java/com/topjohnwu/magisk/net/Request.java b/stub/src/main/java/com/topjohnwu/magisk/net/Request.java index 4c1c55de7..1187f383e 100644 --- a/stub/src/main/java/com/topjohnwu/magisk/net/Request.java +++ b/stub/src/main/java/com/topjohnwu/magisk/net/Request.java @@ -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(); }