From 21b00ac6cacbf628d85d6e03d98b7860bb6a6dfa Mon Sep 17 00:00:00 2001 From: tonymanou Date: Thu, 12 Jan 2017 02:02:52 +0100 Subject: [PATCH] Use try-with-resources in some places --- .../com/topjohnwu/magisk/AboutActivity.java | 4 +-- .../com/topjohnwu/magisk/LogFragment.java | 6 +---- .../magisk/receivers/RepoDlReceiver.java | 6 ++--- .../com/topjohnwu/magisk/utils/Async.java | 18 ++++++------- .../com/topjohnwu/magisk/utils/ZipUtils.java | 26 +++++++++---------- 5 files changed, 27 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java b/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java index 6da59e7a4..3c68cfe07 100644 --- a/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/AboutActivity.java @@ -63,13 +63,11 @@ public class AboutActivity extends AppCompatActivity { appVersionInfo.setSummary(BuildConfig.VERSION_NAME); String changes = null; - try { - InputStream is = getAssets().open("changelog.html"); + try (InputStream is = getAssets().open("changelog.html")) { int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); - is.close(); changes = new String(buffer); } catch (IOException ignored) { diff --git a/app/src/main/java/com/topjohnwu/magisk/LogFragment.java b/app/src/main/java/com/topjohnwu/magisk/LogFragment.java index 4d09deed3..09ffdb859 100644 --- a/app/src/main/java/com/topjohnwu/magisk/LogFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/LogFragment.java @@ -182,14 +182,10 @@ public class LogFragment extends Fragment { List in = Utils.readFile(MAGISK_LOG); - try { - FileWriter out = new FileWriter(targetFile); + try (FileWriter out = new FileWriter(targetFile)) { for (String line : in) { out.write(line + "\n"); } - out.close(); - - return true; } catch (IOException e) { e.printStackTrace(); diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java index b52903f07..b9830a45d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/RepoDlReceiver.java @@ -33,9 +33,9 @@ public class RepoDlReceiver extends DownloadReceiver { ZipUtils.signZip(mContext, buffer.getInputStream(), buffer, true); // Write it back to the downloaded zip - OutputStream out = mContext.getContentResolver().openOutputStream(mUri); - buffer.writeTo(out); - out.close(); + try (OutputStream out = mContext.getContentResolver().openOutputStream(mUri)) { + buffer.writeTo(out); + } } }.exec(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index 11ee12971..04a9d50a1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -194,21 +194,21 @@ public class Async { protected void copyToCache() throws Throwable { publishProgress(mContext.getString(R.string.copying_msg)); - try { - InputStream in = mContext.getContentResolver().openInputStream(mUri); - mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip"); - if (mCachedFile.exists() && !mCachedFile.delete()) { - throw new IOException(); - } - OutputStream outputStream = new FileOutputStream(mCachedFile); + mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip"); + if (mCachedFile.exists() && !mCachedFile.delete()) { + Log.e(Logger.TAG, "FlashZip: Error while deleting already existing file"); + throw new IOException(); + } + try ( + InputStream in = mContext.getContentResolver().openInputStream(mUri); + OutputStream outputStream = new FileOutputStream(mCachedFile) + ) { byte buffer[] = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } - outputStream.close(); Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath()); - in.close(); } catch (FileNotFoundException e) { Log.e(Logger.TAG, "FlashZip: Invalid Uri"); throw e; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.java b/app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.java index 705cd99bb..c5f4300d7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/ZipUtils.java @@ -125,17 +125,16 @@ public class ZipUtils { } public static void unzip(File file, File folder, String path) { - try { - int count; - FileOutputStream out; - File dest; - InputStream is; - JarEntry entry; - byte data[] = new byte[4096]; - JarFile zipfile = new JarFile(file); - Enumeration e = zipfile.entries(); + int count; + FileOutputStream out; + File dest; + InputStream is; + JarEntry entry; + byte data[] = new byte[4096]; + try (JarFile zipfile = new JarFile(file)) { + Enumeration e = zipfile.entries(); while(e.hasMoreElements()) { - entry = (JarEntry) e.nextElement(); + entry = e.nextElement(); if (!entry.getName().contains(path) || entry.getName().charAt(entry.getName().length() - 1) == '/') { // Ignore directories, only create files @@ -518,9 +517,10 @@ public class ZipUtils { .build(signer, publicKey)); gen.addCertificates(certs); CMSSignedData sigData = gen.generate(data, false); - ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded()); - DEROutputStream dos = new DEROutputStream(out); - dos.writeObject(asn1.readObject()); + try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) { + DEROutputStream dos = new DEROutputStream(out); + dos.writeObject(asn1.readObject()); + } } /** * Copy all the files in a manifest from input to output. We set