Skip to content

Use CachingOutputStream when writing archives #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/codehaus/plexus/archiver/util/Streams.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down