Skip to content

Commit 1ff67b2

Browse files
committed
Optimize buffer reuse and improve NIO usage in ZIP archiver
- Refine ByteArrayOutputStream.reset() to safely handle empty buffers. - Use Path and Files API in DeferredScatterOutputStream and OffloadingOutputStream for better performance and reliability. - Expose Path in OffloadingOutputStream to avoid unnecessary File object conversions.
1 parent ae468f2 commit 1ff67b2

3 files changed

Lines changed: 9 additions & 4 deletions

File tree

src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public synchronized void reset() {
262262
} else {
263263
// Throw away old buffers
264264
currentBuffer = null;
265-
int size = buffers.get(0).length;
265+
int size = buffers.isEmpty() ? 1024 : buffers.get(0).length;
266266
buffers.clear();
267267
needNewBuffer(size);
268268
reuseBuffers = true;

src/main/java/org/codehaus/plexus/archiver/zip/DeferredScatterOutputStream.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
*/
1818
package org.codehaus.plexus.archiver.zip;
1919

20-
import java.io.File;
2120
import java.io.IOException;
2221
import java.io.InputStream;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
2324

2425
import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
2526

@@ -48,9 +49,9 @@ public void closeForWriting() throws IOException {
4849

4950
@Override
5051
public void close() throws IOException {
51-
File file = dfos.getFile();
52+
Path file = dfos.getOutputPath();
5253
if (file != null) {
53-
file.delete();
54+
Files.deleteIfExists(file);
5455
}
5556
}
5657
}

src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ public byte[] getData() {
142142
return null;
143143
}
144144

145+
public Path getOutputPath() {
146+
return outputPath;
147+
}
148+
145149
/**
146150
* Returns either the output file specified in the constructor or
147151
* the temporary file created or null.

0 commit comments

Comments
 (0)