From 869d287aaab61995e537bcfdfb8a043559897d71 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 22 Oct 2014 22:03:59 -0500 Subject: [PATCH] Cleanup - switch to java7 try-with-resources - add check in unit-tests for checking control --- .../androlib/res/decoder/ResFileDecoder.java | 51 +++++-------------- .../brut/androlib/BuildAndDecodeTest.java | 2 +- 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java index 9be175f7..c2c3c3e5 100644 --- a/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java +++ b/brut.apktool/apktool-lib/src/main/java/brut/androlib/res/decoder/ResFileDecoder.java @@ -24,6 +24,7 @@ import brut.androlib.res.data.value.ResFileValue; import brut.directory.DirUtil; import brut.directory.Directory; import brut.directory.DirectoryException; + import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -106,25 +107,13 @@ public class ResFileDecoder { public void decode(Directory inDir, String inFileName, Directory outDir, String outFileName, String decoder) throws AndrolibException { - InputStream in = null; - OutputStream out = null; - try { - in = inDir.getFileInput(inFileName); - out = outDir.getFileOutput(outFileName); + try ( + InputStream in = inDir.getFileInput(inFileName); + OutputStream out = outDir.getFileOutput(outFileName) + ) { mDecoders.decode(in, out, decoder); - } catch (DirectoryException ex) { + } catch (DirectoryException | IOException ex) { throw new AndrolibException(ex); - } finally { - try { - if (in != null) { - in.close(); - } - if (out != null) { - out.close(); - } - } catch (IOException ex) { - throw new AndrolibException(ex); - } } } @@ -138,29 +127,15 @@ public class ResFileDecoder { public void decodeManifest(Directory inDir, String inFileName, Directory outDir, String outFileName) throws AndrolibException { - InputStream in = null; - OutputStream out = null; - try { - in = inDir.getFileInput(inFileName); - out = outDir.getFileOutput(outFileName); - ((XmlPullStreamDecoder) mDecoders.getDecoder("xml")) - .decodeManifest(in, out); - } catch (DirectoryException ex) { + try ( + InputStream in = inDir.getFileInput(inFileName); + OutputStream out = outDir.getFileOutput(outFileName) + ) { + ((XmlPullStreamDecoder) mDecoders.getDecoder("xml")).decodeManifest(in, out); + } catch (DirectoryException | IOException ex) { throw new AndrolibException(ex); - } finally { - try { - if (in != null) { - in.close(); - } - if (out != null) { - out.close(); - } - } catch (IOException ex) { - throw new AndrolibException(ex); - } } } - private final static Logger LOGGER = Logger.getLogger(ResFileDecoder.class - .getName()); + private final static Logger LOGGER = Logger.getLogger(ResFileDecoder.class.getName()); } diff --git a/brut.apktool/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java b/brut.apktool/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java index 2a8cb034..9a82d9d8 100644 --- a/brut.apktool/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java +++ b/brut.apktool/apktool-lib/src/test/java/brut/androlib/BuildAndDecodeTest.java @@ -256,7 +256,7 @@ public class BuildAndDecodeTest { // hacky fix - load test by changing name of control File test = new File(control.toString().replace("testapp-orig", "testapp-new")); - if (test.isFile()) { + if (test.isFile() && control.isFile()) { if (control.hashCode() != test.hashCode()) { return false; }