Index: build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java |
diff --git a/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java b/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java |
index beb26facda44e40dc31ec9ced6e18415803a29c9..e800a3941395b34a4afc96938c6dac999b3b86c4 100644 |
--- a/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java |
+++ b/build/android/java_assertion_enabler/java/org/chromium/javaassertionenabler/AssertionEnabler.java |
@@ -3,6 +3,8 @@ |
// found in the LICENSE file. |
package org.chromium.javaassertionenabler; |
+ |
+import com.google.common.io.ByteStreams; |
import org.objectweb.asm.ClassReader; |
import org.objectweb.asm.ClassVisitor; |
@@ -12,30 +14,21 @@ |
import java.io.BufferedInputStream; |
import java.io.BufferedOutputStream; |
-import java.io.ByteArrayOutputStream; |
-import java.io.FileInputStream; |
import java.io.FileOutputStream; |
import java.io.IOException; |
-import java.io.InputStream; |
-import java.nio.file.Files; |
-import java.nio.file.Path; |
-import java.nio.file.Paths; |
-import java.nio.file.StandardCopyOption; |
-import java.util.zip.ZipEntry; |
-import java.util.zip.ZipInputStream; |
-import java.util.zip.ZipOutputStream; |
+import java.util.Collections; |
+import java.util.jar.JarEntry; |
+import java.util.jar.JarFile; |
+import java.util.jar.JarOutputStream; |
/** |
* An application that enables Java ASSERT statements by modifying Java bytecode. It takes in a JAR |
* file, modifies bytecode of classes that use ASSERT, and outputs the bytecode to a new JAR file. |
*/ |
class AssertionEnabler { |
- static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled"; |
static final String CLASS_FILE_SUFFIX = ".class"; |
static final String STATIC_INITIALIZER_NAME = "<clinit>"; |
- static final String TEMPORARY_FILE_SUFFIX = ".temp"; |
- |
- static final int BUFFER_SIZE = 16384; |
+ static final String ASSERTION_DISABLED_NAME = "$assertionsDisabled"; |
static class AssertionEnablerVisitor extends ClassVisitor { |
AssertionEnablerVisitor(ClassWriter writer) { |
@@ -75,52 +68,32 @@ |
} |
} |
- static byte[] readAllBytes(InputStream inputStream) throws IOException { |
- ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
- int numRead = 0; |
- byte[] data = new byte[BUFFER_SIZE]; |
- while ((numRead = inputStream.read(data, 0, data.length)) != -1) { |
- buffer.write(data, 0, numRead); |
- } |
+ static void enableAssertionInJar(String inputJarPath, String outputJarPath) { |
+ try (JarOutputStream outputStream = new JarOutputStream( |
+ new BufferedOutputStream(new FileOutputStream(outputJarPath)))) { |
+ JarFile jarFile = new JarFile(inputJarPath); |
+ for (JarEntry entry : Collections.list(jarFile.entries())) { |
+ try (BufferedInputStream inputStream = new BufferedInputStream( |
+ jarFile.getInputStream(entry))) { |
+ byte[] byteCode = ByteStreams.toByteArray(inputStream); |
- return buffer.toByteArray(); |
- } |
- |
- static void enableAssertionInJar(String inputJarPath, String outputJarPath) { |
- String tempJarPath = outputJarPath + TEMPORARY_FILE_SUFFIX; |
- try (ZipInputStream inputStream = new ZipInputStream( |
- new BufferedInputStream(new FileInputStream(inputJarPath))); |
- ZipOutputStream tempStream = new ZipOutputStream( |
- new BufferedOutputStream(new FileOutputStream(tempJarPath)))) { |
- ZipEntry entry = null; |
- |
- while ((entry = inputStream.getNextEntry()) != null) { |
- byte[] byteCode = readAllBytes(inputStream); |
- |
- if (entry.isDirectory() || !entry.getName().endsWith(CLASS_FILE_SUFFIX)) { |
- tempStream.putNextEntry(entry); |
- tempStream.write(byteCode); |
- tempStream.closeEntry(); |
- continue; |
+ if (entry.isDirectory() || !entry.getName().endsWith(CLASS_FILE_SUFFIX)) { |
+ outputStream.putNextEntry(entry); |
+ outputStream.write(byteCode); |
+ outputStream.closeEntry(); |
+ continue; |
+ } |
+ ClassReader reader = new ClassReader(byteCode); |
+ ClassWriter writer = new ClassWriter(reader, 0); |
+ reader.accept(new AssertionEnablerVisitor(writer), 0); |
+ byte[] patchedByteCode = writer.toByteArray(); |
+ outputStream.putNextEntry(new JarEntry(entry.getName())); |
+ outputStream.write(patchedByteCode); |
+ outputStream.closeEntry(); |
} |
- ClassReader reader = new ClassReader(byteCode); |
- ClassWriter writer = new ClassWriter(reader, 0); |
- reader.accept(new AssertionEnablerVisitor(writer), 0); |
- byte[] patchedByteCode = writer.toByteArray(); |
- tempStream.putNextEntry(new ZipEntry(entry.getName())); |
- tempStream.write(patchedByteCode); |
- tempStream.closeEntry(); |
} |
} catch (IOException e) { |
throw new RuntimeException(e); |
- } |
- |
- try { |
- Path src = Paths.get(tempJarPath); |
- Path dest = Paths.get(outputJarPath); |
- Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING); |
- } catch (IOException ioException) { |
- throw new RuntimeException(ioException); |
} |
} |