Fix stream not being closed on all exception paths.
Change-Id: Ie5474147ade91463b2aa271a6a37c8ee7ba041be
diff --git a/dexgen/src/com/android/dexgen/util/FileUtils.java b/dexgen/src/com/android/dexgen/util/FileUtils.java
index a5bbff4..b1ad30e 100644
--- a/dexgen/src/com/android/dexgen/util/FileUtils.java
+++ b/dexgen/src/com/android/dexgen/util/FileUtils.java
@@ -72,17 +72,21 @@
byte[] result = new byte[length];
try {
+ // convert to try-with-resources once dexgen uses an Android API 19+ which supports it
FileInputStream in = new FileInputStream(file);
- int at = 0;
- while (length > 0) {
- int amt = in.read(result, at, length);
- if (amt == -1) {
- throw new RuntimeException(file + ": unexpected EOF");
+ try {
+ int at = 0;
+ while (length > 0) {
+ int amt = in.read(result, at, length);
+ if (amt == -1) {
+ throw new RuntimeException(file + ": unexpected EOF");
+ }
+ at += amt;
+ length -= amt;
}
- at += amt;
- length -= amt;
+ } finally {
+ in.close();
}
- in.close();
} catch (IOException ex) {
throw new RuntimeException(file + ": trouble reading", ex);
}