diff options
author | Zhi Dou <zhidou@google.com> | 2024-02-28 15:15:52 +0000 |
---|---|---|
committer | Zhi Dou <zhidou@google.com> | 2024-02-28 16:47:36 +0000 |
commit | a02a943a3bcb57a1aa7ec12cd5951ac139a595fb (patch) | |
tree | d79fd260420ca7f26993cadb057551a2bdd75fcf | |
parent | f3b6953e5350ff6e56a607e70dd13fd856473d8c (diff) | |
download | jarjar-a02a943a3bcb57a1aa7ec12cd5951ac139a595fb.tar.gz |
skip if entry has been added
Before if an entry name exists, an error will be thrown. This change
will check whether the existing processed entry is the same entry as the
one currently processing. If they are the same, then the code will
continue to process the next entry. Thus this entry will be only added
once. If they are not the same, then the original error will be thrown.
Test: presbumit
Bug: 327404701
Change-Id: I36c6f13c7899a28534de6d56bcbc8896a60cfd47
-rw-r--r-- | src/main/com/tonicsystems/jarjar/util/EntryStruct.java | 24 | ||||
-rw-r--r-- | src/main/com/tonicsystems/jarjar/util/StandaloneJarProcessor.java | 11 |
2 files changed, 30 insertions, 5 deletions
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 @@ public class EntryStruct { } 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.File; 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 @@ public final class StandaloneJarProcessor { 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 @@ public final class StandaloneJarProcessor { 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 @@ public final class StandaloneJarProcessor { 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); } } |