Skip to content

Commit c0ee2bf

Browse files
authored
Use CachingOutputStream when writing archives (#358)
* Use CachingOutputStream when writing archives * Fix stream being close twice The close method should be idemptotent, however, the JDK8 FilterOutputStream#close() method is not...
1 parent cecc6e7 commit c0ee2bf

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

src/main/java/org/codehaus/plexus/archiver/jar/JarToolModularJarArchiver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.apache.commons.compress.parallel.InputStreamSupplier;
4646
import org.apache.commons.io.output.NullPrintStream;
4747
import org.codehaus.plexus.archiver.ArchiverException;
48+
import org.codehaus.plexus.archiver.util.Streams;
4849
import org.codehaus.plexus.archiver.zip.ConcurrentJarCreator;
4950
import org.codehaus.plexus.util.IOUtil;
5051

@@ -165,7 +166,7 @@ private void fixLastModifiedTimeZipEntries() throws IOException {
165166
Path tmpZip = Files.createTempFile(destFile.getParent(), null, null, attributes);
166167
try {
167168
try (ZipFile zipFile = new ZipFile(getDestFile());
168-
ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(tmpZip))) {
169+
ZipOutputStream out = new ZipOutputStream(Streams.fileOutputStream(tmpZip))) {
169170
Enumeration<? extends ZipEntry> entries = zipFile.entries();
170171
while (entries.hasMoreElements()) {
171172
ZipEntry entry = entries.nextElement();

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
25-
import java.nio.file.Files;
2625
import java.util.zip.GZIPOutputStream;
2726

2827
import io.airlift.compress.snappy.SnappyFramedOutputStream;
@@ -127,7 +126,7 @@ protected void execute() throws ArchiverException, IOException {
127126
getLogger().info("Building tar: " + tarFile.getAbsolutePath());
128127

129128
try {
130-
tOut = new TarArchiveOutputStream(compress(compression, Files.newOutputStream(tarFile.toPath())), "UTF8");
129+
tOut = new TarArchiveOutputStream(compress(compression, Streams.fileOutputStream(tarFile)), "UTF8");
131130
if (longFileMode.isTruncateMode()) {
132131
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_TRUNCATE);
133132
} else if (longFileMode.isPosixMode() || longFileMode.isPosixWarnMode()) {

src/main/java/org/codehaus/plexus/archiver/util/Streams.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.io.InputStream;
2727
import java.io.OutputStream;
2828
import java.nio.file.Files;
29+
import java.nio.file.Path;
2930

3031
import org.codehaus.plexus.archiver.ArchiverException;
3132
import org.codehaus.plexus.util.IOUtil;
33+
import org.codehaus.plexus.util.io.CachingOutputStream;
3234

3335
public class Streams {
3436

@@ -60,12 +62,16 @@ public static InputStream fileInputStream(File file, String operation) throws Ar
6062
}
6163

6264
public static OutputStream fileOutputStream(File file) throws IOException {
63-
return Files.newOutputStream(file.toPath());
65+
return new CachingOutputStream(file);
66+
}
67+
68+
public static OutputStream fileOutputStream(Path file) throws IOException {
69+
return new CachingOutputStream(file);
6470
}
6571

6672
public static OutputStream fileOutputStream(File file, String operation) throws ArchiverException {
6773
try {
68-
return Files.newOutputStream(file.toPath());
74+
return new CachingOutputStream(file);
6975
} catch (IOException e) {
7076
throw new ArchiverException(
7177
"Problem creating output file for " + operation + " " + file.getParent() + ", " + e.getMessage());

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

+6-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.nio.ByteBuffer;
2525
import java.nio.charset.Charset;
2626
import java.nio.charset.StandardCharsets;
27-
import java.nio.file.Files;
2827
import java.nio.file.attribute.FileTime;
2928
import java.util.Calendar;
3029
import java.util.Deque;
@@ -550,7 +549,7 @@ protected boolean createEmptyZip(File zipFile) throws ArchiverException {
550549
// Must create it manually.
551550
getLogger().info("Note: creating empty " + archiveType + " archive " + zipFile);
552551

553-
try (OutputStream os = Files.newOutputStream(zipFile.toPath())) {
552+
try (OutputStream os = Streams.fileOutputStream(zipFile.toPath())) {
554553
// Cf. PKZIP specification.
555554
byte[] empty = new byte[22];
556555
empty[0] = 80; // P
@@ -652,8 +651,10 @@ protected void close() throws IOException {
652651
if (zipArchiveOutputStream != null) {
653652
if (zOut != null) {
654653
zOut.writeTo(zipArchiveOutputStream);
654+
} else {
655+
zipArchiveOutputStream.close();
655656
}
656-
zipArchiveOutputStream.close();
657+
zipArchiveOutputStream = null;
657658
}
658659
} catch (IOException ex) {
659660
// If we're in this finally clause because of an
@@ -670,13 +671,9 @@ protected void close() throws IOException {
670671
}
671672

672673
} catch (InterruptedException e) {
673-
IOException ex = new IOException("InterruptedException exception");
674-
ex.initCause(e.getCause());
675-
throw ex;
674+
throw new IOException("InterruptedException exception", e.getCause());
676675
} catch (ExecutionException e) {
677-
IOException ex = new IOException("Execution exception");
678-
ex.initCause(e.getCause());
679-
throw ex;
676+
throw new IOException("Execution exception", e.getCause());
680677
}
681678
}
682679

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.nio.file.Path;
2727

2828
import org.apache.commons.io.output.ThresholdingOutputStream;
29+
import org.codehaus.plexus.archiver.util.Streams;
2930

3031
/**
3132
* Offloads to disk when a given memory consumption has been reached
@@ -35,7 +36,7 @@ class OffloadingOutputStream extends ThresholdingOutputStream {
3536
// ----------------------------------------------------------- Data members
3637

3738
/**
38-
* The output stream to which data will be written prior to the theshold
39+
* The output stream to which data will be written prior to the threshold
3940
* being reached.
4041
*/
4142
private ByteArrayOutputStream memoryOutputStream;
@@ -111,7 +112,7 @@ protected OutputStream getStream() throws IOException {
111112
@Override
112113
protected void thresholdReached() throws IOException {
113114
outputPath = Files.createTempFile(prefix, suffix);
114-
currentOutputStream = Files.newOutputStream(outputPath);
115+
currentOutputStream = Streams.fileOutputStream(outputPath);
115116
}
116117

117118
public InputStream getInputStream() throws IOException {

0 commit comments

Comments
 (0)