You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2025/03/25 12:02:02 UTC
(commons-compress) branch master updated: [COMPRESS-695] Ability to use different InputStreams for Zstd
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new 05d44db93 [COMPRESS-695] Ability to use different InputStreams for Zstd
05d44db93 is described below
commit 05d44db93ec5dd7c19ebde884f2f58cb0aa62a16
Author: Gary D. Gregory <ga...@gmail.com>
AuthorDate: Tue Mar 25 08:01:36 2025 -0400
[COMPRESS-695] Ability to use different InputStreams for Zstd
Delay Zstd class resolution
---
.../commons/compress/archivers/zip/ZipFile.java | 28 ++++++++++------------
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
index 8311ded03..54bbaa085 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
@@ -93,8 +93,6 @@
*/
public class ZipFile implements Closeable {
- private static final IOFunction<InputStream, InputStream> DEFAULT_ZSTD_INPUT_STREAM_FACTORY = ZstdCompressorInputStream::new;
-
/**
* Lock-free implementation of BoundedInputStream. The implementation uses positioned reads on the underlying archive file channel and therefore performs
* significantly faster in concurrent environment.
@@ -136,12 +134,11 @@ protected int read(final long pos, final ByteBuffer buf) throws IOException {
public static class Builder extends AbstractStreamBuilder<ZipFile, Builder> {
static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
-
private SeekableByteChannel seekableByteChannel;
private boolean useUnicodeExtraFields = true;
private boolean ignoreLocalFileHeader;
private long maxNumberOfDisks = 1;
- private IOFunction<InputStream, InputStream> zstdInputStream = DEFAULT_ZSTD_INPUT_STREAM_FACTORY;
+ private IOFunction<InputStream, InputStream> zstdInputStreamFactory;
/**
* Constructs a new instance.
@@ -171,7 +168,8 @@ public ZipFile get() throws IOException {
actualDescription = path.toString();
}
final boolean closeOnError = seekableByteChannel != null;
- return new ZipFile(actualChannel, actualDescription, getCharset(), useUnicodeExtraFields, closeOnError, ignoreLocalFileHeader, zstdInputStream);
+ return new ZipFile(actualChannel, actualDescription, getCharset(), useUnicodeExtraFields, closeOnError, ignoreLocalFileHeader,
+ zstdInputStreamFactory);
}
/**
@@ -186,7 +184,7 @@ public ZipFile get() throws IOException {
* @since 1.28.0
*/
public Builder setZstdInputStreamFactory(final IOFunction<InputStream, InputStream> zstdInpStreamFactory) {
- this.zstdInputStream = zstdInpStreamFactory == null ? DEFAULT_ZSTD_INPUT_STREAM_FACTORY : zstdInpStreamFactory;
+ this.zstdInputStreamFactory = zstdInpStreamFactory;
return this;
}
@@ -741,7 +739,7 @@ private static boolean tryToLocateSignature(final SeekableByteChannel channel, f
private final ByteBuffer shortBbuf = ByteBuffer.wrap(shortBuf);
- private final IOFunction<InputStream, InputStream> zstdInputStream;
+ private final IOFunction<InputStream, InputStream> zstdInputStreamFactory;
private long centralDirectoryStartDiskNumber;
@@ -920,7 +918,7 @@ private ZipFile(final SeekableByteChannel channel, final String channelDescripti
this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
this.useUnicodeExtraFields = useUnicodeExtraFields;
this.archive = channel;
- this.zstdInputStream = zstdInputStream;
+ this.zstdInputStreamFactory = zstdInputStream;
boolean success = false;
try {
final Map<ZipArchiveEntry, NameAndComment> entriesWithoutUTF8Flag = populateFromCentralDirectory();
@@ -989,8 +987,7 @@ public ZipFile(final SeekableByteChannel channel, final String channelDescriptio
private ZipFile(final SeekableByteChannel channel, final String channelDescription, final String encoding, final boolean useUnicodeExtraFields,
final boolean closeOnError, final boolean ignoreLocalFileHeader) throws IOException {
- this(channel, channelDescription, Charsets.toCharset(encoding), useUnicodeExtraFields, closeOnError, ignoreLocalFileHeader,
- DEFAULT_ZSTD_INPUT_STREAM_FACTORY);
+ this(channel, channelDescription, Charsets.toCharset(encoding), useUnicodeExtraFields, closeOnError, ignoreLocalFileHeader, null);
}
/**
@@ -1279,15 +1276,16 @@ public void close() throws IOException {
}
/**
- * Creates the appropriate InputStream for the ZSTD compression method.
+ * Creates an InputStream for the Zstd compression method.
*
- * @param is the input stream which should be used for compression.
+ * @param in the input stream which should be used for compression.
* @return the {@link InputStream} for handling the Zstd compression.
* @throws IOException if an I/O error occurs.
- * @since 1.28.0
*/
- protected InputStream createZstdInputStream(final InputStream is) throws IOException {
- return zstdInputStream.apply(is);
+ @SuppressWarnings("resource")
+ InputStream createZstdInputStream(final InputStream in) throws IOException {
+ // This method is the only location that references ZstdCompressorInputStream directly to avoid requiring the JAR for all use cases.
+ return zstdInputStreamFactory != null ? zstdInputStreamFactory.apply(in) : new ZstdCompressorInputStream(in);
}
/**