From 894434fdb41a6b217d46a98d259bedc9ea612fd5 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Sun, 29 Nov 2020 20:01:58 +0700 Subject: [PATCH] Changes - [JRE auto install] Use short-hand methods - [IOUtils.copy()] check and add close() after them --- .../kdt/pojavlaunch/PojavLoginActivity.java | 41 +++++++------------ .../main/java/net/kdt/pojavlaunch/Tools.java | 5 ++- .../installers/LegacyForgeInstaller.java | 4 +- .../tasks/MinecraftDownloaderTask.java | 6 ++- .../kdt/pojavlaunch/utils/DownloadUtils.java | 1 + 5 files changed, 26 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java b/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java index fe64e7489..fcbd4dd3d 100644 --- a/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java +++ b/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java @@ -364,31 +364,18 @@ public class PojavLoginActivity extends BaseActivity fos.close(); iis.close(); } - }else { + } else { FileInputStream fis = new FileInputStream(new File(Tools.MAIN_PATH + "/lwjgl3/version")); byte[] release1 = new byte[is.available()]; byte[] release2 = new byte[fis.available()]; is.read(release1); fis.read(release2); - if(!Arrays.equals(release1,release2)) { + if (!Arrays.equals(release1,release2)) { String[] lwjglFileList = am.list("components/lwjgl3"); - FileOutputStream fos; - InputStream iis; - for(String s : lwjglFileList) { - iis = am.open("components/lwjgl3/"+s); - fos = new FileOutputStream(new File(Tools.MAIN_PATH+"/lwjgl3/"+s)); - /* - int i; byte[] buf = new byte[1024]; - while((i = iis.read(buf)) != -1) { - fos.write(buf,0,i); - } - */ - IOUtils.copy(iis,fos); - - fos.close(); - iis.close(); + for (String s : lwjglFileList) { + Tools.copyAssetFile(this, "components/lwjgl3/" + s, Tools.MAIN_PATH + "/lwjgl3", true); } - }else{ + } else { Log.i("LWJGL3Prep","Pack is up-to-date with the launcher, continuing..."); } } @@ -399,10 +386,7 @@ public class PojavLoginActivity extends BaseActivity uncompressTarXZ(jreTarFile, new File(Tools.homeJreDir)); } setPref(PREF_IS_INSTALLED_JAVARUNTIME, true); - byte[] buf = new byte[1024]; - int i = am.open("components/jre/version").read(buf);; - String s = new String(buf,0,i); - setPref(PREF_JAVARUNTIME_VER,s); + setPref(PREF_JAVARUNTIME_VER, Tools.read(am.open("components/jre/version"))); } JREUtils.relocateLibPath(this); @@ -464,10 +448,11 @@ public class PojavLoginActivity extends BaseActivity private void copyDummyNativeLib(String name) throws Throwable { File fileLib = new File(Tools.homeJreDir, Tools.homeJreLib + "/" + name); fileLib.delete(); - IOUtils.copy( - new FileInputStream(new File(getApplicationInfo().nativeLibraryDir, name)), - new FileOutputStream(fileLib) - ); + FileInputStream is = new FileInputStream(new File(getApplicationInfo().nativeLibraryDir, name)); + FileOutputStream os = new FileOutputStream(fileLib); + IOUtils.copy(is, os); + is.close(); + os.close(); } private File selectJreTarFile() throws InterruptedException { @@ -562,7 +547,9 @@ public class PojavLoginActivity extends BaseActivity destPath.createNewFile(); // destPath.setExecutable(true); - IOUtils.copy(tarIn, new FileOutputStream(destPath)); + FileOutputStream os = new FileOutputStream(destPath); + IOUtils.copy(tarIn, os); + os.close(); /* byte[] btoRead = new byte[2048]; diff --git a/app/src/main/java/net/kdt/pojavlaunch/Tools.java b/app/src/main/java/net/kdt/pojavlaunch/Tools.java index 5226cb97d..87a7615f5 100644 --- a/app/src/main/java/net/kdt/pojavlaunch/Tools.java +++ b/app/src/main/java/net/kdt/pojavlaunch/Tools.java @@ -674,8 +674,8 @@ public final class Tools outPath.getParentFile().mkdirs(); outPath.createNewFile(); - FileOutputStream fos = new FileOutputStream(path); - fos.write(content); + BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(path)); + fos.write(content, 0, content.length); fos.close(); } @@ -704,6 +704,7 @@ public final class Tools File file = new File(nameOutput); DownloadUtils.downloadFile(urlInput, file); } + public static class ZipTool { private ZipTool(){} diff --git a/app/src/main/java/net/kdt/pojavlaunch/installers/LegacyForgeInstaller.java b/app/src/main/java/net/kdt/pojavlaunch/installers/LegacyForgeInstaller.java index e9f4bf34c..80021d491 100644 --- a/app/src/main/java/net/kdt/pojavlaunch/installers/LegacyForgeInstaller.java +++ b/app/src/main/java/net/kdt/pojavlaunch/installers/LegacyForgeInstaller.java @@ -37,8 +37,10 @@ public class LegacyForgeInstaller extends BaseInstaller { libraryFile.getParentFile().mkdirs(); target = libraryFile.getAbsolutePath().replace("-universal", ""); ctx.appendlnToLog("Writing " + target); + InputStream in = mJarFile.getInputStream(mJarFile.getEntry(profile.install.filePath)); FileOutputStream out = new FileOutputStream(target); - IOUtils.copy(mJarFile.getInputStream(mJarFile.getEntry(profile.install.filePath)), out); + IOUtils.copy(in, out); + in.close(); out.close(); mJarFile.close(); diff --git a/app/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloaderTask.java b/app/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloaderTask.java index d08cf191a..d18ce5889 100644 --- a/app/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloaderTask.java +++ b/app/src/main/java/net/kdt/pojavlaunch/tasks/MinecraftDownloaderTask.java @@ -125,7 +125,11 @@ public class MinecraftDownloaderTask extends AsyncTask