Skip to content

Commit

Permalink
use SequenceInputStream to concatenate...
Browse files Browse the repository at this point in the history
resources to avoid intermediate Strings

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Oct 13, 2024
1 parent c9f4f98 commit 20a2822
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions biz.aQute.bndlib/src/aQute/bnd/osgi/Jar.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.file.FileVisitOption;
Expand Down Expand Up @@ -390,19 +391,21 @@ public boolean putResource(String path, Resource resource, DupStrategy strategy)
directories.put(dir, null);
}
}
boolean duplicate = s.containsKey(path);

Resource existing = s.get(path);
boolean duplicate = existing != null;
if (!duplicate || DupStrategy.OVERWRITE == strategy) {
resources.put(path, resource);
s.put(path, resource);
updateModified(resource.lastModified(), path);
}
else if (duplicate && DupStrategy.APPEND == strategy) {
try {
try (SequenceInputStream in = new SequenceInputStream(existing.openInputStream(),
resource.openInputStream());) {

String current = IO.collect(getResource(path).openInputStream());
String toAppend = IO.collect(resource.openInputStream());
String appended = current + toAppend;
Resource r = new EmbeddedResource(appended, 0L);
long lastModified = Math.max(existing.lastModified(), resource.lastModified());
Resource r = new EmbeddedResource(ByteBuffer.wrap(in.readAllBytes()),
lastModified);
// addExtra(r, extra.get("extra"));
resources.put(path, r);
s.put(path, r);
Expand Down

0 comments on commit 20a2822

Please sign in to comment.