Use try-with-resources in some places

This commit is contained in:
tonymanou 2017-01-12 02:02:52 +01:00 committed by topjohnwu
parent 57e6f3080c
commit 21b00ac6ca
5 changed files with 27 additions and 33 deletions

View File

@ -63,13 +63,11 @@ public class AboutActivity extends AppCompatActivity {
appVersionInfo.setSummary(BuildConfig.VERSION_NAME); appVersionInfo.setSummary(BuildConfig.VERSION_NAME);
String changes = null; String changes = null;
try { try (InputStream is = getAssets().open("changelog.html")) {
InputStream is = getAssets().open("changelog.html");
int size = is.available(); int size = is.available();
byte[] buffer = new byte[size]; byte[] buffer = new byte[size];
is.read(buffer); is.read(buffer);
is.close();
changes = new String(buffer); changes = new String(buffer);
} catch (IOException ignored) { } catch (IOException ignored) {

View File

@ -182,14 +182,10 @@ public class LogFragment extends Fragment {
List<String> in = Utils.readFile(MAGISK_LOG); List<String> in = Utils.readFile(MAGISK_LOG);
try { try (FileWriter out = new FileWriter(targetFile)) {
FileWriter out = new FileWriter(targetFile);
for (String line : in) { for (String line : in) {
out.write(line + "\n"); out.write(line + "\n");
} }
out.close();
return true; return true;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -33,9 +33,9 @@ public class RepoDlReceiver extends DownloadReceiver {
ZipUtils.signZip(mContext, buffer.getInputStream(), buffer, true); ZipUtils.signZip(mContext, buffer.getInputStream(), buffer, true);
// Write it back to the downloaded zip // Write it back to the downloaded zip
OutputStream out = mContext.getContentResolver().openOutputStream(mUri); try (OutputStream out = mContext.getContentResolver().openOutputStream(mUri)) {
buffer.writeTo(out); buffer.writeTo(out);
out.close(); }
} }
}.exec(); }.exec();
} }

View File

@ -194,21 +194,21 @@ public class Async {
protected void copyToCache() throws Throwable { protected void copyToCache() throws Throwable {
publishProgress(mContext.getString(R.string.copying_msg)); publishProgress(mContext.getString(R.string.copying_msg));
try { mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip");
InputStream in = mContext.getContentResolver().openInputStream(mUri); if (mCachedFile.exists() && !mCachedFile.delete()) {
mCachedFile = new File(mContext.getCacheDir().getAbsolutePath() + "/install.zip"); Log.e(Logger.TAG, "FlashZip: Error while deleting already existing file");
if (mCachedFile.exists() && !mCachedFile.delete()) { throw new IOException();
throw new IOException(); }
} try (
OutputStream outputStream = new FileOutputStream(mCachedFile); InputStream in = mContext.getContentResolver().openInputStream(mUri);
OutputStream outputStream = new FileOutputStream(mCachedFile)
) {
byte buffer[] = new byte[1024]; byte buffer[] = new byte[1024];
int length; int length;
while ((length = in.read(buffer)) > 0) { while ((length = in.read(buffer)) > 0) {
outputStream.write(buffer, 0, length); outputStream.write(buffer, 0, length);
} }
outputStream.close();
Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath()); Logger.dev("FlashZip: File created successfully - " + mCachedFile.getPath());
in.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e(Logger.TAG, "FlashZip: Invalid Uri"); Log.e(Logger.TAG, "FlashZip: Invalid Uri");
throw e; throw e;

View File

@ -125,17 +125,16 @@ public class ZipUtils {
} }
public static void unzip(File file, File folder, String path) { public static void unzip(File file, File folder, String path) {
try { int count;
int count; FileOutputStream out;
FileOutputStream out; File dest;
File dest; InputStream is;
InputStream is; JarEntry entry;
JarEntry entry; byte data[] = new byte[4096];
byte data[] = new byte[4096]; try (JarFile zipfile = new JarFile(file)) {
JarFile zipfile = new JarFile(file); Enumeration<JarEntry> e = zipfile.entries();
Enumeration e = zipfile.entries();
while(e.hasMoreElements()) { while(e.hasMoreElements()) {
entry = (JarEntry) e.nextElement(); entry = e.nextElement();
if (!entry.getName().contains(path) if (!entry.getName().contains(path)
|| entry.getName().charAt(entry.getName().length() - 1) == '/') { || entry.getName().charAt(entry.getName().length() - 1) == '/') {
// Ignore directories, only create files // Ignore directories, only create files
@ -518,9 +517,10 @@ public class ZipUtils {
.build(signer, publicKey)); .build(signer, publicKey));
gen.addCertificates(certs); gen.addCertificates(certs);
CMSSignedData sigData = gen.generate(data, false); CMSSignedData sigData = gen.generate(data, false);
ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded()); try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) {
DEROutputStream dos = new DEROutputStream(out); DEROutputStream dos = new DEROutputStream(out);
dos.writeObject(asn1.readObject()); dos.writeObject(asn1.readObject());
}
} }
/** /**
* Copy all the files in a manifest from input to output. We set * Copy all the files in a manifest from input to output. We set