Version 1.2.38

Merge: Ensure that we close input streams to not leak resources
CL: https://r8-review.googlesource.com/c/r8/+/25020
Bug: 112488773

Change-Id: I50ad4a3e3cece57f9598d92b0e7523b85dc1add3
diff --git a/src/main/java/com/android/tools/r8/ExtractMarker.java b/src/main/java/com/android/tools/r8/ExtractMarker.java
index 734dc4a..d939dbe 100644
--- a/src/main/java/com/android/tools/r8/ExtractMarker.java
+++ b/src/main/java/com/android/tools/r8/ExtractMarker.java
@@ -72,12 +72,14 @@
       throws IOException, ResourceException {
     if (FileUtils.isVDexFile(file)) {
       PathOrigin vdexOrigin = new PathOrigin(file);
-      VDexReader vdexReader = new VDexReader(vdexOrigin, Files.newInputStream(file));
-      VDexParser vDexParser = new VDexParser(vdexReader);
-      int index = 0;
-      for (byte[] bytes : vDexParser.getDexFiles()) {
-        appBuilder.addDexProgramData(bytes, new VdexOrigin(vdexOrigin, index));
-        index++;
+      try (InputStream fileInputStream = Files.newInputStream(file)) {
+        VDexReader vdexReader = new VDexReader(vdexOrigin, fileInputStream);
+        VDexParser vDexParser = new VDexParser(vdexReader);
+        int index = 0;
+        for (byte[] bytes : vDexParser.getDexFiles()) {
+          appBuilder.addDexProgramData(bytes, new VdexOrigin(vdexOrigin, index));
+          index++;
+        }
       }
     } else {
       appBuilder.addProgramFiles(file);
diff --git a/src/main/java/com/android/tools/r8/JarDiff.java b/src/main/java/com/android/tools/r8/JarDiff.java
index dad57d4..025f637 100644
--- a/src/main/java/com/android/tools/r8/JarDiff.java
+++ b/src/main/java/com/android/tools/r8/JarDiff.java
@@ -3,8 +3,6 @@
 // BSD-style license that can be found in the LICENSE file.
 package com.android.tools.r8;
 
-import static com.google.common.io.ByteStreams.toByteArray;
-
 import com.android.tools.r8.cf.code.CfInstruction;
 import com.android.tools.r8.graph.ClassKind;
 import com.android.tools.r8.graph.DexClass;
@@ -14,6 +12,7 @@
 import com.android.tools.r8.graph.JarClassFileReader;
 import com.android.tools.r8.origin.PathOrigin;
 import com.android.tools.r8.utils.InternalOptions;
+import com.android.tools.r8.utils.StreamUtils;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.nio.file.Path;
@@ -135,7 +134,8 @@
 
   private byte[] getClassAsBytes(ArchiveClassFileProvider inputJar, String descriptor)
       throws Exception {
-    return toByteArray(inputJar.getProgramResource(descriptor).getByteStream());
+    return StreamUtils.StreamToByteArrayClose(
+        inputJar.getProgramResource(descriptor).getByteStream());
   }
 
   private DexProgramClass getDexProgramClass(Path path, byte[] bytes) throws IOException {
diff --git a/src/main/java/com/android/tools/r8/Version.java b/src/main/java/com/android/tools/r8/Version.java
index 9e967a2..dfa80e1 100644
--- a/src/main/java/com/android/tools/r8/Version.java
+++ b/src/main/java/com/android/tools/r8/Version.java
@@ -11,7 +11,7 @@
 
   // This field is accessed from release scripts using simple pattern matching.
   // Therefore, changing this field could break our release scripts.
-  public static final String LABEL = "1.2.37";
+  public static final String LABEL = "1.2.38";
 
   private Version() {
   }
diff --git a/src/main/java/com/android/tools/r8/dex/BinaryReader.java b/src/main/java/com/android/tools/r8/dex/BinaryReader.java
index 127e1d5..5d51238 100644
--- a/src/main/java/com/android/tools/r8/dex/BinaryReader.java
+++ b/src/main/java/com/android/tools/r8/dex/BinaryReader.java
@@ -8,7 +8,7 @@
 import com.android.tools.r8.ResourceException;
 import com.android.tools.r8.origin.Origin;
 import com.android.tools.r8.utils.LebUtils;
-import com.google.common.io.ByteStreams;
+import com.android.tools.r8.utils.StreamUtils;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
@@ -20,7 +20,7 @@
   protected final ByteBuffer buffer;
 
   protected BinaryReader(ProgramResource resource) throws ResourceException, IOException {
-    this(resource.getOrigin(), ByteStreams.toByteArray(resource.getByteStream()));
+    this(resource.getOrigin(), StreamUtils.StreamToByteArrayClose(resource.getByteStream()));
   }
 
   protected BinaryReader(Origin origin, byte[] bytes) {
diff --git a/src/main/java/com/android/tools/r8/utils/StreamUtils.java b/src/main/java/com/android/tools/r8/utils/StreamUtils.java
new file mode 100644
index 0000000..8d4ebc3
--- /dev/null
+++ b/src/main/java/com/android/tools/r8/utils/StreamUtils.java
@@ -0,0 +1,20 @@
+// Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.utils;
+
+import com.google.common.io.ByteStreams;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class StreamUtils {
+  /**
+   * Read all data from the stream into a byte[], close the stream and return the bytes.
+   * @return The bytes of the stream
+   */
+  public static byte[] StreamToByteArrayClose(InputStream inputStream) throws IOException {
+    byte[] result = ByteStreams.toByteArray(inputStream);
+    inputStream.close();
+    return result;
+  }
+}