Limit use of jar building utility.

R=ager

Bug:
Change-Id: Ia77925c0c0c02c1f91352b8f4943e90fb8bc7336
diff --git a/src/test/java/com/android/tools/r8/R8RunArtTestsTest.java b/src/test/java/com/android/tools/r8/R8RunArtTestsTest.java
index 92de268..4f3a4c8 100644
--- a/src/test/java/com/android/tools/r8/R8RunArtTestsTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunArtTestsTest.java
@@ -19,7 +19,6 @@
 import com.android.tools.r8.utils.ArtErrorParser.ArtErrorInfo;
 import com.android.tools.r8.utils.DexInspector;
 import com.android.tools.r8.utils.FileUtils;
-import com.android.tools.r8.utils.JarBuilder;
 import com.android.tools.r8.utils.ListUtils;
 import com.google.common.base.Charsets;
 import com.google.common.collect.ImmutableList;
@@ -30,6 +29,7 @@
 import com.google.common.collect.Sets;
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -45,6 +45,8 @@
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.function.BiFunction;
+import java.util.jar.JarEntry;
+import java.util.jar.JarOutputStream;
 import java.util.stream.Collectors;
 import org.junit.ComparisonFailure;
 import org.junit.Rule;
@@ -1576,7 +1578,7 @@
       // Run Art on JAR file with multiple dex files.
       processedFile
           = temp.getRoot().toPath().resolve(specification.name + ".jar").toFile();
-      JarBuilder.buildJar(outputFiles, processedFile);
+      buildJar(outputFiles, processedFile);
     }
 
     boolean compileOnly = System.getProperty("jctf_compile_only", "0").equals("1");
@@ -1762,7 +1764,7 @@
         // Run Art on JAR file with multiple dex files.
         processedFile
             = temp.getRoot().toPath().resolve(specification.name + ".jar").toFile();
-        JarBuilder.buildJar(outputFiles, processedFile);
+        buildJar(outputFiles, processedFile);
       }
 
       File expectedFile = specification.resolveFile("expected.txt");
@@ -1874,4 +1876,18 @@
     }
     throw errors.get(errors.size() - 1);
   }
+
+  // TODO(zerny): Refactor tests to output jar files directly and eliminate this method.
+  private static void buildJar(File[] files, File jarFile) throws IOException {
+    try (JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile))) {
+      for (File file : files) {
+        // Only use the file name in the JAR entry (classes.dex, classes2.dex, ...)
+        JarEntry entry = new JarEntry(file.getName());
+        entry.setTime(file.lastModified());
+        target.putNextEntry(entry);
+        Files.copy(file.toPath(), target);
+        target.closeEntry();
+      }
+    }
+  }
 }
diff --git a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
index 614f771..f1ebcbb 100644
--- a/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
+++ b/src/test/java/com/android/tools/r8/R8RunExamplesTest.java
@@ -15,10 +15,8 @@
 import com.android.tools.r8.ToolHelper.DexVm.Version;
 import com.android.tools.r8.errors.Unreachable;
 import com.android.tools.r8.shaking.ProguardRuleParserException;
-import com.android.tools.r8.utils.JarBuilder;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import java.io.File;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -163,6 +161,10 @@
     this.mainClass = mainClass;
   }
 
+  private Path getOutputFile() {
+    return temp.getRoot().toPath().resolve("out.jar");
+  }
+
   private Path getInputFile() {
     switch(input) {
       case DX:
@@ -201,7 +203,7 @@
       case D8: {
         ToolHelper.runD8(D8Command.builder()
             .addProgramFiles(getInputFile())
-            .setOutputPath(out)
+            .setOutputPath(getOutputFile())
             .setMode(mode)
             .build());
         break;
@@ -209,7 +211,7 @@
       case R8: {
         ToolHelper.runR8(R8Command.builder()
             .addProgramFiles(getInputFile())
-            .setOutputPath(out)
+            .setOutputPath(getOutputFile())
             .setMode(mode)
             .build());
         break;
@@ -226,19 +228,7 @@
     }
 
     String original = getOriginalDexFile().toString();
-
-    File generated;
-    // Collect the generated dex files.
-    File[] outputFiles =
-        temp.getRoot().listFiles((File file) -> file.getName().endsWith(".dex"));
-    if (outputFiles.length == 1) {
-      // Just run Art on classes.dex.
-      generated = outputFiles[0];
-    } else {
-      // Run Art on JAR file with multiple dex files.
-      generated = temp.getRoot().toPath().resolve(pkg + ".jar").toFile();
-      JarBuilder.buildJar(outputFiles, generated);
-    }
+    Path generated = getOutputFile();
 
     ToolHelper.ProcessResult javaResult =
         ToolHelper.runJava(ImmutableList.of(getOriginalJarFile("").toString()), mainClass);
diff --git a/src/test/java/com/android/tools/r8/utils/JarBuilder.java b/src/test/java/com/android/tools/r8/utils/JarBuilder.java
deleted file mode 100644
index 9186ff4..0000000
--- a/src/test/java/com/android/tools/r8/utils/JarBuilder.java
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyright (c) 2016, 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.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.jar.JarEntry;
-import java.util.jar.JarOutputStream;
-
-public class JarBuilder {
-  public static void buildJar(File[] files, File jarFile) throws IOException {
-    JarOutputStream target = new JarOutputStream(new FileOutputStream(jarFile));
-    for (File file : files) {
-      // Only use the file name in the JAR entry (classes.dex, classes2.dex, ...)
-      JarEntry entry = new JarEntry(file.getName());
-      entry.setTime(file.lastModified());
-      target.putNextEntry(entry);
-      InputStream in = new BufferedInputStream(new FileInputStream(file));
-      ByteStreams.copy(in, target);
-      in.close();
-      target.closeEntry();
-    }
-    target.close();
-  }
-}