skip if entry has been added am: a02a943a3b

Original change: https://android-review.googlesource.com/c/platform/external/jarjar/+/2982311

Change-Id: Ia06021992185f83de2b5145dc4da88338d7c0b29
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/main/com/tonicsystems/jarjar/util/EntryStruct.java b/src/main/com/tonicsystems/jarjar/util/EntryStruct.java
index 9478ac8..36f9b76 100644
--- a/src/main/com/tonicsystems/jarjar/util/EntryStruct.java
+++ b/src/main/com/tonicsystems/jarjar/util/EntryStruct.java
@@ -16,6 +16,9 @@
 
 package com.tonicsystems.jarjar.util;
 
+import java.util.Arrays;
+import java.util.Objects;
+
 public class EntryStruct {
   public byte[] data;
   public String name;
@@ -32,4 +35,25 @@
     }
     return true;
   }
+
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    }
+
+    if (!(other instanceof EntryStruct)) {
+      return false;
+    }
+
+    EntryStruct that  = (EntryStruct) other;
+    return this.name.equals(that.name) &&
+        Arrays.equals(this.data, that.data) &&
+        this.time == that.time;
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(Arrays.hashCode(data), name, time);
+  }
 }
diff --git a/src/main/com/tonicsystems/jarjar/util/StandaloneJarProcessor.java b/src/main/com/tonicsystems/jarjar/util/StandaloneJarProcessor.java
index b91b6f9..2e87104 100644
--- a/src/main/com/tonicsystems/jarjar/util/StandaloneJarProcessor.java
+++ b/src/main/com/tonicsystems/jarjar/util/StandaloneJarProcessor.java
@@ -21,8 +21,8 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.Enumeration;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
@@ -34,7 +34,7 @@
     JarFile in = new JarFile(from);
     final File tmpTo = File.createTempFile("jarjar", ".jar");
     JarOutputStream out = new JarOutputStream(new FileOutputStream(tmpTo));
-    Set<String> entries = new HashSet<>();
+    Map<String, EntryStruct> entries = new HashMap<>();
     try {
       EntryStruct struct = new EntryStruct();
       Enumeration<JarEntry> e = in.entries();
@@ -46,7 +46,8 @@
         IoUtil.pipe(in.getInputStream(entry), baos, buf);
         struct.data = baos.toByteArray();
         if (proc.process(struct)) {
-          if (entries.add(struct.name)) {
+          EntryStruct existEntry = entries.putIfAbsent(struct.name, struct);
+          if (existEntry == null) {
             entry = new JarEntry(struct.name);
             entry.setTime(struct.time);
             entry.setCompressedSize(-1);
@@ -54,7 +55,7 @@
             out.write(struct.data);
           } else if (struct.name.endsWith("/")) {
             // TODO(chrisn): log
-          } else {
+          } else if (!existEntry.equals(struct)) {
             throw new IllegalArgumentException("Duplicate jar entries: " + struct.name);
           }
         }