diff --git a/src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java b/src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java index 93c95a51c..078efa16c 100644 --- a/src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java @@ -45,6 +45,7 @@ import org.apache.commons.compress.parallel.InputStreamSupplier; import org.apache.commons.io.output.NullPrintStream; import org.codehaus.plexus.archiver.ArchiverException; +import org.codehaus.plexus.archiver.util.Streams; import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator; import org.codehaus.plexus.util.IOUtil; @@ -165,7 +166,7 @@ private void fixLastModifiedTimeZipEntries() throws IOException { Path tmpZip = Files.createTempFile(destFile.getParent(), null, null, attributes); try { try (ZipFile zipFile = new ZipFile(getDestFile()); - ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) { + ZipOutputStream out = new ZipOutputStream(Streams.fileOutputStream(tmpZip))) { Enumeration entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); diff --git a/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java b/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java index ba53a1b90..6c3eb1160 100644 --- a/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java @@ -22,7 +22,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.file.Files; import java.util.zip.GZIPOutputStream; import io.airlift.compress.snappy.SnappyFramedOutputStream; @@ -127,7 +126,7 @@ protected void execute() throws ArchiverException, IOException { getLogger().info("Building tar: " + tarFile.getAbsolutePath()); try { - tOut = new TarArchiveOutputStream(compress(compression, Files.newOutputStream(tarFile.toPath())), "UTF8"); + tOut = new TarArchiveOutputStream(compress(compression, Streams.fileOutputStream(tarFile)), "UTF8"); if (longFileMode.isTruncateMode()) { tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE); } else if (longFileMode.isPosixMode() || longFileMode.isPosixWarnMode()) { diff --git a/src/main/java/org/codehaus/plexus/archiver/util/Streams.java b/src/main/java/org/codehaus/plexus/archiver/util/Streams.java index dbdd71f77..1e8f84bde 100644 --- a/src/main/java/org/codehaus/plexus/archiver/util/Streams.java +++ b/src/main/java/org/codehaus/plexus/archiver/util/Streams.java @@ -26,9 +26,11 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; +import java.nio.file.Path; import org.codehaus.plexus.archiver.ArchiverException; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.io.CachingOutputStream; public class Streams { @@ -60,12 +62,16 @@ public static InputStream fileInputStream(File file, String operation) throws Ar } public static OutputStream fileOutputStream(File file) throws IOException { - return Files.newOutputStream(file.toPath()); + return new CachingOutputStream(file); + } + + public static OutputStream fileOutputStream(Path file) throws IOException { + return new CachingOutputStream(file); } public static OutputStream fileOutputStream(File file, String operation) throws ArchiverException { try { - return Files.newOutputStream(file.toPath()); + return new CachingOutputStream(file); } catch (IOException e) { throw new ArchiverException( "Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage()); diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java index 44e9f2ebb..0b4bdb5e7 100755 --- a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java @@ -24,7 +24,6 @@ import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.attribute.FileTime; import java.util.Calendar; import java.util.Deque; @@ -550,7 +549,7 @@ protected boolean createEmptyZip(File zipFile) throws ArchiverException { // Must create it manually. getLogger().info("Note: creating empty " + archiveType + " archive " + zipFile); - try (OutputStream os = Files.newOutputStream(zipFile.toPath())) { + try (OutputStream os = Streams.fileOutputStream(zipFile.toPath())) { // Cf. PKZIP specification. byte[] empty = new byte[22]; empty[0] = 80; // P @@ -652,8 +651,10 @@ protected void close() throws IOException { if (zipArchiveOutputStream != null) { if (zOut != null) { zOut.writeTo(zipArchiveOutputStream); + } else { + zipArchiveOutputStream.close(); } - zipArchiveOutputStream.close(); + zipArchiveOutputStream = null; } } catch (IOException ex) { // If we're in this finally clause because of an @@ -670,13 +671,9 @@ protected void close() throws IOException { } } catch (InterruptedException e) { - IOException ex = new IOException("InterruptedException exception"); - ex.initCause(e.getCause()); - throw ex; + throw new IOException("InterruptedException exception", e.getCause()); } catch (ExecutionException e) { - IOException ex = new IOException("Execution exception"); - ex.initCause(e.getCause()); - throw ex; + throw new IOException("Execution exception", e.getCause()); } } 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 4fc0abffa..115add4f4 100644 --- a/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/OffloadingOutputStream.java @@ -26,6 +26,7 @@ import java.nio.file.Path; import org.apache.commons.io.output.ThresholdingOutputStream; +import org.codehaus.plexus.archiver.util.Streams; /** * Offloads to disk when a given memory consumption has been reached @@ -35,7 +36,7 @@ class OffloadingOutputStream extends ThresholdingOutputStream { // ----------------------------------------------------------- Data members /** - * The output stream to which data will be written prior to the theshold + * The output stream to which data will be written prior to the threshold * being reached. */ private ByteArrayOutputStream memoryOutputStream; @@ -111,7 +112,7 @@ protected OutputStream getStream() throws IOException { @Override protected void thresholdReached() throws IOException { outputPath = Files.createTempFile(prefix, suffix); - currentOutputStream = Files.newOutputStream(outputPath); + currentOutputStream = Streams.fileOutputStream(outputPath); } public InputStream getInputStream() throws IOException {