diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java b/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java index 7a6bc952..2b767534 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/ByteArrayOutputStream.java @@ -262,7 +262,7 @@ public synchronized void reset() { } else { // Throw away old buffers currentBuffer = null; - int size = buffers.get(0).length; + int size = buffers.isEmpty() ? 1024 : buffers.get(0).length; buffers.clear(); needNewBuffer(size); reuseBuffers = true; diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java index 4b2e7d74..4b39c361 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java @@ -23,6 +23,7 @@ import java.io.SequenceInputStream; import java.io.UncheckedIOException; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.zip.Deflater; import java.util.zip.ZipEntry; @@ -55,6 +56,8 @@ public class ConcurrentJarCreator { private final ParallelScatterZipCreator parallelScatterZipCreator; + private final ExecutorService es; + private long zipCloseElapsed; private static class DeferredSupplier implements ScatterGatherBackingStoreSupplier { @@ -118,8 +121,8 @@ public ConcurrentJarCreator(boolean compressAddedZips, int nThreads) throws IOEx manifest = createDeferred(defaultSupplier); directories = createDeferred(defaultSupplier); synchronousEntries = createDeferred(defaultSupplier); - parallelScatterZipCreator = - new ParallelScatterZipCreator(Executors.newFixedThreadPool(nThreads), defaultSupplier); + es = Executors.newFixedThreadPool(nThreads); + parallelScatterZipCreator = new ParallelScatterZipCreator(es, defaultSupplier); } /** @@ -161,11 +164,15 @@ public void addArchiveEntry( public void writeTo(ZipArchiveOutputStream targetStream) throws IOException, ExecutionException, InterruptedException { - metaInfDir.writeTo(targetStream); - manifest.writeTo(targetStream); - directories.writeTo(targetStream); - synchronousEntries.writeTo(targetStream); - parallelScatterZipCreator.writeTo(targetStream); + try { + metaInfDir.writeTo(targetStream); + manifest.writeTo(targetStream); + directories.writeTo(targetStream); + synchronousEntries.writeTo(targetStream); + parallelScatterZipCreator.writeTo(targetStream); + } finally { + es.shutdown(); + } long startAt = System.currentTimeMillis(); targetStream.close(); zipCloseElapsed = System.currentTimeMillis() - startAt; diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/DeferredScatterOutputStream.java b/src/main/java/org/codehaus/plexus/archiver/zip/DeferredScatterOutputStream.java index dda5eb2f..21d8758f 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/DeferredScatterOutputStream.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/DeferredScatterOutputStream.java @@ -17,9 +17,10 @@ */ package org.codehaus.plexus.archiver.zip; -import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import org.apache.commons.compress.parallel.ScatterGatherBackingStore; @@ -48,9 +49,9 @@ public void closeForWriting() throws IOException { @Override public void close() throws IOException { - File file = dfos.getFile(); + Path file = dfos.getOutputPath(); if (file != null) { - file.delete(); + Files.deleteIfExists(file); } } } diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java b/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java index a71d3053..13f3a4c0 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java @@ -113,7 +113,7 @@ protected OutputStream getStream() throws IOException { @Override protected void thresholdReached() throws IOException { outputPath = Files.createTempFile(prefix, suffix); - currentOutputStream = Streams.fileOutputStream(outputPath); + currentOutputStream = Streams.bufferedOutputStream(Files.newOutputStream(outputPath)); } public InputStream getInputStream() throws IOException { @@ -142,6 +142,10 @@ public byte[] getData() { return null; } + public Path getOutputPath() { + return outputPath; + } + /** * Returns either the output file specified in the constructor or * the temporary file created or null.