You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by pk...@apache.org on 2025/09/12 20:44:32 UTC
(commons-compress) branch feat/archive-builders created (now e5e9dd8dc)
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a change to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
at e5e9dd8dc docs: add changelog entry
This branch includes the following new commits:
new 5e60d5660 feat: add generic builder for `ArchiveInputStream`
new 7dc70dd2d feat: add builder for `ArArchiveInputStream`
new c4140ba3e feat: add builder for `ArjArchiveInputStream`
new d4bcf0e77 feat: add builder for `CpioArchiveInputStream`
new 712833837 feat: add builder for `DumpArchiveInputStream`
new 4aa829245 feat: add builder for `JarArchiveInputStream` and `ZipArchiveInputStream`
new 95bd6cecb feat: use builder instead of TarArchiveInputStream constructors
new e5e9dd8dc docs: add changelog entry
The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(commons-compress) 06/08: feat: add builder for `JarArchiveInputStream` and `ZipArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 4aa829245a420ee5f23b808590ead9d76a136d35
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 20:18:43 2025 +0200
feat: add builder for `JarArchiveInputStream` and `ZipArchiveInputStream`
Introduce a builder for `JarArchiveInputStream` and `ZipArchiveInputStream` and refactor existing
constructor calls to use it.
---
.../compress/archivers/ArchiveStreamFactory.java | 11 +-
.../archivers/jar/JarArchiveInputStream.java | 35 ++++
.../archivers/zip/ZipArchiveInputStream.java | 173 ++++++++++++++++++--
.../apache/commons/compress/archivers/ZipTest.java | 14 +-
...fd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java | 12 +-
.../archivers/zip/EncryptedArchiveTest.java | 11 +-
.../compress/archivers/zip/ExplodeSupportTest.java | 4 +-
.../commons/compress/archivers/zip/Lister.java | 19 ++-
.../archivers/zip/Maven221MultiVolumeTest.java | 7 +-
.../compress/archivers/zip/UTF8ZipFilesTest.java | 34 ++--
.../compress/archivers/zip/Zip64SupportIT.java | 8 +-
.../zip/ZipArchiveInputStreamMalformedTest.java | 7 +-
.../archivers/zip/ZipArchiveInputStreamTest.java | 182 +++++++++++++--------
13 files changed, 375 insertions(+), 142 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index d80a2766f..7dd47f7a7 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -440,10 +440,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) arjBuilder.get();
}
if (ZIP.equalsIgnoreCase(archiverName)) {
+ final ZipArchiveInputStream.Builder zipBuilder = ZipArchiveInputStream.builder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new ZipArchiveInputStream(in, actualEncoding);
+ zipBuilder.setCharset(actualEncoding);
}
- return (I) new ZipArchiveInputStream(in);
+ return (I) zipBuilder.get();
}
if (TAR.equalsIgnoreCase(archiverName)) {
if (actualEncoding != null) {
@@ -452,10 +453,12 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) new TarArchiveInputStream(in);
}
if (JAR.equalsIgnoreCase(archiverName) || APK.equalsIgnoreCase(archiverName)) {
+ final JarArchiveInputStream.Builder jarBuilder =
+ JarArchiveInputStream.jarInputStreamBuilder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new JarArchiveInputStream(in, actualEncoding);
+ jarBuilder.setCharset(actualEncoding);
}
- return (I) new JarArchiveInputStream(in);
+ return (I) jarBuilder.get();
}
if (CPIO.equalsIgnoreCase(archiverName)) {
final CpioArchiveInputStream.Builder cpioBuilder = CpioArchiveInputStream.builder().setInputStream(in);
diff --git a/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
index f8d9d180e..14ba94c01 100644
--- a/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/jar/JarArchiveInputStream.java
@@ -31,6 +31,27 @@
*/
public class JarArchiveInputStream extends ZipArchiveInputStream {
+ /**
+ * Builds a new {@link JarArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * JarArchiveInputStream in = JarArchiveInputStream.builder()
+ * .setPath(inputPath)
+ * .setCharset(StandardCharsets.UTF_8)
+ * .setUseUnicodeExtraFields(false)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static class Builder extends ZipArchiveInputStream.AbstractBuilder<JarArchiveInputStream, Builder> {
+ @Override
+ public JarArchiveInputStream get() throws IOException {
+ return new JarArchiveInputStream(this);
+ }
+ }
+
/**
* Checks if the signature matches what is expected for a jar file (in this case it is the same as for a ZIP file).
*
@@ -42,6 +63,16 @@ public static boolean matches(final byte[] signature, final int length) {
return ZipArchiveInputStream.matches(signature, length);
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder jarInputStreamBuilder() {
+ return new Builder();
+ }
+
/**
* Creates an instance from the input stream using the default encoding.
*
@@ -62,6 +93,10 @@ public JarArchiveInputStream(final InputStream inputStream, final String encodin
super(inputStream, encoding);
}
+ private JarArchiveInputStream(final Builder builder) throws IOException {
+ super(builder);
+ }
+
@Override
public JarArchiveEntry getNextEntry() throws IOException {
return getNextJarEntry();
diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
index ea7544772..e5b6bd8b0 100644
--- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
@@ -77,6 +77,112 @@
*/
public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> implements InputStreamStatistics {
+ /**
+ * Abstract builder for derived classes of {@link ZipArchiveInputStream}.
+ *
+ * @param <T> The type of the {@link ZipArchiveInputStream}.
+ * @param <B> The type of the builder itself.
+ * @since 1.29.0
+ */
+ public abstract static class AbstractBuilder<T extends ZipArchiveInputStream, B extends AbstractBuilder<T, B>>
+ extends ArchiveInputStream.Builder<T, B> {
+ private boolean useUnicodeExtraFields = true;
+ private boolean allowStoredEntriesWithDataDescriptor;
+ private boolean skipSplitSig;
+
+ protected AbstractBuilder() {
+ setCharset(StandardCharsets.UTF_8);
+ }
+
+ /**
+ * Controls whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.
+ *
+ * <p>This feature is enabled by default.</p>
+ *
+ * @param useUnicodeExtraFields If {@code true} Unicode Extra Fields should be used.
+ * @return this
+ */
+ public B setUseUnicodeExtraFields(final boolean useUnicodeExtraFields) {
+ this.useUnicodeExtraFields = useUnicodeExtraFields;
+ return asThis();
+ }
+
+ protected boolean isUseUnicodeExtraFields() {
+ return useUnicodeExtraFields;
+ }
+
+ /**
+ * Controls whether the stream attempts to read STORED entries that use a data descriptor.
+ *
+ * <p>If set to {@code true}, the stream will not stop reading an entry at the
+ * declared compressed size. Instead, it will continue until a data descriptor
+ * is encountered (by detecting the Data Descriptor Signature). This may cause
+ * issues in certain cases, such as JARs embedded in WAR files.</p>
+ *
+ * <p>See <a href="https://issues.apache.org/jira/browse/COMPRESS-555">COMPRESS-555</a>
+ * for details.</p>
+ *
+ * <p>This feature is disabled by default.</p>
+ *
+ * @param allowStoredEntriesWithDataDescriptor {@code true} to read STORED entries with data descriptors,
+ * {@code false} to stop at the compressed size.
+ * @return this
+ */
+
+ public B setAllowStoredEntriesWithDataDescriptor(final boolean allowStoredEntriesWithDataDescriptor) {
+ this.allowStoredEntriesWithDataDescriptor = allowStoredEntriesWithDataDescriptor;
+ return asThis();
+ }
+
+ protected boolean isAllowStoredEntriesWithDataDescriptor() {
+ return allowStoredEntriesWithDataDescriptor;
+ }
+
+ /**
+ * Configures whether the stream should skip the ZIP split signature
+ * ({@code 08074B50}) at the beginning of the input.
+ *
+ * <p>Disabled by default.</p>
+ *
+ * @param skipSplitSig {@code true} to skip the ZIP split signature, {@code false} otherwise
+ * @return this
+ */
+ public B setSkipSplitSig(final boolean skipSplitSig) {
+ this.skipSplitSig = skipSplitSig;
+ return asThis();
+ }
+
+ protected boolean isSkipSplitSig() {
+ return skipSplitSig;
+ }
+ }
+
+ /**
+ * Builds a new {@link ZipArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * ZipArchiveInputStream in = ZipArchiveInputStream.builder()
+ * .setPath(inputPath)
+ * .setCharset(StandardCharsets.UTF_8)
+ * .setUseUnicodeExtraFields(false)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static final class Builder extends AbstractBuilder<ZipArchiveInputStream, Builder> {
+
+ private Builder() {
+ // empty
+ }
+
+ @Override
+ public ZipArchiveInputStream get() throws IOException {
+ return new ZipArchiveInputStream(this);
+ }
+ }
+
/**
* Input stream adapted from commons-io.
*/
@@ -220,6 +326,16 @@ public static boolean matches(final byte[] buffer, final int length) {
|| ArrayUtils.startsWith(buffer, ZipLong.SINGLE_SEGMENT_SPLIT_MARKER.getBytes());
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
/** The ZIP encoding to use for file names and the file comment. */
private final ZipEncoding zipEncoding;
/** Whether to look for and use Unicode extra fields. */
@@ -270,7 +386,7 @@ public static boolean matches(final byte[] buffer, final int length) {
* @param inputStream the stream to wrap
*/
public ZipArchiveInputStream(final InputStream inputStream) {
- this(inputStream, StandardCharsets.UTF_8.name());
+ this(inputStream, builder());
}
/**
@@ -281,7 +397,7 @@ public ZipArchiveInputStream(final InputStream inputStream) {
* @since 1.5
*/
public ZipArchiveInputStream(final InputStream inputStream, final String encoding) {
- this(inputStream, encoding, true);
+ this(inputStream, builder().setCharset(encoding));
}
/**
@@ -291,8 +407,9 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin
* @param encoding the encoding to use for file names, use null for the platform's default encoding
* @param useUnicodeExtraFields whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.
*/
- public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields) {
- this(inputStream, encoding, useUnicodeExtraFields, false);
+ public ZipArchiveInputStream(
+ final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields) {
+ this(inputStream, builder().setCharset(encoding).setUseUnicodeExtraFields(useUnicodeExtraFields));
}
/**
@@ -304,9 +421,17 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin
* @param allowStoredEntriesWithDataDescriptor whether the stream will try to read STORED entries that use a data descriptor
* @since 1.1
*/
- public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields,
+ public ZipArchiveInputStream(
+ final InputStream inputStream,
+ final String encoding,
+ final boolean useUnicodeExtraFields,
final boolean allowStoredEntriesWithDataDescriptor) {
- this(inputStream, encoding, useUnicodeExtraFields, allowStoredEntriesWithDataDescriptor, false);
+ this(
+ inputStream,
+ builder()
+ .setCharset(encoding)
+ .setUseUnicodeExtraFields(useUnicodeExtraFields)
+ .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor));
}
/**
@@ -320,14 +445,32 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin
* this to true if you want to read a split archive.
* @since 1.20
*/
- public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields,
- final boolean allowStoredEntriesWithDataDescriptor, final boolean skipSplitSig) {
- super(inputStream, encoding);
+ public ZipArchiveInputStream(
+ final InputStream inputStream,
+ final String encoding,
+ final boolean useUnicodeExtraFields,
+ final boolean allowStoredEntriesWithDataDescriptor,
+ final boolean skipSplitSig) {
+ this(
+ inputStream,
+ builder()
+ .setCharset(encoding)
+ .setUseUnicodeExtraFields(useUnicodeExtraFields)
+ .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor)
+ .setSkipSplitSig(skipSplitSig));
+ }
+
+ protected ZipArchiveInputStream(final AbstractBuilder<?, ?> builder) throws IOException {
+ this(builder.getInputStream(), builder);
+ }
+
+ private ZipArchiveInputStream(final InputStream inputStream, final AbstractBuilder<?, ?> builder) {
+ super(inputStream, builder.getCharset());
this.in = new PushbackInputStream(inputStream, buf.capacity());
- this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
- this.useUnicodeExtraFields = useUnicodeExtraFields;
- this.allowStoredEntriesWithDataDescriptor = allowStoredEntriesWithDataDescriptor;
- this.skipSplitSig = skipSplitSig;
+ this.zipEncoding = ZipEncodingHelper.getZipEncoding(builder.getCharset());
+ this.useUnicodeExtraFields = builder.isUseUnicodeExtraFields();
+ this.allowStoredEntriesWithDataDescriptor = builder.isAllowStoredEntriesWithDataDescriptor();
+ this.skipSplitSig = builder.isSkipSplitSig();
// haven't read anything so far
buf.limit(0);
}
@@ -339,7 +482,9 @@ public ZipArchiveInputStream(final InputStream inputStream, final String encodin
* If it contains such a signature, reads the data descriptor and positions the stream right after the data descriptor.
* </p>
*/
- private boolean bufferContainsSignature(final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen) throws IOException {
+ private boolean bufferContainsSignature(
+ final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen)
+ throws IOException {
boolean done = false;
for (int i = 0; !done && i < offset + lastRead - 4; i++) {
if (buf.array()[i] == LFH[0] && buf.array()[i + 1] == LFH[1]) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
index 9e54b5452..df2071d66 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java
@@ -18,7 +18,6 @@
*/
package org.apache.commons.compress.archivers;
-import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -353,7 +352,10 @@ void testBuildSplitZipTest() throws IOException {
final File lastFile = newTempFile("splitZip.zip");
try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
InputStream inputStream = Channels.newInputStream(channel);
- ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, UTF_8.toString(), true, false, true)) {
+ ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .setSkipSplitSig(true)
+ .get()) {
ArchiveEntry entry;
final int filesNum = countNonDirectories(directoryToZip);
@@ -653,7 +655,7 @@ void testListAllFilesWithNestedArchive() throws Exception {
*/
@Test
void testSkipEntryWithUnsupportedCompressionMethod() throws IOException {
- try (ZipArchiveInputStream zip = new ZipArchiveInputStream(newInputStream("moby.zip"))) {
+ try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder().setURI(getURI("moby.zip")).get()) {
final ZipArchiveEntry entry = zip.getNextZipEntry();
assertEquals(ZipMethod.TOKENIZATION.getCode(), entry.getMethod(), "method");
assertEquals("README", entry.getName());
@@ -667,12 +669,12 @@ void testSkipEntryWithUnsupportedCompressionMethod() throws IOException {
*/
@Test
void testSkipsPK00Prefix() throws Exception {
- final File input = getFile("COMPRESS-208.zip");
final ArrayList<String> al = new ArrayList<>();
al.add("test1.xml");
al.add("test2.xml");
- try (InputStream fis = Files.newInputStream(input.toPath());
- ZipArchiveInputStream inputStream = new ZipArchiveInputStream(fis)) {
+ try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-208.zip"))
+ .get()) {
checkArchiveContent(inputStream, al);
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java b/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java
index b2faabd64..f96f0f1ba 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test.java
@@ -20,25 +20,23 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.ByteArrayInputStream;
-import java.io.File;
import java.io.IOException;
+import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
/**
* Tests https://issues.apache.org/jira/browse/COMPRESS-598
*/
-class Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test {
+class Crash_f2efd9eaeb86cda597d07b5e3c3d81363633c2da_Test extends AbstractTest {
@Test
void test() throws IOException {
- final byte[] input = FileUtils
- .readFileToByteArray(new File("src/test/resources/org/apache/commons/compress/fuzz/crash-f2efd9eaeb86cda597d07b5e3c3d81363633c2da"));
- try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new ByteArrayInputStream(input))) {
+ try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/fuzz/crash-f2efd9eaeb86cda597d07b5e3c3d81363633c2da"))
+ .get()) {
assertThrows(IOException.class, () -> {
for (;;) {
final ArchiveEntry zipEntry = zis.getNextEntry();
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
index 888cefdad..9fb7b9aad 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
@@ -19,16 +19,14 @@
package org.apache.commons.compress.archivers.zip;
-import static org.apache.commons.compress.AbstractTest.getFile;
+import static org.apache.commons.compress.AbstractTest.getURI;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.File;
import java.io.IOException;
-import java.nio.file.Files;
import org.junit.jupiter.api.Test;
@@ -36,8 +34,9 @@ class EncryptedArchiveTest {
@Test
void testReadPasswordEncryptedEntryViaStream() throws IOException {
- final File file = getFile("password-encrypted.zip");
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder()
+ .setURI(getURI("password-encrypted.zip"))
+ .get()) {
final ZipArchiveEntry zae = zin.getNextZipEntry();
assertEquals("LICENSE.txt", zae.getName());
assertTrue(zae.getGeneralPurposeBit().usesEncryption());
@@ -53,7 +52,7 @@ void testReadPasswordEncryptedEntryViaStream() throws IOException {
@Test
void testReadPasswordEncryptedEntryViaZipFile() throws IOException {
- try (ZipFile zf = ZipFile.builder().setFile(getFile("password-encrypted.zip")).get()) {
+ try (ZipFile zf = ZipFile.builder().setURI(getURI("password-encrypted.zip")).get()) {
final ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
assertTrue(zae.getGeneralPurposeBit().usesEncryption());
assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java
index 60451e946..ff9ea106a 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ExplodeSupportTest.java
@@ -25,10 +25,8 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
-import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.file.Files;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
@@ -85,7 +83,7 @@ void testTikaTestStream() throws IOException {
}
private void testZipStreamWithImplodeCompression(final String fileName, final String entryName) throws IOException {
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(new File(fileName).toPath()))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(fileName).get()) {
final ZipArchiveEntry entry = zin.getNextZipEntry();
assertEquals(entryName, entry.getName(), "entry name");
assertTrue(zin.canReadEntryData(entry), "entry can't be read");
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java
index 07e2f90f9..e3ead9ae4 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java
@@ -19,7 +19,6 @@
package org.apache.commons.compress.archivers.zip;
-import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -70,14 +69,16 @@ public static void main(final String[] args) throws IOException {
usage();
}
if (cl.useStream) {
- try (BufferedInputStream fs = new BufferedInputStream(Files.newInputStream(f.toPath()))) {
- final ZipArchiveInputStream zs = new ZipArchiveInputStream(fs, cl.encoding, true, cl.allowStoredEntriesWithDataDescriptor);
- for (ArchiveEntry entry = zs.getNextEntry(); entry != null; entry = zs.getNextEntry()) {
- final ZipArchiveEntry ze = (ZipArchiveEntry) entry;
- list(ze);
- if (cl.dir != null) {
- extract(cl.dir, ze, zs);
- }
+ final ZipArchiveInputStream zs = ZipArchiveInputStream.builder()
+ .setFile(f)
+ .setCharset(cl.encoding)
+ .setAllowStoredEntriesWithDataDescriptor(cl.allowStoredEntriesWithDataDescriptor)
+ .get();
+ for (ArchiveEntry entry = zs.getNextEntry(); entry != null; entry = zs.getNextEntry()) {
+ final ZipArchiveEntry ze = (ZipArchiveEntry) entry;
+ list(ze);
+ if (cl.dir != null) {
+ extract(cl.dir, ze, zs);
}
}
} else {
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
index 48b37731b..fa9dd532c 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/Maven221MultiVolumeTest.java
@@ -23,7 +23,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
-import java.io.InputStream;
import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -71,8 +70,10 @@ void testRead7ZipMultiVolumeArchiveForFile() {
@Test
void testRead7ZipMultiVolumeArchiveForStream() throws IOException {
- try (InputStream archive = newInputStream("apache-maven-2.2.1.zip.001");
- ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, null, false)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setURI(getURI("apache-maven-2.2.1.zip.001"))
+ .setUseUnicodeExtraFields(false)
+ .get()) {
// these are the entries that are supposed to be processed correctly without any problems.
for (final String element : ENTRIES) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
index b028a2d29..aceae288b 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
@@ -31,7 +31,6 @@
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
import java.util.zip.CRC32;
import org.apache.commons.compress.AbstractTest;
@@ -178,7 +177,11 @@ private void testFileRoundtrip(final String encoding, final boolean withEFS, fin
@Test
void testRawNameReadFromStream() throws IOException {
- try (ZipArchiveInputStream zi = new ZipArchiveInputStream(newInputStream("utf8-7zip-test.zip"), CP437, false)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setURI(getURI("utf8-7zip-test.zip"))
+ .setCharset(CP437)
+ .setUseUnicodeExtraFields(false)
+ .get()) {
assertRawNameOfAcsiiTxt(zi.getNextEntry());
}
}
@@ -208,7 +211,11 @@ void testRead7ZipArchive() throws IOException {
@Test
void testRead7ZipArchiveForStream() throws IOException {
- try (ZipArchiveInputStream zi = new ZipArchiveInputStream(newInputStream("utf8-7zip-test.zip"), CP437, false)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setURI(getURI("utf8-7zip-test.zip"))
+ .setCharset(CP437)
+ .setUseUnicodeExtraFields(false)
+ .get()) {
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName());
@@ -235,13 +242,10 @@ void testReadWinZipArchive() throws IOException {
@Test
void testReadWinZipArchiveForStream() throws IOException {
- // fix for test fails on Windows with default charset that is not UTF-8
- String encoding = null;
- if (Charset.defaultCharset() != UTF_8) {
- encoding = UTF_8.name();
- }
- try (InputStream archive = newInputStream("utf8-winzip-test.zip");
- ZipArchiveInputStream zi = new ZipArchiveInputStream(archive, encoding, true)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setURI(getURI("utf8-winzip-test.zip"))
+ .setCharset(UTF_8)
+ .get()) {
assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName());
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
@@ -253,8 +257,9 @@ void testReadWinZipArchiveForStream() throws IOException {
*/
@Test
void testStreamSkipsOverUnicodeExtraFieldWithUnsupportedVersion() throws IOException {
- try (InputStream archive = newInputStream("COMPRESS-479.zip");
- ZipArchiveInputStream zi = new ZipArchiveInputStream(archive)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-479.zip"))
+ .get()) {
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals("%U20AC_for_Dollar.txt", zi.getNextEntry().getName());
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
@@ -304,7 +309,10 @@ void testZipArchiveInputStreamReadsUnicodeFields() throws IOException {
void testZipFileReadsUnicodeFields() throws IOException {
final File file = createTempFile("unicode-test", ".zip");
createTestFile(file, StandardCharsets.US_ASCII.name(), false, true);
- try (ZipArchiveInputStream zi = new ZipArchiveInputStream(Files.newInputStream(file.toPath()), StandardCharsets.US_ASCII.name(), true)) {
+ try (ZipArchiveInputStream zi = ZipArchiveInputStream.builder()
+ .setFile(file)
+ .setCharset(StandardCharsets.US_ASCII)
+ .get()) {
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName());
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java b/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
index 43cb2d805..7e070fba3 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java
@@ -152,8 +152,8 @@ private static File getTempFile(final String testName) throws Throwable {
}
private static void read100KFilesImpl(final File f) throws IOException {
- try (InputStream fin = Files.newInputStream(f.toPath());
- ZipArchiveInputStream zin = new ZipArchiveInputStream(fin)) {
+ try (ZipArchiveInputStream zin =
+ ZipArchiveInputStream.builder().setFile(f).get()) {
int files = 0;
ZipArchiveEntry zae;
while ((zae = zin.getNextZipEntry()) != null) {
@@ -182,8 +182,8 @@ private static void read100KFilesUsingZipFileImpl(final File file) throws IOExce
}
private static void read5GBOfZerosImpl(final File f, final String expectedName) throws IOException {
- try (InputStream fin = Files.newInputStream(f.toPath());
- ZipArchiveInputStream zin = new ZipArchiveInputStream(fin)) {
+ try (ZipArchiveInputStream zin =
+ ZipArchiveInputStream.builder().setFile(f).get()) {
ZipArchiveEntry zae = zin.getNextZipEntry();
while (zae.isDirectory()) {
zae = zin.getNextZipEntry();
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java
index 5dec0aef9..233fe852d 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamMalformedTest.java
@@ -21,12 +21,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.apache.commons.compress.CompressException;
@@ -69,7 +67,10 @@ void test() throws IOException {
fos.write(chunk);
}
}
- try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(fixture), StandardCharsets.UTF_8.name(), true, true)) {
+ try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder()
+ .setFile(fixture)
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
zis.getNextEntry();
assertThrows(CompressException.class, () -> IOUtils.toByteArray(zis));
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
index 8fdd0b739..e73cc0304 100644
--- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java
@@ -32,7 +32,6 @@
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -42,7 +41,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.time.Instant;
import java.util.Arrays;
import java.util.zip.ZipEntry;
@@ -90,7 +88,7 @@ public boolean isUsed() {
}
private static void nameSource(final String archive, final String entry, int entryNo, final ZipArchiveEntry.NameSource expected) throws Exception {
- try (ZipArchiveInputStream zis = new ZipArchiveInputStream(Files.newInputStream(getFile(archive).toPath()))) {
+ try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder().setURI(getURI(archive)).get()) {
ZipArchiveEntry ze;
do {
ze = zis.getNextZipEntry();
@@ -116,7 +114,9 @@ private void extractZipInputStream(final ZipArchiveInputStream inputStream) thro
ZipArchiveEntry zae = inputStream.getNextZipEntry();
while (zae != null) {
if (zae.getName().endsWith(".zip")) {
- try (ZipArchiveInputStream innerInputStream = new ZipArchiveInputStream(inputStream)) {
+ try (ZipArchiveInputStream innerInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .get()) {
extractZipInputStream(innerInputStream);
}
}
@@ -181,8 +181,8 @@ private void getAllZipEntries(final ZipArchiveInputStream zipInputStream) throws
private void multiByteReadConsistentlyReturnsMinusOneAtEof(final File file) throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ try (ZipArchiveInputStream archive =
+ ZipArchiveInputStream.builder().setFile(file).get()) {
assertEquals(-1, archive.getCompressedCount());
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
@@ -192,8 +192,8 @@ private void multiByteReadConsistentlyReturnsMinusOneAtEof(final File file) thro
}
private void singleByteReadConsistentlyReturnsMinusOneAtEof(final File file) throws Exception {
- try (InputStream in = Files.newInputStream(file.toPath());
- ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ try (ZipArchiveInputStream archive =
+ ZipArchiveInputStream.builder().setFile(file).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());
@@ -203,8 +203,9 @@ private void singleByteReadConsistentlyReturnsMinusOneAtEof(final File file) thr
@Test
void testDecompressNextSymbol() throws IOException {
- try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/zip/decompressNextSymbol.bin")))) {
+ try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/zip/decompressNextSymbol.bin"))
+ .get()) {
inputStream.getNextEntry();
assertThrows(IOException.class, inputStream::getNextEntry);
}
@@ -212,14 +213,14 @@ void testDecompressNextSymbol() throws IOException {
@Test
void testGetCompressedCountEmptyZip() throws IOException {
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) {
assertEquals(-1, zin.getCompressedCount());
}
}
@Test
void testGetFirstEntryEmptyZip() throws IOException {
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) {
final ZipArchiveEntry entry = zin.getNextEntry();
assertNull(entry);
}
@@ -227,15 +228,16 @@ void testGetFirstEntryEmptyZip() throws IOException {
@Test
void testGetNextZipEntry() throws IOException {
- try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/zip/getNextZipEntry.bin")))) {
+ try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/zip/getNextZipEntry.bin"))
+ .get()) {
assertThrows(IOException.class, () -> inputStream.forEach(IOConsumer.noop()));
}
}
@Test
void testGetUncompressedCountEmptyZip() throws IOException {
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setByteArray(ByteUtils.EMPTY_BYTE_ARRAY).get()) {
assertEquals(0, zin.getUncompressedCount());
}
}
@@ -245,7 +247,7 @@ void testGetUncompressedCountEmptyZip() throws IOException {
*/
@Test
void testMessageWithCorruptFileName() throws Exception {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-351.zip"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-351.zip")).get()) {
final EOFException ex = assertThrows(EOFException.class, () -> in.forEach(IOConsumer.noop()));
final String m = ex.getMessage();
assertTrue(m.startsWith("Truncated ZIP entry: ?2016")); // the first character is not printable
@@ -288,8 +290,8 @@ void testMultiByteReadThrowsAtEofForCorruptedStoredEntry() throws Exception {
// make size much bigger than entry's real size
Arrays.fill(content, 17, 26, (byte) 0xff);
final byte[] buf = new byte[2];
- try (ByteArrayInputStream in = new ByteArrayInputStream(content);
- ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ try (ZipArchiveInputStream archive =
+ ZipArchiveInputStream.builder().setByteArray(content).get()) {
assertNotNull(archive.getNextEntry());
final IOException ex1 = assertThrows(ArchiveException.class, () -> IOUtils.toByteArray(archive), "expected exception");
assertEquals("Truncated ZIP file", ex1.getMessage());
@@ -321,8 +323,8 @@ void testNameSourceIsSetToUnicodeExtraField() throws Exception {
@Test
void testOffsets() throws Exception {
// mixed.zip contains both inflated and stored files
- try (InputStream archiveStream = ZipArchiveInputStream.class.getResourceAsStream("/mixed.zip");
- ZipArchiveInputStream zipStream = new ZipArchiveInputStream(archiveStream)) {
+ try (ZipArchiveInputStream zipStream =
+ ZipArchiveInputStream.builder().setURI(getURI("mixed.zip")).get()) {
final ZipArchiveEntry inflatedEntry = zipStream.getNextZipEntry();
assertEquals("inflated.txt", inflatedEntry.getName());
assertEquals(0x0000, inflatedEntry.getLocalHeaderOffset());
@@ -338,7 +340,9 @@ void testOffsets() throws Exception {
@Test
void testProperlyMarksEntriesAsUnreadableIfUncompressedSizeIsUnknown() throws Exception {
// we never read any data
- try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new ByteArrayInputStream(ByteUtils.EMPTY_BYTE_ARRAY))) {
+ try (ZipArchiveInputStream zis = ZipArchiveInputStream.builder()
+ .setByteArray(ByteUtils.EMPTY_BYTE_ARRAY)
+ .get()) {
final ZipArchiveEntry e = new ZipArchiveEntry("test");
e.setMethod(ZipMethod.DEFLATED.getCode());
assertTrue(zis.canReadEntryData(e));
@@ -351,8 +355,8 @@ void testProperlyMarksEntriesAsUnreadableIfUncompressedSizeIsUnknown() throws Ex
@Test
void testProperlyReadsStoredEntries() throws IOException {
- try (InputStream fs = newInputStream("bla-stored.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs)) {
+ try (ZipArchiveInputStream archive =
+ ZipArchiveInputStream.builder().setURI(getURI("bla-stored.zip")).get()) {
ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -373,8 +377,10 @@ void testProperlyReadsStoredEntries() throws IOException {
@Test
void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOException {
- try (InputStream fs = newInputStream("bla-stored-dd-nosig.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) {
+ try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder()
+ .setURI(getURI("bla-stored-dd-nosig.zip"))
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
final ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -389,8 +395,10 @@ void testProperlyReadsStoredEntryWithDataDescriptorWithoutSignature() throws IOE
@Test
void testProperlyReadsStoredEntryWithDataDescriptorWithSignature() throws IOException {
- try (InputStream fs = newInputStream("bla-stored-dd.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) {
+ try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder()
+ .setURI(getURI("bla-stored-dd.zip"))
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
final ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -410,7 +418,9 @@ void testProperlyReadsStoredEntryWithDataDescriptorWithSignature() throws IOExce
void testProperUseOfInflater() throws Exception {
try (ZipFile zf = ZipFile.builder().setFile(getFile("COMPRESS-189.zip")).get()) {
final ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder()
+ .setInputStream(new BufferedInputStream(zf.getInputStream(zae)))
+ .get()) {
ZipArchiveEntry innerEntry;
while ((innerEntry = in.getNextZipEntry()) != null) {
if (innerEntry.getName().endsWith("XML")) {
@@ -428,7 +438,7 @@ void testProperUseOfInflater() throws Exception {
void testReadDeflate64CompressedStream() throws Exception {
final byte[] orig = readAllBytes("COMPRESS-380/COMPRESS-380-input");
final File archive = getFile("COMPRESS-380/COMPRESS-380.zip");
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(archive).get()) {
assertNotNull(zin.getNextZipEntry());
final byte[] fromZip = IOUtils.toByteArray(zin);
assertArrayEquals(orig, fromZip);
@@ -439,7 +449,7 @@ void testReadDeflate64CompressedStream() throws Exception {
void testReadDeflate64CompressedStreamWithDataDescriptor() throws Exception {
// this is a copy of bla.jar with META-INF/MANIFEST.MF's method manually changed to ENHANCED_DEFLATED
final File archive = getFile("COMPRESS-380/COMPRESS-380-dd.zip");
- try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ZipArchiveInputStream zin = ZipArchiveInputStream.builder().setFile(archive).get()) {
final ZipArchiveEntry e = zin.getNextZipEntry();
assertEquals(-1, e.getSize());
assertEquals(ZipMethod.ENHANCED_DEFLATED.getCode(), e.getMethod());
@@ -458,7 +468,7 @@ void testReadDeflate64CompressedStreamWithDataDescriptor() throws Exception {
@Test
void testReadingOfFirstStoredEntry() throws Exception {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-264.zip"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-264.zip")).get()) {
final ZipArchiveEntry ze = in.getNextZipEntry();
assertEquals(5, ze.getSize());
assertArrayEquals(new byte[] { 'd', 'a', 't', 'a', '\n' }, IOUtils.toByteArray(in));
@@ -467,8 +477,9 @@ void testReadingOfFirstStoredEntry() throws Exception {
@Test
void testRejectsStoredEntriesWithDataDescriptorByDefault() throws IOException {
- try (InputStream fs = newInputStream("bla-stored-dd.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs)) {
+ try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder()
+ .setURI(getURI("bla-stored-dd.zip"))
+ .get()) {
final ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -481,7 +492,8 @@ void testRejectsStoredEntriesWithDataDescriptorByDefault() throws IOException {
@Test
void testShouldConsumeArchiveCompletely() throws Exception {
try (InputStream is = ZipArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.zip");
- ZipArchiveInputStream zip = new ZipArchiveInputStream(is)) {
+ ZipArchiveInputStream zip =
+ ZipArchiveInputStream.builder().setInputStream(is).get()) {
getAllZipEntries(zip);
final byte[] expected = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' };
final byte[] actual = new byte[expected.length];
@@ -495,7 +507,7 @@ void testShouldConsumeArchiveCompletely() throws Exception {
*/
@Test
void testShouldReadNestedZip() throws IOException {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("COMPRESS-219.zip"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("COMPRESS-219.zip")).get()) {
extractZipInputStream(in);
}
}
@@ -537,8 +549,8 @@ void testSingleByteReadThrowsAtEofForCorruptedStoredEntry() throws Exception {
for (int i = 17; i < 26; i++) {
content[i] = (byte) 0xff;
}
- try (ByteArrayInputStream in = new ByteArrayInputStream(content);
- ZipArchiveInputStream archive = new ZipArchiveInputStream(in)) {
+ try (ZipArchiveInputStream archive =
+ ZipArchiveInputStream.builder().setByteArray(content).get()) {
assertNotNull(archive.getNextEntry());
final IOException ex1 = assertThrows(ArchiveException.class, () -> IOUtils.toByteArray(archive), "expected exception");
assertEquals("Truncated ZIP file", ex1.getMessage());
@@ -554,12 +566,16 @@ void testSplitZipCreatedByWinrar() throws IOException {
final File lastFile = getFile("COMPRESS-477/split_zip_created_by_winrar/split_zip_created_by_winrar.zip");
try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
InputStream inputStream = Channels.newInputStream(channel);
- ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) {
+ ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .setSkipSplitSig(true)
+ .get()) {
final File fileToCompare = getFile("COMPRESS-477/split_zip_created_by_winrar/zip_to_compare_created_by_winrar.zip");
- try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare.toPath()),
- StandardCharsets.UTF_8.name(), true, false, true)) {
-
+ try (ZipArchiveInputStream inputStreamToCompare = ZipArchiveInputStream.builder()
+ .setFile(fileToCompare)
+ .setSkipSplitSig(true)
+ .get()) {
ArchiveEntry entry;
while ((entry = splitInputStream.getNextEntry()) != null && inputStreamToCompare.getNextEntry() != null) {
if (entry.isDirectory()) {
@@ -576,7 +592,10 @@ void testSplitZipCreatedByZip() throws IOException {
final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.zip");
try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
InputStream inputStream = Channels.newInputStream(channel);
- ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) {
+ ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .setSkipSplitSig(true)
+ .get()) {
final Path fileToCompare = getPath("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip.zip");
try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare), StandardCharsets.UTF_8.name(),
@@ -598,11 +617,16 @@ void testSplitZipCreatedByZipOfZip64() throws IOException {
final File lastFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip_zip64.zip");
try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile);
InputStream inputStream = Channels.newInputStream(channel);
- ZipArchiveInputStream splitInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, false, true)) {
+ ZipArchiveInputStream splitInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .setSkipSplitSig(true)
+ .get()) {
final Path fileToCompare = getPath("COMPRESS-477/split_zip_created_by_zip/zip_to_compare_created_by_zip_zip64.zip");
- try (ZipArchiveInputStream inputStreamToCompare = new ZipArchiveInputStream(Files.newInputStream(fileToCompare), StandardCharsets.UTF_8.name(),
- true, false, true)) {
+ try (ZipArchiveInputStream inputStreamToCompare = ZipArchiveInputStream.builder()
+ .setPath(fileToCompare)
+ .setSkipSplitSig(true)
+ .get()) {
ArchiveEntry entry;
while ((entry = splitInputStream.getNextEntry()) != null && inputStreamToCompare.getNextEntry() != null) {
@@ -617,9 +641,10 @@ void testSplitZipCreatedByZipOfZip64() throws IOException {
@Test
void testSplitZipCreatedByZipThrowsException() throws IOException {
- final File zipSplitFile = getFile("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01");
- try (ZipArchiveInputStream inputStream = new ZipArchiveInputStream(Files.newInputStream(zipSplitFile.toPath()), StandardCharsets.UTF_8.name(), true,
- false, true)) {
+ try (ZipArchiveInputStream inputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-477/split_zip_created_by_zip/split_zip_created_by_zip.z01"))
+ .setSkipSplitSig(true)
+ .get()) {
assertThrows(EOFException.class, () -> {
ArchiveEntry entry = inputStream.getNextEntry();
@@ -635,7 +660,9 @@ void testSplitZipCreatedByZipThrowsException() throws IOException {
*/
@Test
void testThrowOnInvalidEntry() throws Exception {
- try (ZipArchiveInputStream zip = new ZipArchiveInputStream(ZipArchiveInputStreamTest.class.getResourceAsStream("/invalid-zip.zip"))) {
+ try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder()
+ .setURI(getURI("invalid-zip.zip"))
+ .get()) {
final ZipException expected = assertThrows(ZipException.class, zip::getNextZipEntry, "IOException expected");
assertTrue(expected.getMessage().contains("Cannot find zip signature"));
}
@@ -643,8 +670,10 @@ void testThrowOnInvalidEntry() throws Exception {
@Test
void testThrowsIfStoredDDIsDifferentFromLengthRead() throws IOException {
- try (InputStream fs = newInputStream("bla-stored-dd-contradicts-actualsize.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) {
+ try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder()
+ .setURI(getURI("bla-stored-dd-contradicts-actualsize.zip"))
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
final ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -656,8 +685,10 @@ void testThrowsIfStoredDDIsDifferentFromLengthRead() throws IOException {
@Test
void testThrowsIfStoredDDIsInconsistent() throws IOException {
- try (InputStream fs = newInputStream("bla-stored-dd-sizes-differ.zip");
- ZipArchiveInputStream archive = new ZipArchiveInputStream(fs, StandardCharsets.UTF_8.name(), true, true)) {
+ try (ZipArchiveInputStream archive = ZipArchiveInputStream.builder()
+ .setURI(getURI("bla-stored-dd-sizes-differ.zip"))
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
final ZipArchiveEntry e = archive.getNextZipEntry();
assertNotNull(e);
assertEquals("test1.xml", e.getName());
@@ -689,15 +720,16 @@ void testThrowsIfZip64ExtraCouldNotBeUnderstood() {
@Test
void testThrowsIOExceptionIfThereIsCorruptedZip64Extra() throws IOException {
- try (InputStream fis = newInputStream("COMPRESS-546.zip");
- ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fis)) {
+ try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-546.zip"))
+ .get()) {
assertThrows(IOException.class, () -> getAllZipEntries(zipInputStream));
}
}
@Test
void testUnshrinkEntry() throws Exception {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("SHRUNK.ZIP"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("SHRUNK.ZIP")).get()) {
ZipArchiveEntry entry = in.getNextZipEntry();
assertEquals(ZipMethod.UNSHRINKING.getCode(), entry.getMethod(), "method");
assertTrue(in.canReadEntryData(entry));
@@ -721,7 +753,7 @@ void testUnshrinkEntry() throws Exception {
@Test
void testUnzipBZip2CompressedEntry() throws Exception {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("bzip2-zip.zip"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("bzip2-zip.zip")).get()) {
final ZipArchiveEntry ze = in.getNextZipEntry();
assertEquals(42, ze.getSize());
final byte[] expected = ArrayFill.fill(new byte[42], (byte) 'a');
@@ -734,7 +766,7 @@ void testUnzipBZip2CompressedEntry() throws Exception {
*/
@Test
void testWinzipBackSlashWorkaround() throws Exception {
- try (ZipArchiveInputStream in = new ZipArchiveInputStream(newInputStream("test-winzip.zip"))) {
+ try (ZipArchiveInputStream in = ZipArchiveInputStream.builder().setURI(getURI("test-winzip.zip")).get()) {
ZipArchiveEntry zae = in.getNextZipEntry();
zae = in.getNextZipEntry();
zae = in.getNextZipEntry();
@@ -748,8 +780,9 @@ void testWinzipBackSlashWorkaround() throws Exception {
@Test
void testWithBytesAfterData() throws Exception {
final int expectedNumEntries = 2;
- try (InputStream is = ZipArchiveInputStreamTest.class.getResourceAsStream("/archive_with_bytes_after_data.zip");
- ZipArchiveInputStream zip = new ZipArchiveInputStream(is)) {
+ try (ZipArchiveInputStream zip = ZipArchiveInputStream.builder()
+ .setURI(getURI("archive_with_bytes_after_data.zip"))
+ .get()) {
int actualNumEntries = 0;
ZipArchiveEntry zae = zip.getNextZipEntry();
while (zae != null) {
@@ -785,7 +818,9 @@ void testWriteZipWithLinks() throws IOException {
assertFalse(zipFile.getEntry("original").isUnixSymlink(), "'original' detected but it's not sym link");
}
// Doesn't reads the central directory
- try (ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(new FileInputStream("target/zipWithLinks.zip"))) {
+ try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder()
+ .setFile("target/zipWithLinks.zip")
+ .get()) {
ZipArchiveEntry entry;
int entriesCount = 0;
while ((entry = zipInputStream.getNextEntry()) != null) {
@@ -830,8 +865,11 @@ void testZipArchiveInputStreamSubclassReplacement() throws IOException {
@ParameterizedTest
@ValueSource(booleans = { true, false })
void testZipInputStream(final boolean allowStoredEntriesWithDataDescriptor) {
- try (ZipArchiveInputStream zIn = new ZipArchiveInputStream(Files.newInputStream(Paths.get("src/test/resources/COMPRESS-647/test.zip")),
- StandardCharsets.UTF_8.name(), false, allowStoredEntriesWithDataDescriptor)) {
+ try (ZipArchiveInputStream zIn = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-647/test.zip"))
+ .setUseUnicodeExtraFields(false)
+ .setAllowStoredEntriesWithDataDescriptor(allowStoredEntriesWithDataDescriptor)
+ .get()) {
ZipArchiveEntry zae = zIn.getNextEntry();
while (zae != null) {
zae = zIn.getNextEntry();
@@ -844,15 +882,19 @@ void testZipInputStream(final boolean allowStoredEntriesWithDataDescriptor) {
@Test
void testZipUsingStoredWithDDAndNoDDSignature() throws IOException {
try (InputStream inputStream = forgeZipInputStream();
- ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(inputStream, StandardCharsets.UTF_8.name(), true, true)) {
+ ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder()
+ .setInputStream(inputStream)
+ .setAllowStoredEntriesWithDataDescriptor(true)
+ .get()) {
getAllZipEntries(zipInputStream);
}
}
@Test
void testZipWithBadExtraFields() throws IOException {
- try (InputStream fis = newInputStream("COMPRESS-548.zip");
- ZipArchiveInputStream zipInputStream = new ZipArchiveInputStream(fis)) {
+ try (ZipArchiveInputStream zipInputStream = ZipArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-548.zip"))
+ .get()) {
getAllZipEntries(zipInputStream);
}
}
@@ -872,8 +914,8 @@ void testZipWithLongerBeginningGarbage() throws IOException {
}
}
- try (InputStream is = Files.newInputStream(path);
- ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) {
+ try (ZipArchiveInputStream zis =
+ ZipArchiveInputStream.builder().setPath(path).get()) {
final ZipArchiveEntry entry = zis.getNextEntry();
assertEquals("file-1.txt", entry.getName());
final byte[] content = IOUtils.toByteArray(zis);
@@ -896,8 +938,8 @@ void testZipWithShortBeginningGarbage() throws IOException {
}
}
- try (InputStream is = Files.newInputStream(path);
- ZipArchiveInputStream zis = new ZipArchiveInputStream(is)) {
+ try (ZipArchiveInputStream zis =
+ ZipArchiveInputStream.builder().setPath(path).get()) {
final ZipArchiveEntry entry = zis.getNextEntry();
assertEquals("file-1.txt", entry.getName());
final byte[] content = IOUtils.toByteArray(zis);
(commons-compress) 02/08: feat: add builder for `ArArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 7dc70dd2dbad4fafe09e332d9257053c3b065dd7
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 18:04:07 2025 +0200
feat: add builder for `ArArchiveInputStream`
Introduce a builder for `ArArchiveInputStream` and refactor existing
constructor calls to use it.
---
.../compress/archivers/ArchiveStreamFactory.java | 85 ++++++++++++----------
.../archivers/ar/ArArchiveInputStream.java | 44 ++++++++++-
.../org/apache/commons/compress/AbstractTest.java | 19 +++--
.../apache/commons/compress/archivers/ArTest.java | 10 +--
.../archivers/ar/ArArchiveInputStreamTest.java | 50 +++++++------
.../compress/archivers/ar/Compress678Test.java | 5 +-
6 files changed, 135 insertions(+), 78 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index b2f50bac1..ae105b00a 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -428,53 +428,58 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
if (in == null) {
throw new IllegalArgumentException("InputStream must not be null.");
}
- if (AR.equalsIgnoreCase(archiverName)) {
- return (I) new ArArchiveInputStream(in);
- }
- if (ARJ.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new ArjArchiveInputStream(in, actualEncoding);
+ try {
+ if (AR.equalsIgnoreCase(archiverName)) {
+ return (I) ArArchiveInputStream.builder().setInputStream(in).get();
}
- return (I) new ArjArchiveInputStream(in);
- }
- if (ZIP.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new ZipArchiveInputStream(in, actualEncoding);
+ if (ARJ.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new ArjArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new ArjArchiveInputStream(in);
}
- return (I) new ZipArchiveInputStream(in);
- }
- if (TAR.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new TarArchiveInputStream(in, actualEncoding);
+ if (ZIP.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new ZipArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new ZipArchiveInputStream(in);
}
- return (I) new TarArchiveInputStream(in);
- }
- if (JAR.equalsIgnoreCase(archiverName) || APK.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new JarArchiveInputStream(in, actualEncoding);
+ if (TAR.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new TarArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new TarArchiveInputStream(in);
}
- return (I) new JarArchiveInputStream(in);
- }
- if (CPIO.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new CpioArchiveInputStream(in, actualEncoding);
+ if (JAR.equalsIgnoreCase(archiverName) || APK.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new JarArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new JarArchiveInputStream(in);
}
- return (I) new CpioArchiveInputStream(in);
- }
- if (DUMP.equalsIgnoreCase(archiverName)) {
- if (actualEncoding != null) {
- return (I) new DumpArchiveInputStream(in, actualEncoding);
+ if (CPIO.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new CpioArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new CpioArchiveInputStream(in);
}
- return (I) new DumpArchiveInputStream(in);
- }
- if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
- throw new StreamingNotSupportedException(SEVEN_Z);
- }
- final ArchiveStreamProvider archiveStreamProvider = getArchiveInputStreamProviders().get(toKey(archiverName));
- if (archiveStreamProvider != null) {
- return archiveStreamProvider.createArchiveInputStream(archiverName, in, actualEncoding);
+ if (DUMP.equalsIgnoreCase(archiverName)) {
+ if (actualEncoding != null) {
+ return (I) new DumpArchiveInputStream(in, actualEncoding);
+ }
+ return (I) new DumpArchiveInputStream(in);
+ }
+ if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
+ throw new StreamingNotSupportedException(SEVEN_Z);
+ }
+ final ArchiveStreamProvider archiveStreamProvider = getArchiveInputStreamProviders().get(toKey(archiverName));
+ if (archiveStreamProvider != null) {
+ return archiveStreamProvider.createArchiveInputStream(archiverName, in, actualEncoding);
+ }
+ throw new ArchiveException("Archiver: %s not found.", archiverName);
+ } catch (final IOException e) {
+ throw new ArchiveException(
+ "IOException while creating " + archiverName + " input stream: " + e.getMessage(), e);
}
- throw new ArchiveException("Archiver: %s not found.", archiverName);
}
/**
diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
index 4420c60f9..5875c22ec 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java
@@ -39,6 +39,30 @@
*/
public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
+ /**
+ * Builds a new {@link ArArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * ArArchiveInputStream in = ArArchiveInputStream.builder()
+ * .setPath(inputPath)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static final class Builder extends ArchiveInputStream.Builder<ArArchiveInputStream, Builder> {
+
+ private Builder() {
+ setCharset(StandardCharsets.US_ASCII);
+ }
+
+ @Override
+ public ArArchiveInputStream get() throws IOException {
+ return new ArArchiveInputStream(this);
+ }
+ }
+
// offsets and length of meta data parts
private static final int NAME_OFFSET = 0;
private static final int NAME_LEN = 16;
@@ -113,6 +137,16 @@ public static boolean matches(final byte[] buffer, final int ignored) {
return ArrayUtils.startsWith(buffer, ArArchiveEntry.HEADER_BYTES);
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
private boolean closed;
/*
@@ -137,7 +171,15 @@ public static boolean matches(final byte[] buffer, final int ignored) {
* @param inputStream the ar input stream
*/
public ArArchiveInputStream(final InputStream inputStream) {
- super(inputStream, StandardCharsets.US_ASCII.name());
+ this(inputStream, builder());
+ }
+
+ private ArArchiveInputStream(final Builder builder) throws IOException {
+ this(builder.getInputStream(), builder);
+ }
+
+ private ArArchiveInputStream(final InputStream inputStream, final Builder builder) {
+ super(inputStream, builder.getCharset());
}
private int asInt(final byte[] byteArray, final int offset, final int len, final boolean treatBlankAsZero) throws IOException {
diff --git a/src/test/java/org/apache/commons/compress/AbstractTest.java b/src/test/java/org/apache/commons/compress/AbstractTest.java
index 71941a03b..1a72bb298 100644
--- a/src/test/java/org/apache/commons/compress/AbstractTest.java
+++ b/src/test/java/org/apache/commons/compress/AbstractTest.java
@@ -28,6 +28,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
@@ -81,21 +82,25 @@ public static boolean forceDelete(final Path path) {
}
public static File getFile(final String path) throws IOException {
- final URL url = AbstractTest.class.getClassLoader().getResource(path);
+ return new File(getURI(path));
+ }
+
+ public static Path getPath(final String path) throws IOException {
+ return getFile(path).toPath();
+ }
+
+ public static URI getURI(final String resourceName) throws IOException {
+ final URL url = AbstractTest.class.getClassLoader().getResource(resourceName);
if (url == null) {
- throw new FileNotFoundException("couldn't find " + path);
+ throw new FileNotFoundException("couldn't find " + resourceName);
}
try {
- return new File(url.toURI());
+ return url.toURI();
} catch (final URISyntaxException ex) {
throw new IOException(ex);
}
}
- public static Path getPath(final String path) throws IOException {
- return getFile(path).toPath();
- }
-
public static InputStream newInputStream(final String path) throws IOException {
return Files.newInputStream(getPath(path));
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/ArTest.java b/src/test/java/org/apache/commons/compress/archivers/ArTest.java
index 965005779..542402493 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ArTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ArTest.java
@@ -180,7 +180,7 @@ void testExplicitFileEntry() throws Exception {
}
//
final ArArchiveEntry out;
- try (ArArchiveInputStream ais = new ArArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ArArchiveInputStream ais = ArArchiveInputStream.builder().setFile(archive).get()) {
out = ais.getNextEntry();
}
assertNotNull(out);
@@ -201,7 +201,7 @@ void testFileEntryFromFile() throws Exception {
aos.closeArchiveEntry();
}
final ArArchiveEntry out;
- try (ArArchiveInputStream ais = new ArArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ArArchiveInputStream ais = ArArchiveInputStream.builder().setFile(archive).get()) {
out = ais.getNextEntry();
}
assertNotNull(out);
@@ -223,7 +223,7 @@ void testFileEntryFromPath() throws Exception {
aos.closeArchiveEntry();
}
final ArArchiveEntry out;
- try (ArArchiveInputStream ais = new ArArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ArArchiveInputStream ais = ArArchiveInputStream.builder().setFile(archive).get()) {
out = ais.getNextEntry();
}
assertNotNull(out);
@@ -248,7 +248,7 @@ void testXtestDirectoryEntryFromFile() throws Exception {
aos.closeArchiveEntry();
}
final ArArchiveEntry out;
- try (ArArchiveInputStream ais = new ArArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ArArchiveInputStream ais = ArArchiveInputStream.builder().setFile(archive).get()) {
out = ais.getNextEntry();
}
assertNotNull(out);
@@ -273,7 +273,7 @@ void testXtestExplicitDirectoryEntry() throws Exception {
aos.closeArchiveEntry();
}
final ArArchiveEntry out;
- try (ArArchiveInputStream ais = new ArArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (ArArchiveInputStream ais = ArArchiveInputStream.builder().setFile(archive).get()) {
out = ais.getNextEntry();
}
assertNotNull(out);
diff --git a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
index 7ab223f22..0192cd42b 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStreamTest.java
@@ -28,8 +28,6 @@
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -43,7 +41,9 @@ class ArArchiveInputStreamTest extends AbstractTest {
private void checkLongNameEntry(final String archive) throws Exception {
try (InputStream fis = newInputStream(archive);
- ArArchiveInputStream s = new ArArchiveInputStream(new BufferedInputStream(fis))) {
+ ArArchiveInputStream s = ArArchiveInputStream.builder()
+ .setInputStream(new BufferedInputStream(fis))
+ .get()) {
ArchiveEntry e = s.getNextEntry();
assertEquals("this_is_a_long_file_name.txt", e.getName());
assertEquals(14, e.getSize());
@@ -62,8 +62,7 @@ private void checkLongNameEntry(final String archive) throws Exception {
@Test
void testCantReadAfterClose() throws Exception {
- try (InputStream in = newInputStream("bla.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder().setURI(getURI("bla.ar")).get()) {
archive.close();
assertThrows(IllegalStateException.class, () -> archive.read());
}
@@ -71,8 +70,7 @@ void testCantReadAfterClose() throws Exception {
@Test
void testCantReadWithoutOpeningAnEntry() throws Exception {
- try (InputStream in = newInputStream("bla.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder().setURI(getURI("bla.ar")).get()) {
assertThrows(IllegalStateException.class, () -> archive.read());
}
}
@@ -85,7 +83,9 @@ void testCompress661() throws IOException {
private void testCompress661(final boolean checkMarkReadReset) throws IOException {
try (InputStream in = newInputStream("org/apache/commons/compress/COMPRESS-661/testARofText.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(new BufferedInputStream(in))) {
+ ArArchiveInputStream archive = ArArchiveInputStream.builder()
+ .setInputStream(new BufferedInputStream(in))
+ .get()) {
assertNotNull(archive.getNextEntry());
if (checkMarkReadReset && archive.markSupported()) {
// mark() shouldn't be supported, but if it would be,
@@ -107,16 +107,18 @@ private void testCompress661(final boolean checkMarkReadReset) throws IOExceptio
*/
@Test
void testGetNextArEntry() throws IOException {
- try (ArArchiveInputStream inputStream = new ArArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/ar/getNextArEntry.bin")))) {
+ try (ArArchiveInputStream inputStream = ArArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/ar/getNextArEntry.bin"))
+ .get()) {
assertThrows(EOFException.class, inputStream::getNextEntry);
}
}
@Test
void testInvalidBadTableLength() throws Exception {
- try (InputStream in = newInputStream("org/apache/commons/compress/ar/number_parsing/bad_table_length_gnu-fail.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/ar/number_parsing/bad_table_length_gnu-fail.ar"))
+ .get()) {
assertThrows(IOException.class, archive::getNextEntry);
}
}
@@ -125,8 +127,9 @@ void testInvalidBadTableLength() throws Exception {
@ValueSource(strings = { "bad_long_namelen_bsd-fail.ar", "bad_long_namelen_gnu1-fail.ar", "bad_long_namelen_gnu2-fail.ar", "bad_long_namelen_gnu3-fail.ar",
"bad_table_length_gnu-fail.ar" })
void testInvalidLongNameLength(final String testFileName) throws Exception {
- try (InputStream in = newInputStream("org/apache/commons/compress/ar/number_parsing/" + testFileName);
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/ar/number_parsing/" + testFileName))
+ .get()) {
assertThrows(IOException.class, archive::getNextEntry);
}
}
@@ -134,8 +137,9 @@ void testInvalidLongNameLength(final String testFileName) throws Exception {
@ParameterizedTest
@ValueSource(strings = { "bad_group-fail.ar", "bad_length-fail.ar", "bad_modified-fail.ar", "bad_user-fail.ar" })
void testInvalidNumericFields(final String testFileName) throws Exception {
- try (InputStream in = newInputStream("org/apache/commons/compress/ar/number_parsing/" + testFileName);
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/ar/number_parsing/" + testFileName))
+ .get()) {
assertThrows(IOException.class, archive::getNextEntry);
}
}
@@ -143,8 +147,7 @@ void testInvalidNumericFields(final String testFileName) throws Exception {
@Test
void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder().setURI(getURI("bla.ar")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read(buf));
@@ -173,7 +176,9 @@ public int read() throws IOException {
return fileInputStream.read();
}
}) {
- try (ArArchiveInputStream archiveInputStream = new ArArchiveInputStream(simpleInputStream)) {
+ try (ArArchiveInputStream archiveInputStream = ArArchiveInputStream.builder()
+ .setInputStream(simpleInputStream)
+ .get()) {
final ArArchiveEntry entry1 = archiveInputStream.getNextEntry();
assertNotNull(entry1);
assertEquals("test1.xml", entry1.getName());
@@ -197,7 +202,9 @@ public int read() throws IOException {
return fileInputStream.read();
}
}) {
- try (ArArchiveInputStream archiveInputStream = new ArArchiveInputStream(simpleInputStream)) {
+ try (ArArchiveInputStream archiveInputStream = ArArchiveInputStream.builder()
+ .setInputStream(simpleInputStream)
+ .get()) {
final ArArchiveEntry entry1 = archiveInputStream.getNextEntry();
assertNotNull(entry1);
assertEquals("test1.xml", entry1.getName());
@@ -212,8 +219,7 @@ public int read() throws IOException {
@Test
void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
- try (InputStream in = newInputStream("bla.ar");
- ArArchiveInputStream archive = new ArArchiveInputStream(in)) {
+ try (ArArchiveInputStream archive = ArArchiveInputStream.builder().setURI(getURI("bla.ar")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());
diff --git a/src/test/java/org/apache/commons/compress/archivers/ar/Compress678Test.java b/src/test/java/org/apache/commons/compress/archivers/ar/Compress678Test.java
index 4074186ac..571dd8917 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ar/Compress678Test.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ar/Compress678Test.java
@@ -24,7 +24,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -69,7 +68,7 @@ private void test_LONGFILE_BSD(final String fileName) throws IOException, FileNo
arOut.write(data);
arOut.closeArchiveEntry();
}
- try (ArArchiveInputStream arIn = new ArArchiveInputStream(new FileInputStream(file))) {
+ try (ArArchiveInputStream arIn = ArArchiveInputStream.builder().setFile(file).get()) {
final ArArchiveEntry entry = arIn.getNextEntry();
assertEquals(fileName, entry.getName());
// Fix
@@ -120,7 +119,7 @@ void testShortName() throws IOException {
arOut.write(data);
arOut.closeArchiveEntry();
}
- try (ArArchiveInputStream arIn = new ArArchiveInputStream(new FileInputStream(file))) {
+ try (ArArchiveInputStream arIn = ArArchiveInputStream.builder().setFile(file).get()) {
final ArArchiveEntry entry = arIn.getNextEntry();
assertEquals(name1, entry.getName());
assertNotNull(arIn.getNextEntry());
(commons-compress) 04/08: feat: add builder for `CpioArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit d4bcf0e7754bef7cbde7a85ef3fd0de058648db1
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 18:37:47 2025 +0200
feat: add builder for `CpioArchiveInputStream`
Introduce a builder for `CpioArchiveInputStream` and refactor existing
constructor calls to use it.
---
.../compress/archivers/ArchiveStreamFactory.java | 5 +-
.../archivers/cpio/CpioArchiveInputStream.java | 79 +++++++++++++++++++---
.../archivers/cpio/CpioArchiveOutputStream.java | 4 +-
.../commons/compress/archivers/cpio/CpioUtil.java | 3 +-
.../commons/compress/archivers/CpioTest.java | 10 +--
.../archivers/cpio/CpioArchiveInputStreamTest.java | 31 +++++----
.../cpio/CpioArchiveOutputStreamTest.java | 2 +-
.../compress/archivers/cpio/CpioArchiveTest.java | 18 +++--
8 files changed, 111 insertions(+), 41 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index 8559b8bd1..64b76288f 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -458,10 +458,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) new JarArchiveInputStream(in);
}
if (CPIO.equalsIgnoreCase(archiverName)) {
+ final CpioArchiveInputStream.Builder cpioBuilder = CpioArchiveInputStream.builder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new CpioArchiveInputStream(in, actualEncoding);
+ cpioBuilder.setCharset(actualEncoding);
}
- return (I) new CpioArchiveInputStream(in);
+ return (I) cpioBuilder.get();
}
if (DUMP.equalsIgnoreCase(archiverName)) {
if (actualEncoding != null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
index 85c137725..e2008005b 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java
@@ -64,6 +64,48 @@
*/
public class CpioArchiveInputStream extends ArchiveInputStream<CpioArchiveEntry> implements CpioConstants {
+ /**
+ * Builds a new {@link CpioArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ * .setBlockSize(1024)
+ * .setPath(inputPath)
+ * .setCharset(StandardCharsets.UTF_8)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static final class Builder extends ArchiveInputStream.Builder<CpioArchiveInputStream, Builder> {
+
+ private int blockSize = BLOCK_SIZE;
+
+ private Builder() {
+ setCharset(CpioUtil.DEFAULT_CHARSET);
+ }
+
+ /**
+ * Sets the block size of the archive.
+ * <p>
+ * Default value is {@value CpioConstants#BLOCK_SIZE}.
+ * </p>
+ *
+ * @param blockSize The block size must be bigger than 0
+ * @return this
+ */
+ public Builder setBlockSize(final int blockSize) {
+ this.blockSize = blockSize;
+ return asThis();
+ }
+
+ @Override
+ public CpioArchiveInputStream get() throws IOException {
+ return new CpioArchiveInputStream(this);
+ }
+ }
+
/**
* Checks if the signature matches one of the following magic values:
*
@@ -117,6 +159,16 @@ public static boolean matches(final byte[] signature, final int length) {
return false;
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
private boolean closed;
private CpioArchiveEntry entry;
@@ -150,7 +202,7 @@ public static boolean matches(final byte[] signature, final int length) {
* @param in The cpio stream
*/
public CpioArchiveInputStream(final InputStream in) {
- this(in, BLOCK_SIZE, CpioUtil.DEFAULT_CHARSET_NAME);
+ this(in, builder());
}
/**
@@ -161,7 +213,7 @@ public CpioArchiveInputStream(final InputStream in) {
* @since 1.5
*/
public CpioArchiveInputStream(final InputStream in, final int blockSize) {
- this(in, blockSize, CpioUtil.DEFAULT_CHARSET_NAME);
+ this(in, builder().setBlockSize(blockSize));
}
/**
@@ -174,13 +226,7 @@ public CpioArchiveInputStream(final InputStream in, final int blockSize) {
* @since 1.6
*/
public CpioArchiveInputStream(final InputStream in, final int blockSize, final String encoding) {
- super(in, encoding);
- this.in = in;
- if (blockSize <= 0) {
- throw new IllegalArgumentException("blockSize must be bigger than 0");
- }
- this.blockSize = blockSize;
- this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
+ this(in, builder().setBlockSize(blockSize).setCharset(encoding));
}
/**
@@ -191,7 +237,20 @@ public CpioArchiveInputStream(final InputStream in, final int blockSize, final S
* @since 1.6
*/
public CpioArchiveInputStream(final InputStream in, final String encoding) {
- this(in, BLOCK_SIZE, encoding);
+ this(in, builder().setCharset(encoding));
+ }
+
+ private CpioArchiveInputStream(final Builder builder) throws IOException {
+ this(builder.getInputStream(), builder);
+ }
+
+ private CpioArchiveInputStream(final InputStream in, final Builder builder) {
+ super(in, builder.getCharset());
+ if (builder.blockSize <= 0) {
+ throw new IllegalArgumentException("blockSize must be bigger than 0");
+ }
+ this.blockSize = builder.blockSize;
+ this.zipEncoding = ZipEncodingHelper.getZipEncoding(builder.getCharset());
}
/**
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
index d03e14807..e0e7d2361 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java
@@ -116,7 +116,7 @@ public CpioArchiveOutputStream(final OutputStream out) {
* @param format The format of the stream
*/
public CpioArchiveOutputStream(final OutputStream out, final short format) {
- this(out, format, BLOCK_SIZE, CpioUtil.DEFAULT_CHARSET_NAME);
+ this(out, format, BLOCK_SIZE, CpioUtil.DEFAULT_CHARSET.name());
}
/**
@@ -128,7 +128,7 @@ public CpioArchiveOutputStream(final OutputStream out, final short format) {
* @since 1.1
*/
public CpioArchiveOutputStream(final OutputStream out, final short format, final int blockSize) {
- this(out, format, blockSize, CpioUtil.DEFAULT_CHARSET_NAME);
+ this(out, format, blockSize, CpioUtil.DEFAULT_CHARSET.name());
}
/**
diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioUtil.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioUtil.java
index 7c71ee1dd..a43409822 100644
--- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioUtil.java
+++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioUtil.java
@@ -18,6 +18,7 @@
*/
package org.apache.commons.compress.archivers.cpio;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
@@ -28,7 +29,7 @@
*/
final class CpioUtil {
- static final String DEFAULT_CHARSET_NAME = StandardCharsets.US_ASCII.name();
+ static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
/**
* Converts a byte array to a long. Halfwords can be swapped by setting swapHalfWord=true.
diff --git a/src/test/java/org/apache/commons/compress/archivers/CpioTest.java b/src/test/java/org/apache/commons/compress/archivers/CpioTest.java
index 1168e217f..72efa86a8 100644
--- a/src/test/java/org/apache/commons/compress/archivers/CpioTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/CpioTest.java
@@ -120,7 +120,7 @@ void testDirectoryEntryFromFile() throws Exception {
tos.closeArchiveEntry();
}
final CpioArchiveEntry entryOut;
- try (CpioArchiveInputStream tis = new CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (CpioArchiveInputStream tis = CpioArchiveInputStream.builder().setFile(archive).get()) {
entryOut = tis.getNextCPIOEntry();
}
assertNotNull(entryOut);
@@ -144,7 +144,7 @@ void testExplicitDirectoryEntry() throws Exception {
tos.closeArchiveEntry();
}
final CpioArchiveEntry out;
- try (CpioArchiveInputStream tis = new CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (CpioArchiveInputStream tis = CpioArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextCPIOEntry();
}
assertNotNull(out);
@@ -168,7 +168,7 @@ void testExplicitFileEntry() throws Exception {
tos.closeArchiveEntry();
}
final CpioArchiveEntry out;
- try (CpioArchiveInputStream tis = new CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (CpioArchiveInputStream tis = CpioArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextCPIOEntry();
}
assertNotNull(out);
@@ -189,7 +189,7 @@ void testFileEntryFromFile() throws Exception {
tos.closeArchiveEntry();
}
final CpioArchiveEntry out;
- try (CpioArchiveInputStream tis = new CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (CpioArchiveInputStream tis = CpioArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextCPIOEntry();
}
assertNotNull(out);
@@ -220,7 +220,7 @@ void testSymbolicLinkFileEntry() throws Exception {
tos.closeArchiveEntry();
}
final CpioArchiveEntry entry;
- try (CpioArchiveInputStream tis = new CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (CpioArchiveInputStream tis = CpioArchiveInputStream.builder().setFile(archive).get()) {
entry = tis.getNextEntry();
assertEquals(nameLink, IOUtils.toString(tis, charset));
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
index 2a0490909..b8eb5f5f9 100644
--- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStreamTest.java
@@ -22,9 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.compress.AbstractTest;
@@ -52,7 +50,8 @@ void testCpioUnarchive() throws Exception {
expected.append("<empty/>./test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
- try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("bla.cpio"))) {
+ try (CpioArchiveInputStream in =
+ CpioArchiveInputStream.builder().setURI(getURI("bla.cpio")).get()) {
CpioArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.append(entry.getName());
@@ -68,7 +67,9 @@ void testCpioUnarchive() throws Exception {
@Test
void testCpioUnarchiveCreatedByRedlineRpm() throws Exception {
long count = 0;
- try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("redline.cpio"))) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ .setURI(getURI("redline.cpio"))
+ .get()) {
count = consumeEntries(in);
}
assertEquals(count, 1);
@@ -77,7 +78,10 @@ void testCpioUnarchiveCreatedByRedlineRpm() throws Exception {
@Test
void testCpioUnarchiveMultibyteCharName() throws Exception {
long count = 0;
- try (CpioArchiveInputStream in = new CpioArchiveInputStream(newInputStream("COMPRESS-459.cpio"), StandardCharsets.UTF_8.name())) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-459.cpio"))
+ .setCharset(StandardCharsets.UTF_8)
+ .get()) {
count = consumeEntries(in);
}
assertEquals(2, count);
@@ -105,7 +109,7 @@ void testEndOfFileInEntry_c_namesize_0x7FFFFFFF() throws Exception {
// @formatter:on
final byte[] data = new byte[header.getBytes(StandardCharsets.US_ASCII).length + 1];
System.arraycopy(header.getBytes(), 0, data, 0, header.getBytes().length);
- try (CpioArchiveInputStream cpio = new CpioArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (CpioArchiveInputStream cpio = CpioArchiveInputStream.builder().setByteArray(data).get()) {
assertThrows(MemoryLimitException.class, () -> cpio.getNextEntry());
}
}
@@ -132,15 +136,16 @@ void testEndOfFileInEntry_c_namesize_0xFFFFFFFF() throws Exception {
// @formatter:on
final byte[] data = new byte[header.getBytes(StandardCharsets.US_ASCII).length + 1];
System.arraycopy(header.getBytes(), 0, data, 0, header.getBytes().length);
- try (CpioArchiveInputStream cpio = new CpioArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (CpioArchiveInputStream cpio = CpioArchiveInputStream.builder().setByteArray(data).get()) {
assertThrows(ArchiveException.class, () -> cpio.getNextEntry());
}
}
@Test
void testInvalidLongValueInMetadata() throws Exception {
- try (InputStream in = newInputStream("org/apache/commons/compress/cpio/bad_long_value.cpio");
- CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
+ try (CpioArchiveInputStream archive = CpioArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/cpio/bad_long_value.cpio"))
+ .get()) {
assertThrows(IOException.class, archive::getNextEntry);
}
}
@@ -148,8 +153,8 @@ void testInvalidLongValueInMetadata() throws Exception {
@Test
void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.cpio");
- CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
+ try (CpioArchiveInputStream archive =
+ CpioArchiveInputStream.builder().setURI(getURI("bla.cpio")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read(buf));
@@ -159,8 +164,8 @@ void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
@Test
void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
- try (InputStream in = newInputStream("bla.cpio");
- CpioArchiveInputStream archive = new CpioArchiveInputStream(in)) {
+ try (CpioArchiveInputStream archive =
+ CpioArchiveInputStream.builder().setURI(getURI("bla.cpio")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());
diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java
index 1dfc94371..7ec8d49bf 100644
--- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStreamTest.java
@@ -42,7 +42,7 @@ void testWriteOldBinary() throws Exception {
outputStream.closeArchiveEntry();
}
assertTrue(ref.isClosed());
- try (CpioArchiveInputStream in = new CpioArchiveInputStream(Files.newInputStream(output.toPath()))) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder().setFile(output).get()) {
final CpioArchiveEntry e = in.getNextCPIOEntry();
assertEquals("test1.xml", e.getName());
assertNull(in.getNextEntry());
diff --git a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java
index 805a0cef5..6df89bb2c 100644
--- a/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/cpio/CpioArchiveTest.java
@@ -22,7 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
@@ -50,8 +49,10 @@ void utf18RoundtripTestCtor2() throws Exception {
os.closeArchiveEntry();
}
baos.close();
- try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
- CpioArchiveInputStream in = new CpioArchiveInputStream(bin, StandardCharsets.UTF_8.name())) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ .setByteArray(baos.toByteArray())
+ .setCharset(StandardCharsets.UTF_8)
+ .get()) {
final CpioArchiveEntry entry = in.getNextEntry();
assertNotNull(entry);
assertEquals("Test.txt", entry.getName());
@@ -74,8 +75,9 @@ public void utf18RoundtripTestCtor3(final short format) throws Exception {
os.closeArchiveEntry();
}
baos.close();
- try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
- CpioArchiveInputStream in = new CpioArchiveInputStream(bin)) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ .setByteArray(baos.toByteArray())
+ .get()) {
final CpioArchiveEntry entry = in.getNextEntry();
assertNotNull(entry);
assertEquals("T%U00E4st.txt", entry.getName());
@@ -98,8 +100,10 @@ public void utf18RoundtripTestCtor4(final short format) throws Exception {
os.closeArchiveEntry();
}
baos.close();
- try (ByteArrayInputStream bin = new ByteArrayInputStream(baos.toByteArray());
- CpioArchiveInputStream in = new CpioArchiveInputStream(bin, StandardCharsets.UTF_16LE.name())) {
+ try (CpioArchiveInputStream in = CpioArchiveInputStream.builder()
+ .setByteArray(baos.toByteArray())
+ .setCharset(StandardCharsets.UTF_16LE)
+ .get()) {
final CpioArchiveEntry entry = in.getNextEntry();
assertNotNull(entry);
assertEquals("T\u00e4st.txt", entry.getName());
(commons-compress) 07/08: feat: use builder instead of TarArchiveInputStream constructors
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 95bd6cecb9f91d00d564a18148fdaaf41a4e6b11
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 22:41:46 2025 +0200
feat: use builder instead of TarArchiveInputStream constructors
Refactor code to replace direct `TarArchiveInputStream` constructor calls
with the builder, improving consistency and readability.
---
.../compress/archivers/ArchiveStreamFactory.java | 9 ++-
.../archivers/tar/TarArchiveInputStream.java | 47 +++++++-----
.../org/apache/commons/compress/ChainingTest.java | 10 ++-
.../commons/compress/DetectArchiverTest.java | 4 +-
.../apache/commons/compress/archivers/TarTest.java | 32 ++++----
.../commons/compress/archivers/tar/BigFilesIT.java | 6 +-
.../compress/archivers/tar/Compress700Test.java | 14 ++--
.../compress/archivers/tar/FileTimesIT.java | 66 +++++-----------
.../compress/archivers/tar/SparseFilesTest.java | 28 +++----
.../archivers/tar/TarArchiveEntryTest.java | 13 ++--
.../archivers/tar/TarArchiveInputStreamTest.java | 89 ++++++++++------------
.../archivers/tar/TarArchiveOutputStreamTest.java | 34 ++++-----
.../commons/compress/archivers/tar/TarLister.java | 11 +--
.../archivers/tar/TarMemoryFileSystemTest.java | 5 +-
.../compress/archivers/tar/TarUtilsTest.java | 5 +-
.../gzip/GzipCompressorInputStreamTest.java | 2 +-
.../compressors/pack200/Pack200UtilsTest.java | 5 +-
17 files changed, 187 insertions(+), 193 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index 7dd47f7a7..a953205d4 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -18,7 +18,6 @@
*/
package org.apache.commons.compress.archivers;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -270,7 +269,8 @@ public static String detect(final InputStream in) throws ArchiveException {
}
// COMPRESS-117
if (signatureLength >= TAR_HEADER_SIZE) {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(new ByteArrayInputStream(tarHeader))) {
+ try (TarArchiveInputStream inputStream =
+ TarArchiveInputStream.builder().setByteArray(tarHeader).get()) {
// COMPRESS-191 - verify the header checksum
TarArchiveEntry entry = inputStream.getNextEntry();
// try to find the first non-directory entry within the first 10 entries.
@@ -447,10 +447,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) zipBuilder.get();
}
if (TAR.equalsIgnoreCase(archiverName)) {
+ final TarArchiveInputStream.Builder tarBuilder = TarArchiveInputStream.builder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new TarArchiveInputStream(in, actualEncoding);
+ tarBuilder.setCharset(actualEncoding);
}
- return (I) new TarArchiveInputStream(in);
+ return (I) tarBuilder.get();
}
if (JAR.equalsIgnoreCase(archiverName) || APK.equalsIgnoreCase(archiverName)) {
final JarArchiveInputStream.Builder jarBuilder =
diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
index 2ac85e398..5f168e6f5 100644
--- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java
@@ -42,7 +42,6 @@
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.utils.ArchiveUtils;
import org.apache.commons.compress.utils.IOUtils;
-import org.apache.commons.io.build.AbstractStreamBuilder;
import org.apache.commons.io.input.BoundedInputStream;
/**
@@ -72,7 +71,7 @@ public class TarArchiveInputStream extends ArchiveInputStream<TarArchiveEntry> {
* @since 1.29.0
*/
// @formatter:on
- public static class Builder extends AbstractStreamBuilder<TarArchiveInputStream, Builder> {
+ public static class Builder extends ArchiveInputStream.Builder<TarArchiveInputStream, Builder> {
private int blockSize = TarConstants.DEFAULT_BLKSIZE;
private int recordSize = TarConstants.DEFAULT_RCDSIZE;
@@ -216,7 +215,11 @@ public static boolean matches(final byte[] signature, final int length) {
@SuppressWarnings("resource") // caller closes.
private TarArchiveInputStream(final Builder builder) throws IOException {
- super(builder.getInputStream(), builder.getCharset());
+ this(builder.getInputStream(), builder);
+ }
+
+ private TarArchiveInputStream(final InputStream inputStream, final Builder builder) {
+ super(inputStream, builder.getCharset());
this.zipEncoding = ZipEncodingHelper.getZipEncoding(builder.getCharset());
this.recordBuffer = new byte[builder.recordSize];
this.blockSize = builder.blockSize;
@@ -229,7 +232,7 @@ private TarArchiveInputStream(final Builder builder) throws IOException {
* @param inputStream the input stream to use.
*/
public TarArchiveInputStream(final InputStream inputStream) {
- this(inputStream, TarConstants.DEFAULT_BLKSIZE, TarConstants.DEFAULT_RCDSIZE);
+ this(inputStream, builder());
}
/**
@@ -243,7 +246,7 @@ public TarArchiveInputStream(final InputStream inputStream) {
*/
@Deprecated
public TarArchiveInputStream(final InputStream inputStream, final boolean lenient) {
- this(inputStream, TarConstants.DEFAULT_BLKSIZE, TarConstants.DEFAULT_RCDSIZE, null, lenient);
+ this(inputStream, builder().setLenient(lenient));
}
/**
@@ -255,7 +258,7 @@ public TarArchiveInputStream(final InputStream inputStream, final boolean lenien
*/
@Deprecated
public TarArchiveInputStream(final InputStream inputStream, final int blockSize) {
- this(inputStream, blockSize, TarConstants.DEFAULT_RCDSIZE);
+ this(inputStream, builder().setBlockSize(blockSize));
}
/**
@@ -268,7 +271,7 @@ public TarArchiveInputStream(final InputStream inputStream, final int blockSize)
*/
@Deprecated
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize) {
- this(inputStream, blockSize, recordSize, null);
+ this(inputStream, builder().setBlockSize(blockSize).setRecordSize(recordSize));
}
/**
@@ -282,8 +285,11 @@ public TarArchiveInputStream(final InputStream inputStream, final int blockSize,
* @deprecated Use {@link #builder()} to configure and {@link Builder#get() get()} a {@link Builder}.
*/
@Deprecated
- public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize, final String encoding) {
- this(inputStream, blockSize, recordSize, encoding, false);
+ public TarArchiveInputStream(
+ final InputStream inputStream, final int blockSize, final int recordSize, final String encoding) {
+ this(
+ inputStream,
+ builder().setBlockSize(blockSize).setRecordSize(recordSize).setCharset(encoding));
}
/**
@@ -299,12 +305,19 @@ public TarArchiveInputStream(final InputStream inputStream, final int blockSize,
* @deprecated Use {@link #builder()} to configure and {@link Builder#get() get()} a {@link Builder}.
*/
@Deprecated
- public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize, final String encoding, final boolean lenient) {
- super(inputStream, encoding);
- this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
- this.recordBuffer = new byte[recordSize];
- this.blockSize = blockSize;
- this.lenient = lenient;
+ public TarArchiveInputStream(
+ final InputStream inputStream,
+ final int blockSize,
+ final int recordSize,
+ final String encoding,
+ final boolean lenient) {
+ this(
+ inputStream,
+ builder()
+ .setBlockSize(blockSize)
+ .setRecordSize(recordSize)
+ .setCharset(encoding)
+ .setLenient(lenient));
}
/**
@@ -318,7 +331,7 @@ public TarArchiveInputStream(final InputStream inputStream, final int blockSize,
*/
@Deprecated
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final String encoding) {
- this(inputStream, blockSize, TarConstants.DEFAULT_RCDSIZE, encoding);
+ this(inputStream, builder().setBlockSize(blockSize).setCharset(encoding));
}
/**
@@ -331,7 +344,7 @@ public TarArchiveInputStream(final InputStream inputStream, final int blockSize,
*/
@Deprecated
public TarArchiveInputStream(final InputStream inputStream, final String encoding) {
- this(inputStream, TarConstants.DEFAULT_BLKSIZE, TarConstants.DEFAULT_RCDSIZE, encoding);
+ this(inputStream, builder().setCharset(encoding));
}
/**
diff --git a/src/test/java/org/apache/commons/compress/ChainingTest.java b/src/test/java/org/apache/commons/compress/ChainingTest.java
index e327076c2..3c6036a45 100644
--- a/src/test/java/org/apache/commons/compress/ChainingTest.java
+++ b/src/test/java/org/apache/commons/compress/ChainingTest.java
@@ -33,7 +33,9 @@ class ChainingTest extends AbstractTest {
@Test
void testTarBzip2() throws Exception {
- try (TarArchiveInputStream is = new TarArchiveInputStream(new BZip2CompressorInputStream(newInputStream("bla.tar.bz2")))) {
+ try (TarArchiveInputStream is = TarArchiveInputStream.builder()
+ .setInputStream(new BZip2CompressorInputStream(newInputStream("bla.tar.bz2")))
+ .get()) {
final TarArchiveEntry entry = is.getNextEntry();
assertNotNull(entry);
assertEquals("test1.xml", entry.getName());
@@ -43,7 +45,11 @@ void testTarBzip2() throws Exception {
@Test
void testTarGzip() throws Exception {
- try (TarArchiveInputStream is = new TarArchiveInputStream(new GzipCompressorInputStream(newInputStream("bla.tgz")))) {
+ try (TarArchiveInputStream is = TarArchiveInputStream.builder()
+ .setInputStream(GzipCompressorInputStream.builder()
+ .setURI(getURI("bla.tgz"))
+ .get())
+ .get()) {
final TarArchiveEntry entry = is.getNextEntry();
assertNotNull(entry);
assertEquals("test1.xml", entry.getName());
diff --git a/src/test/java/org/apache/commons/compress/DetectArchiverTest.java b/src/test/java/org/apache/commons/compress/DetectArchiverTest.java
index 492589ac9..4304d0bdc 100644
--- a/src/test/java/org/apache/commons/compress/DetectArchiverTest.java
+++ b/src/test/java/org/apache/commons/compress/DetectArchiverTest.java
@@ -197,7 +197,9 @@ void testIcoFile() {
@Test
void testIcoFileFirstTarArchiveEntry() throws Exception {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(createBufferedInputStream("org/apache/commons/compress/COMPRESS-644/ARW05UP.ICO"))) {
+ try (TarArchiveInputStream inputStream = TarArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/COMPRESS-644/ARW05UP.ICO"))
+ .get()) {
final TarArchiveEntry entry = inputStream.getNextEntry();
// Find hints that the file is not a TAR file.
assertNull(entry.getCreationTime());
diff --git a/src/test/java/org/apache/commons/compress/archivers/TarTest.java b/src/test/java/org/apache/commons/compress/archivers/TarTest.java
index 512df8842..3d68c3bec 100644
--- a/src/test/java/org/apache/commons/compress/archivers/TarTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/TarTest.java
@@ -27,7 +27,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -69,9 +68,10 @@ private byte[] createTarWithOneLongNameEntry(final String longName) throws IOExc
@Test
void testCOMPRESS114() throws Exception {
- final File input = getFile("COMPRESS-114.tar");
- try (InputStream is = Files.newInputStream(input.toPath());
- TarArchiveInputStream in = new TarArchiveInputStream(is, StandardCharsets.ISO_8859_1.name())) {
+ try (TarArchiveInputStream in = TarArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-114.tar"))
+ .setCharset(StandardCharsets.ISO_8859_1)
+ .get()) {
TarArchiveEntry entry = in.getNextEntry();
assertEquals("3\u00b1\u00b1\u00b1F06\u00b1W2345\u00b1ZB\u00b1la\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1\u00b1BLA", entry.getName());
assertEquals(TarConstants.LF_NORMAL, entry.getLinkFlag());
@@ -94,9 +94,10 @@ void testCOMPRESS178() throws Exception {
@Test
void testCOMPRESS178Lenient() throws Exception {
- final File input = getFile("COMPRESS-178-fail.tar");
- try (InputStream is = Files.newInputStream(input.toPath());
- ArchiveInputStream<?> in = new TarArchiveInputStream(is, true)) {
+ try (ArchiveInputStream<?> in = TarArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-178-fail.tar"))
+ .setLenient(true)
+ .get()) {
in.getNextEntry();
}
}
@@ -113,7 +114,7 @@ void testDirectoryEntryFromFile() throws Exception {
tos.closeArchiveEntry();
}
final TarArchiveEntry out;
- try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextTarEntry();
}
assertNotNull(out);
@@ -127,9 +128,8 @@ void testDirectoryEntryFromFile() throws Exception {
@Test
void testDirectoryRead() throws IOException {
- final File input = getFile("directory.tar");
- try (InputStream is = Files.newInputStream(input.toPath());
- TarArchiveInputStream in = new TarArchiveInputStream(is)) {
+ try (TarArchiveInputStream in =
+ TarArchiveInputStream.builder().setURI(getURI("directory.tar")).get()) {
final TarArchiveEntry directoryEntry = in.getNextTarEntry();
assertEquals("directory/", directoryEntry.getName());
assertEquals(TarConstants.LF_DIR, directoryEntry.getLinkFlag());
@@ -151,7 +151,7 @@ void testExplicitDirectoryEntry() throws Exception {
tos.closeArchiveEntry();
}
final TarArchiveEntry out;
- try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextTarEntry();
}
assertNotNull(out);
@@ -175,7 +175,7 @@ void testExplicitFileEntry() throws Exception {
outputStream.closeArchiveEntry();
}
final TarArchiveEntry entryOut;
- try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setFile(archive).get()) {
entryOut = tis.getNextTarEntry();
}
assertNotNull(entryOut);
@@ -197,7 +197,7 @@ void testFileEntryFromFile() throws Exception {
outputStream.closeArchiveEntry();
}
final TarArchiveEntry out;
- try (TarArchiveInputStream tis = new TarArchiveInputStream(Files.newInputStream(archive.toPath()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setFile(archive).get()) {
out = tis.getNextTarEntry();
}
assertNotNull(out);
@@ -216,8 +216,8 @@ void testLongNameLargerThanBuffer() throws IOException {
final String fileName = createLongName(length);
assertEquals(length.intValue(), fileName.length());
final byte[] data = createTarWithOneLongNameEntry(fileName);
- try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
- TarArchiveInputStream tis = new TarArchiveInputStream(bis)) {
+ try (TarArchiveInputStream tis =
+ TarArchiveInputStream.builder().setByteArray(data).get()) {
assertEquals(fileName, tis.getNextTarEntry().getName());
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/BigFilesIT.java b/src/test/java/org/apache/commons/compress/archivers/tar/BigFilesIT.java
index b4fe731ea..a647c2ed0 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/BigFilesIT.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/BigFilesIT.java
@@ -40,7 +40,8 @@ public class BigFilesIT extends AbstractTest {
private void readFileBiggerThan8GByte(final String name) throws Exception {
try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(name)));
GzipCompressorInputStream gzin = new GzipCompressorInputStream(in);
- TarArchiveInputStream tin = new TarArchiveInputStream(gzin)) {
+ TarArchiveInputStream tin =
+ TarArchiveInputStream.builder().setInputStream(gzin).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertEquals(8200L * 1024 * 1024, e.getSize());
@@ -77,7 +78,8 @@ void testReadFileBiggerThan8GByteStar() throws Exception {
void testReadFileHeadersOfArchiveBiggerThan8GByte() throws Exception {
try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath("8.posix.tar.gz")));
GzipCompressorInputStream gzin = new GzipCompressorInputStream(in);
- TarArchiveInputStream tin = new TarArchiveInputStream(gzin)) {
+ TarArchiveInputStream tin =
+ TarArchiveInputStream.builder().setInputStream(gzin).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertNull(tin.getNextTarEntry());
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/Compress700Test.java b/src/test/java/org/apache/commons/compress/archivers/tar/Compress700Test.java
index a778452a1..42bfe8459 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/Compress700Test.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/Compress700Test.java
@@ -27,9 +27,6 @@
import java.io.BufferedInputStream;
import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Arrays;
@@ -38,19 +35,20 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.junit.jupiter.api.Test;
/**
* Tests https://issues.apache.org/jira/browse/COMPRESS-699
*/
-class Compress700Test {
+class Compress700Test extends AbstractTest {
- private static final Path PATH = Paths.get("src/test/resources/org/apache/commons/compress/COMPRESS-700/flutter_awesome_buttons-0.1.0.tar");
+ private static final String RESOURCE_NAME = "org/apache/commons/compress/COMPRESS-700/flutter_awesome_buttons-0.1.0.tar";
@Test
void testFirstTarArchiveEntry() throws Exception {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(new BufferedInputStream(Files.newInputStream(PATH)))) {
+ try (TarArchiveInputStream inputStream = TarArchiveInputStream.builder().setURI(getURI(RESOURCE_NAME)).get()) {
final TarArchiveEntry entry = inputStream.getNextEntry();
assertNull(entry.getCreationTime());
assertEquals(-1, entry.getDataOffset());
@@ -175,7 +173,7 @@ void testListEntries() throws IOException {
new Object[] {9879, "README.md"},
new Object[] {433, "test/flutter_buttons_test.dart"});
// @formatter:on
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(new BufferedInputStream(Files.newInputStream(PATH)))) {
+ try (TarArchiveInputStream inputStream = TarArchiveInputStream.builder().setURI(getURI(RESOURCE_NAME)).get()) {
final AtomicInteger i = new AtomicInteger();
for (final Object[] pair : list) {
final TarArchiveEntry entry = inputStream.getNextEntry();
@@ -195,7 +193,7 @@ void testListEntries() throws IOException {
//@Ignore
@Test
void testTarArchive() throws Exception {
- try (BufferedInputStream fileInputStream = new BufferedInputStream(Files.newInputStream(PATH))) {
+ try (BufferedInputStream fileInputStream = new BufferedInputStream(newInputStream(RESOURCE_NAME))) {
assertEquals(ArchiveStreamFactory.TAR, ArchiveStreamFactory.detect(fileInputStream));
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/FileTimesIT.java b/src/test/java/org/apache/commons/compress/archivers/tar/FileTimesIT.java
index b38252359..fc03c7309 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/FileTimesIT.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/FileTimesIT.java
@@ -24,9 +24,6 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.BufferedInputStream;
-import java.io.InputStream;
-import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
@@ -49,8 +46,7 @@ private void assertGlobalHeaders(final TarArchiveEntry e) {
@Test
void testReadTimeFromTarEpax() throws Exception {
final String file = "COMPRESS-612/test-times-epax-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -79,8 +75,7 @@ void testReadTimeFromTarEpax() throws Exception {
@Test
void testReadTimeFromTarExustar() throws Exception {
final String file = "COMPRESS-612/test-times-exustar-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertEquals("test/", e.getName());
@@ -109,8 +104,7 @@ void testReadTimeFromTarExustar() throws Exception {
@Test
void testReadTimeFromTarGnu() throws Exception {
final String file = "COMPRESS-612/test-times-gnu.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -127,8 +121,7 @@ void testReadTimeFromTarGnu() throws Exception {
@Test
void testReadTimeFromTarGnuIncremental() throws Exception {
final String file = "COMPRESS-612/test-times-gnu-incremental.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -154,8 +147,7 @@ void testReadTimeFromTarGnuIncremental() throws Exception {
@Test
void testReadTimeFromTarGnuTar() throws Exception {
final String file = "COMPRESS-612/test-times-gnutar.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -171,8 +163,7 @@ void testReadTimeFromTarGnuTar() throws Exception {
@Test
void testReadTimeFromTarOldBsdTar() throws Exception {
final String file = "COMPRESS-612/test-times-oldbsdtar.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -189,8 +180,7 @@ void testReadTimeFromTarOldBsdTar() throws Exception {
@Test
void testReadTimeFromTarOldGnu() throws Exception {
final String file = "COMPRESS-612/test-times-oldgnu.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -207,8 +197,7 @@ void testReadTimeFromTarOldGnu() throws Exception {
@Test
void testReadTimeFromTarOldGnuIncremental() throws Exception {
final String file = "COMPRESS-612/test-times-oldgnu-incremental.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -234,8 +223,7 @@ void testReadTimeFromTarOldGnuIncremental() throws Exception {
@Test
void testReadTimeFromTarPax() throws Exception {
final String file = "COMPRESS-612/test-times-pax-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -265,8 +253,7 @@ void testReadTimeFromTarPax() throws Exception {
@Test
void testReadTimeFromTarPosix() throws Exception {
final String file = "COMPRESS-612/test-times-posix.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -283,8 +270,7 @@ void testReadTimeFromTarPosix() throws Exception {
@Test
void testReadTimeFromTarPosixLibArchive() throws Exception {
final String file = "COMPRESS-612/test-times-bsd-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -314,8 +300,7 @@ void testReadTimeFromTarPosixLibArchive() throws Exception {
@Test
void testReadTimeFromTarPosixLinux() throws Exception {
final String file = "COMPRESS-612/test-times-posix-linux.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -331,8 +316,7 @@ void testReadTimeFromTarPosixLinux() throws Exception {
@Test
void testReadTimeFromTarStarFolder() throws Exception {
final String file = "COMPRESS-612/test-times-star-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -360,8 +344,7 @@ void testReadTimeFromTarStarFolder() throws Exception {
@Test
void testReadTimeFromTarUstar() throws Exception {
final String file = "COMPRESS-612/test-times-ustar.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -377,8 +360,7 @@ void testReadTimeFromTarUstar() throws Exception {
@Test
void testReadTimeFromTarV7() throws Exception {
final String file = "COMPRESS-612/test-times-v7.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -394,8 +376,7 @@ void testReadTimeFromTarV7() throws Exception {
@Test
void testReadTimeFromTarXstar() throws Exception {
final String file = "COMPRESS-612/test-times-xstar.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -411,8 +392,7 @@ void testReadTimeFromTarXstar() throws Exception {
@Test
void testReadTimeFromTarXstarFolder() throws Exception {
final String file = "COMPRESS-612/test-times-xstar-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -440,8 +420,7 @@ void testReadTimeFromTarXstarFolder() throws Exception {
@Test
void testReadTimeFromTarXstarIncremental() throws Exception {
final String file = "COMPRESS-612/test-times-xstar-incremental.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -466,8 +445,7 @@ void testReadTimeFromTarXstarIncremental() throws Exception {
@Test
void testReadTimeFromTarXustar() throws Exception {
final String file = "COMPRESS-612/test-times-xustar.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -483,8 +461,7 @@ void testReadTimeFromTarXustar() throws Exception {
@Test
void testReadTimeFromTarXustarFolder() throws Exception {
final String file = "COMPRESS-612/test-times-xustar-folder.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
@@ -512,8 +489,7 @@ void testReadTimeFromTarXustarFolder() throws Exception {
@Test
void testReadTimeFromTarXustarIncremental() throws Exception {
final String file = "COMPRESS-612/test-times-xustar-incremental.tar";
- try (InputStream in = new BufferedInputStream(Files.newInputStream(getPath(file)));
- TarArchiveInputStream tin = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setURI(getURI(file)).get()) {
TarArchiveEntry e = tin.getNextTarEntry();
assertNotNull(e);
assertTrue(e.getExtraPaxHeaders().isEmpty());
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
index 9f609801c..779521617 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/SparseFilesTest.java
@@ -27,7 +27,6 @@
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
-import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -116,7 +115,8 @@ private String getTarBinaryHelp() throws IOException {
@Test
void testCompareTarArchiveInputStreamWithTarFile() throws IOException {
final Path file = getPath("oldgnu_sparse.tar");
- try (TarArchiveInputStream tarIn = new TarArchiveInputStream(new BufferedInputStream(Files.newInputStream(file)));
+ try (TarArchiveInputStream tarIn =
+ TarArchiveInputStream.builder().setPath(file).get();
TarFile tarFile = new TarFile(file)) {
assertNotNull(tarIn.getNextTarEntry());
try (InputStream inputStream = tarFile.getInputStream(tarFile.getEntries().get(0))) {
@@ -130,7 +130,7 @@ void testCompareTarArchiveInputStreamWithTarFile() throws IOException {
void testExtractExtendedOldGNU() throws IOException, InterruptedException {
final File file = getFile("oldgnu_extended_sparse.tar");
try (InputStream sparseFileInputStream = extractTarAndGetInputStream(file, "sparse6");
- TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ TarArchiveInputStream tin = TarArchiveInputStream.builder().setFile(file).get()) {
final TarArchiveEntry ae = tin.getNextTarEntry();
assertTrue(tin.canReadEntryData(ae));
@@ -168,7 +168,7 @@ void testExtractOldGNU() throws IOException, InterruptedException {
try {
final File file = getFile("oldgnu_sparse.tar");
try (InputStream sparseFileInputStream = extractTarAndGetInputStream(file, "sparsefile");
- TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ TarArchiveInputStream tin = TarArchiveInputStream.builder().setFile(file).get()) {
final TarArchiveEntry entry = tin.getNextTarEntry();
assertTrue(tin.canReadEntryData(entry));
assertArrayEquals(IOUtils.toByteArray(tin), IOUtils.toByteArray(sparseFileInputStream));
@@ -188,7 +188,7 @@ void testExtractPaxGNU() throws IOException, InterruptedException {
assumeFalse(getTarBinaryHelp().startsWith("tar (GNU tar) 1.28"), "This test should be ignored if GNU tar is version 1.28");
final File file = getFile("pax_gnu_sparse.tar");
- try (TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setFile(file).get()) {
TarArchiveEntry paxGNUEntry = tin.getNextTarEntry();
assertTrue(tin.canReadEntryData(paxGNUEntry));
@@ -215,12 +215,12 @@ void testExtractPaxGNU() throws IOException, InterruptedException {
void testExtractSparseTarsOnWindows() throws IOException {
final File oldGNUSparseTar = getFile("oldgnu_sparse.tar");
final File paxGNUSparseTar = getFile("pax_gnu_sparse.tar");
- try (TarArchiveInputStream paxGNUSparseInputStream = new TarArchiveInputStream(Files.newInputStream(paxGNUSparseTar.toPath()))) {
+ try (TarArchiveInputStream paxGNUSparseInputStream = TarArchiveInputStream.builder().setFile(paxGNUSparseTar).get()) {
// compare between old GNU and PAX 0.0
TarArchiveEntry paxGNUEntry = paxGNUSparseInputStream.getNextTarEntry();
assertTrue(paxGNUSparseInputStream.canReadEntryData(paxGNUEntry));
- try (TarArchiveInputStream oldGNUSparseInputStream = new TarArchiveInputStream(Files.newInputStream(oldGNUSparseTar.toPath()))) {
+ try (TarArchiveInputStream oldGNUSparseInputStream = TarArchiveInputStream.builder().setFile(oldGNUSparseTar).get()) {
final TarArchiveEntry oldGNUEntry = oldGNUSparseInputStream.getNextTarEntry();
assertTrue(oldGNUSparseInputStream.canReadEntryData(oldGNUEntry));
assertArrayEquals(IOUtils.toByteArray(oldGNUSparseInputStream), IOUtils.toByteArray(paxGNUSparseInputStream));
@@ -229,7 +229,7 @@ void testExtractSparseTarsOnWindows() throws IOException {
// compare between old GNU and PAX 0.1
paxGNUEntry = paxGNUSparseInputStream.getNextTarEntry();
assertTrue(paxGNUSparseInputStream.canReadEntryData(paxGNUEntry));
- try (TarArchiveInputStream oldGNUSparseInputStream = new TarArchiveInputStream(Files.newInputStream(oldGNUSparseTar.toPath()))) {
+ try (TarArchiveInputStream oldGNUSparseInputStream = TarArchiveInputStream.builder().setFile(oldGNUSparseTar).get()) {
final TarArchiveEntry oldGNUEntry = oldGNUSparseInputStream.getNextTarEntry();
assertTrue(oldGNUSparseInputStream.canReadEntryData(oldGNUEntry));
assertArrayEquals(IOUtils.toByteArray(oldGNUSparseInputStream), IOUtils.toByteArray(paxGNUSparseInputStream));
@@ -238,7 +238,7 @@ void testExtractSparseTarsOnWindows() throws IOException {
// compare between old GNU and PAX 1.0
paxGNUEntry = paxGNUSparseInputStream.getNextTarEntry();
assertTrue(paxGNUSparseInputStream.canReadEntryData(paxGNUEntry));
- try (TarArchiveInputStream oldGNUSparseInputStream = new TarArchiveInputStream(Files.newInputStream(oldGNUSparseTar.toPath()))) {
+ try (TarArchiveInputStream oldGNUSparseInputStream = TarArchiveInputStream.builder().setFile(oldGNUSparseTar).get()) {
final TarArchiveEntry oldGNUEntry = oldGNUSparseInputStream.getNextTarEntry();
assertTrue(oldGNUSparseInputStream.canReadEntryData(oldGNUEntry));
assertArrayEquals(IOUtils.toByteArray(oldGNUSparseInputStream), IOUtils.toByteArray(paxGNUSparseInputStream));
@@ -248,8 +248,9 @@ void testExtractSparseTarsOnWindows() throws IOException {
@Test
void testOldGNU() throws Throwable {
- final File file = getFile("oldgnu_sparse.tar");
- try (TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder()
+ .setURI(getURI("oldgnu_sparse.tar"))
+ .get()) {
final TarArchiveEntry ae = tin.getNextTarEntry();
assertEquals("sparsefile", ae.getName());
assertEquals(TarConstants.LF_GNUTYPE_SPARSE, ae.getLinkFlag());
@@ -289,8 +290,9 @@ void testOldGNU() throws Throwable {
@Test
void testPaxGNU() throws Throwable {
- final File file = getFile("pax_gnu_sparse.tar");
- try (TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(file.toPath()))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder()
+ .setURI(getURI("pax_gnu_sparse.tar"))
+ .get()) {
assertPaxGNUEntry(tin, "0.0");
assertPaxGNUEntry(tin, "0.1");
assertPaxGNUEntry(tin, "1.0");
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
index 6d15d283a..7cf84ba26 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveEntryTest.java
@@ -30,7 +30,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -100,7 +99,7 @@ void testExtraPaxHeaders() throws IOException {
assertNotEquals(0, entry.getExtraPaxHeaders().size(), "should have extra headers before clear");
entry.clearExtraPaxHeaders();
assertEquals(0, entry.getExtraPaxHeaders().size(), "extra headers should be empty after clear");
- try (TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get()) {
entry = tis.getNextTarEntry();
assertNotNull(entry, "couldn't get entry");
@@ -304,7 +303,7 @@ void testShouldNotWriteTimePaxHeadersByDefault() throws IOException {
tos.write('W');
tos.closeArchiveEntry();
}
- try (TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get()) {
final TarArchiveEntry entry = tis.getNextTarEntry();
assertNotNull(entry, "couldn't get entry");
@@ -351,7 +350,7 @@ void testShouldWriteTimesAsPaxHeadersForPosixMode() throws IOException {
tos.write('W');
tos.closeArchiveEntry();
}
- try (TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get()) {
final TarArchiveEntry entry = tis.getNextTarEntry();
assertNotNull(entry, "couldn't get entry");
@@ -383,7 +382,7 @@ void testShouldWriteTimesAsPaxHeadersForPosixModeAndCreationTimeShouldBeUsedAsCt
tos.write('W');
tos.closeArchiveEntry();
}
- try (TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get()) {
final TarArchiveEntry entry = tis.getNextTarEntry();
assertNotNull(entry, "couldn't get entry");
@@ -414,7 +413,7 @@ void testShouldWriteTimesForStarMode() throws IOException {
tos.write('W');
tos.closeArchiveEntry();
}
- try (TarArchiveInputStream tis = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get()) {
final TarArchiveEntry entry = tis.getNextTarEntry();
assertNotNull(entry, "couldn't get entry");
@@ -460,7 +459,7 @@ void testTarFileWithFSRoot() throws IOException {
tout.write(new byte[] { '!' });
tout.closeArchiveEntry();
}
- try (TarArchiveInputStream tin = new TarArchiveInputStream(Files.newInputStream(f.toPath()))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setFile(f).get()) {
// tin.setDebug(true);
entry = tin.getNextTarEntry();
assertNotNull(entry);
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
index a95b164c6..834f3ed63 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java
@@ -40,7 +40,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
@@ -70,7 +69,7 @@
class TarArchiveInputStreamTest extends AbstractTest {
private void datePriorToEpoch(final String archive) throws Exception {
- try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(getFile(archive).toPath()))) {
+ try (TarArchiveInputStream in = getTestStream(archive)) {
final TarArchiveEntry tae = in.getNextTarEntry();
assertEquals("foo", tae.getName());
assertEquals(TarConstants.LF_NORMAL, tae.getLinkFlag());
@@ -88,14 +87,18 @@ private void getNextEntryUntilIOException(final TarArchiveInputStream archive) {
}
@SuppressWarnings("resource") // Caller closes
- private TarArchiveInputStream getTestStream(final String name) {
- return new TarArchiveInputStream(TarArchiveInputStreamTest.class.getResourceAsStream(name));
+ private static TarArchiveInputStream getTestStream(final String name) throws IOException {
+ return TarArchiveInputStream.builder()
+ .setURI(getURI(name))
+ .get();
}
@Test
void testChecksumOnly4Byte() throws IOException {
- try (InputStream in = newInputStream("org/apache/commons/compress/COMPRESS-707/COMPRESS-707-lenient.tar");
- TarArchiveInputStream archive = TarArchiveInputStream.builder().setInputStream(in).setLenient(true).get()) {
+ try (TarArchiveInputStream archive = TarArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/COMPRESS-707/COMPRESS-707-lenient.tar"))
+ .setLenient(true)
+ .get()) {
final TarArchiveEntry nextEntry = archive.getNextEntry();
assertNotNull(nextEntry);
assertEquals("hi-gary.txt", nextEntry.getName());
@@ -105,7 +108,7 @@ void testChecksumOnly4Byte() throws IOException {
@Test
void testCompress197() throws IOException {
- try (TarArchiveInputStream tar = getTestStream("/COMPRESS-197.tar")) {
+ try (TarArchiveInputStream tar = getTestStream("COMPRESS-197.tar")) {
TarArchiveEntry entry = tar.getNextTarEntry();
assertNotNull(entry);
while (entry != null) {
@@ -117,7 +120,7 @@ void testCompress197() throws IOException {
@Test
void testCompress197ForEach() throws IOException {
- try (TarArchiveInputStream tar = getTestStream("/COMPRESS-197.tar")) {
+ try (TarArchiveInputStream tar = getTestStream("COMPRESS-197.tar")) {
tar.forEach(IOConsumer.noop());
}
}
@@ -144,8 +147,8 @@ void testCompress558() throws IOException {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
- TarArchiveInputStream tis = new TarArchiveInputStream(bis)) {
+ try (TarArchiveInputStream tis =
+ TarArchiveInputStream.builder().setByteArray(data).get()) {
assertEquals(folderName, tis.getNextTarEntry().getName());
assertEquals(TarConstants.LF_DIR, tis.getCurrentEntry().getLinkFlag());
assertEquals(consumerJavaName, tis.getNextTarEntry().getName());
@@ -268,7 +271,7 @@ void testDirectoryWithLongNameEndsWithSlash() throws IOException, ArchiveExcepti
@Test
void testGetAndSetOfPaxEntry() throws Exception {
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-356.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-356.tar")) {
final TarArchiveEntry entry = is.getNextTarEntry();
assertEquals("package/package.json", entry.getName());
assertEquals(TarConstants.LF_NORMAL, entry.getLinkFlag());
@@ -293,8 +296,7 @@ void testGetAndSetOfPaxEntry() throws Exception {
*/
@Test
void testGetNextEntry() throws IOException {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/tar/getNextTarEntry.bin")))) {
+ try (TarArchiveInputStream inputStream = getTestStream("org/apache/commons/compress/tar/getNextTarEntry.bin")) {
final AtomicLong count = new AtomicLong();
final TarArchiveEntry entry = inputStream.getNextEntry();
assertNull(entry.getCreationTime());
@@ -331,8 +333,7 @@ void testGetNextEntry() throws IOException {
*/
@Test
void testGetNextTarEntryDeprecated() throws IOException {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/tar/getNextTarEntry.bin")))) {
+ try (TarArchiveInputStream inputStream = getTestStream("org/apache/commons/compress/tar/getNextTarEntry.bin")) {
final AtomicLong count = new AtomicLong();
final TarArchiveEntry entry = inputStream.getNextTarEntry();
assertNull(entry.getCreationTime());
@@ -367,8 +368,7 @@ void testGetNextTarEntryDeprecated() throws IOException {
@Test
void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("bla.tar")) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read(buf));
@@ -378,32 +378,28 @@ void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
@Test
void testParseTarTruncatedInContent() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-544_truncated_in_content-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-544_truncated_in_content-fail.tar")) {
getNextEntryUntilIOException(archive);
}
}
@Test
void testParseTarTruncatedInPadding() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-544_truncated_in_padding-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-544_truncated_in_padding-fail.tar")) {
getNextEntryUntilIOException(archive);
}
}
@Test
void testParseTarWithNonNumberPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-529-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-529-fail.tar")) {
assertThrows(ArchiveException.class, () -> archive.getNextEntry());
}
}
@Test
void testParseTarWithSpecialPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-530-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-530-fail.tar")) {
assertThrows(ArchiveException.class, () -> archive.getNextEntry());
assertThrows(ArchiveException.class, () -> IOUtils.toByteArray(archive));
}
@@ -414,8 +410,7 @@ void testParseTarWithSpecialPaxHeaders() throws IOException {
*/
@Test
void testPaxHeaders() throws IOException {
- try (TarArchiveInputStream inputStream = new TarArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/tar/paxHeaders.bin")))) {
+ try (TarArchiveInputStream inputStream = getTestStream("org/apache/commons/compress/tar/paxHeaders.bin")) {
assertThrows(ArchiveException.class, inputStream::getNextEntry);
}
}
@@ -424,7 +419,8 @@ void testPaxHeaders() throws IOException {
void testReadsArchiveCompletely_COMPRESS245() {
try (InputStream is = TarArchiveInputStreamTest.class.getResourceAsStream("/COMPRESS-245.tar.gz")) {
final InputStream gin = new GZIPInputStream(is);
- try (TarArchiveInputStream tar = new TarArchiveInputStream(gin)) {
+ try (TarArchiveInputStream tar =
+ TarArchiveInputStream.builder().setInputStream(gin).get()) {
int count = 0;
TarArchiveEntry entry = tar.getNextTarEntry();
while (entry != null) {
@@ -440,8 +436,7 @@ void testReadsArchiveCompletely_COMPRESS245() {
@Test
void testRejectsArchivesWithNegativeSizes() throws Exception {
- try (InputStream in = newInputStream("COMPRESS-569-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-569-fail.tar")) {
getNextEntryUntilIOException(archive);
}
}
@@ -452,7 +447,7 @@ void testRejectsArchivesWithNegativeSizes() throws Exception {
@Test
void testShouldConsumeArchiveCompletely() throws Exception {
try (InputStream is = TarArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.tar");
- TarArchiveInputStream tar = new TarArchiveInputStream(is)) {
+ TarArchiveInputStream tar = TarArchiveInputStream.builder().setInputStream(is).get()) {
while (tar.getNextTarEntry() != null) {
// just consume the archive
}
@@ -476,8 +471,8 @@ void testShouldReadBigGid() throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- final ByteArrayInputStream bis = new ByteArrayInputStream(data);
- try (TarArchiveInputStream tis = new TarArchiveInputStream(bis)) {
+ try (TarArchiveInputStream tis =
+ TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry t = tis.getNextTarEntry();
assertEquals(4294967294L, t.getLongGroupId());
}
@@ -488,7 +483,7 @@ void testShouldReadBigGid() throws Exception {
*/
@Test
void testShouldReadGNULongNameEntryWithWrongName() throws Exception {
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-324.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-324.tar")) {
final TarArchiveEntry entry = is.getNextTarEntry();
assertEquals(
"1234567890123456789012345678901234567890123456789012345678901234567890"
@@ -501,7 +496,7 @@ void testShouldReadGNULongNameEntryWithWrongName() throws Exception {
@Test
void testShouldThrowAnExceptionOnTruncatedEntries() throws Exception {
final Path dir = createTempDirectory("COMPRESS-279");
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-279-fail.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-279-fail.tar")) {
assertThrows(ArchiveException.class, () -> {
TarArchiveEntry entry = is.getNextTarEntry();
int count = 0;
@@ -517,7 +512,7 @@ void testShouldThrowAnExceptionOnTruncatedEntries() throws Exception {
@Test
void testShouldThrowAnExceptionOnTruncatedStream() throws Exception {
final Path dir = createTempDirectory("COMPRESS-279");
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-279-fail.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-279-fail.tar")) {
final AtomicInteger count = new AtomicInteger();
assertThrows(ArchiveException.class, () -> is.forEach(entry -> Files.copy(is, dir.resolve(String.valueOf(count.getAndIncrement())))));
}
@@ -538,7 +533,10 @@ void testShouldUseSpecifiedEncodingWhenReadingGNULongNames() throws Exception {
}
final byte[] data = bos.toByteArray();
final ByteArrayInputStream bis = new ByteArrayInputStream(data);
- try (TarArchiveInputStream tis = new TarArchiveInputStream(bis, encoding)) {
+ try (TarArchiveInputStream tis = TarArchiveInputStream.builder()
+ .setByteArray(data)
+ .setCharset(encoding)
+ .get()) {
final TarArchiveEntry t = tis.getNextTarEntry();
assertEquals(name, t.getName());
}
@@ -546,8 +544,7 @@ void testShouldUseSpecifiedEncodingWhenReadingGNULongNames() throws Exception {
@Test
void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
- try (InputStream in = newInputStream("bla.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("bla.tar")) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());
@@ -560,7 +557,7 @@ void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
*/
@Test
void testSkipsDevNumbersWhenEntryIsNoDevice() throws Exception {
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-417.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-417.tar")) {
assertEquals("test1.xml", is.getNextTarEntry().getName());
assertEquals(TarConstants.LF_NORMAL, is.getCurrentEntry().getLinkFlag());
assertEquals("test2.xml", is.getNextTarEntry().getName());
@@ -574,7 +571,7 @@ void testSkipsDevNumbersWhenEntryIsNoDevice() throws Exception {
*/
@Test
void testSurvivesBlankLinesInPaxHeader() throws Exception {
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-355.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-355.tar")) {
final TarArchiveEntry entry = is.getNextTarEntry();
assertEquals("package/package.json", entry.getName());
assertEquals(TarConstants.LF_NORMAL, entry.getLinkFlag());
@@ -587,7 +584,7 @@ void testSurvivesBlankLinesInPaxHeader() throws Exception {
*/
@Test
void testSurvivesPaxHeaderWithNameEndingInSlash() throws Exception {
- try (TarArchiveInputStream is = getTestStream("/COMPRESS-356.tar")) {
+ try (TarArchiveInputStream is = getTestStream("COMPRESS-356.tar")) {
final TarArchiveEntry entry = is.getNextTarEntry();
assertEquals("package/package.json", entry.getName());
assertEquals(TarConstants.LF_NORMAL, entry.getLinkFlag());
@@ -597,23 +594,21 @@ void testSurvivesPaxHeaderWithNameEndingInSlash() throws Exception {
@Test
void testThrowException() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-553-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-553-fail.tar")) {
getNextEntryUntilIOException(archive);
}
}
@Test
void testThrowExceptionWithNullEntry() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-554-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = getTestStream("COMPRESS-554-fail.tar")) {
getNextEntryUntilIOException(archive);
}
}
@Test
void testWorkaroundForBrokenTimeHeader() throws Exception {
- try (TarArchiveInputStream in = new TarArchiveInputStream(newInputStream("simple-aix-native-tar.tar"))) {
+ try (TarArchiveInputStream in = getTestStream("simple-aix-native-tar.tar")) {
TarArchiveEntry tae = in.getNextTarEntry();
tae = in.getNextTarEntry();
assertEquals("sample/link-to-txt-file.lnk", tae.getName());
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
index db6a9e174..137f9eb1e 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStreamTest.java
@@ -28,7 +28,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@@ -109,7 +108,7 @@ void testBigNumberPosixMode() throws Exception {
final byte[] data = bos.toByteArray();
assertEquals("00000000000 ",
new String(data, 1024 + TarConstants.NAMELEN + TarConstants.MODELEN + TarConstants.UIDLEN + TarConstants.GIDLEN, 12, UTF_8));
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(0100000000000L, e.getSize());
}
@@ -130,7 +129,7 @@ void testBigNumberStarMode() throws Exception {
tos.write(new byte[10 * 1024]);
final byte[] data = bos.toByteArray();
assertEquals(0x80, data[TarConstants.NAMELEN + TarConstants.MODELEN + TarConstants.UIDLEN + TarConstants.GIDLEN] & 0x80);
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(0100000000000L, e.getSize());
}
@@ -209,7 +208,7 @@ void testLongNameMd5Hash() throws Exception {
// do I still have the correct modification date?
// let a second elapse, so we don't get the current time
Thread.sleep(1000);
- try (TarArchiveInputStream tarIn = new TarArchiveInputStream(new ByteArrayInputStream(archive2))) {
+ try (TarArchiveInputStream tarIn = TarArchiveInputStream.builder().setByteArray(archive2).get()) {
final ArchiveEntry nextEntry = tarIn.getNextEntry();
assertEquals(longFileName, nextEntry.getName());
// tar archive stores modification time to second granularity only (floored)
@@ -252,7 +251,7 @@ void testOldEntryPosixMode() throws Exception {
final byte[] data = bos.toByteArray();
assertEquals("00000000000 ", new String(data,
1024 + TarConstants.NAMELEN + TarConstants.MODELEN + TarConstants.UIDLEN + TarConstants.GIDLEN + TarConstants.SIZELEN, 12, UTF_8));
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
final Calendar cal = Calendar.getInstance(TimeZones.GMT);
cal.set(1969, 11, 31, 23, 59, 59);
@@ -277,7 +276,7 @@ void testOldEntryStarMode() throws Exception {
tos.write(new byte[10 * 1024]);
final byte[] data = bos.toByteArray();
assertEquals((byte) 0xff, data[TarConstants.NAMELEN + TarConstants.MODELEN + TarConstants.UIDLEN + TarConstants.GIDLEN + TarConstants.SIZELEN]);
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
final Calendar cal = Calendar.getInstance(TimeZones.GMT);
cal.set(1969, 11, 31, 23, 59, 59);
@@ -359,7 +358,8 @@ void testPutGlobalPaxHeaderEntry() throws IOException {
tos.write(y.getBytes());
tos.closeArchiveEntry();
}
- final TarArchiveInputStream in = new TarArchiveInputStream(new ByteArrayInputStream(bos.toByteArray()));
+ final TarArchiveInputStream in =
+ TarArchiveInputStream.builder().setByteArray(bos.toByteArray()).get();
TarArchiveEntry entryIn = in.getNextTarEntry();
assertNotNull(entryIn);
assertEquals("message", entryIn.getName());
@@ -403,7 +403,7 @@ private void testRoundtripWith67CharFileName(final int mode) throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
assertEquals(n, tin.getNextTarEntry().getName());
}
}
@@ -435,7 +435,7 @@ private void testWriteLongDirectoryName(final int mode) throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n, e.getName());
assertTrue(e.isDirectory());
@@ -491,7 +491,7 @@ void testWriteLongDirectoryNameTruncateMode() throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n.substring(0, TarConstants.NAMELEN) + "/", e.getName(), "Entry name");
assertEquals(TarConstants.LF_DIR, e.getLinkFlag());
@@ -516,7 +516,7 @@ void testWriteLongFileNamePosixMode() throws Exception {
tos.closeArchiveEntry();
final byte[] data = bos.toByteArray();
assertEquals("160 path=" + n + "\n", new String(data, 512, 160, UTF_8));
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
assertEquals(n, tin.getNextTarEntry().getName());
assertEquals(TarConstants.LF_NORMAL, tin.getCurrentEntry().getLinkFlag());
}
@@ -549,7 +549,7 @@ private void testWriteLongLinkName(final int mode) throws Exception {
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals("test", e.getName(), "Entry name");
assertEquals(linkName, e.getLinkName(), "Link name");
@@ -609,7 +609,7 @@ void testWriteLongLinkNameTruncateMode() throws Exception {
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(linkName.substring(0, TarConstants.NAMELEN), e.getLinkName(), "Link name");
assertEquals(TarConstants.LF_SYMLINK, e.getLinkFlag(), "Link flag");
@@ -630,7 +630,7 @@ void testWriteNonAsciiDirectoryNamePosixMode() throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n, e.getName());
assertEquals(TarConstants.LF_DIR, e.getLinkFlag());
@@ -653,7 +653,7 @@ void testWriteNonAsciiLinkPathNamePaxHeader() throws Exception {
}
final byte[] data = bos.toByteArray();
assertEquals("15 linkpath=" + n + "\n", new String(data, 512, 15, UTF_8));
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n, e.getLinkName());
assertEquals(TarConstants.LF_LINK, e.getLinkFlag(), "Link flag");
@@ -674,7 +674,7 @@ void testWriteNonAsciiNameWithUnfortunateNamePosixMode() throws Exception {
tos.closeArchiveEntry();
}
final byte[] data = bos.toByteArray();
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n, e.getName());
assertEquals(TarConstants.LF_NORMAL, e.getLinkFlag());
@@ -696,7 +696,7 @@ void testWriteNonAsciiPathNamePaxHeader() throws Exception {
}
final byte[] data = bos.toByteArray();
assertEquals("11 path=" + n + "\n", new String(data, 512, 11, UTF_8));
- try (TarArchiveInputStream tin = new TarArchiveInputStream(new ByteArrayInputStream(data))) {
+ try (TarArchiveInputStream tin = TarArchiveInputStream.builder().setByteArray(data).get()) {
final TarArchiveEntry e = tin.getNextTarEntry();
assertEquals(n, e.getName());
assertEquals(TarConstants.LF_NORMAL, e.getLinkFlag());
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
index 448ee34aa..a049a5f21 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarLister.java
@@ -19,10 +19,7 @@
package org.apache.commons.compress.archivers.tar;
-import java.io.BufferedInputStream;
import java.io.File;
-import java.io.InputStream;
-import java.nio.file.Files;
/**
* Simple command line application that lists the contents of a tar archive.
@@ -87,8 +84,12 @@ public static void main(final String[] args) throws Exception {
if (!f.isFile()) {
System.err.println(f + " doesn't exist or is a directory");
}
- try (InputStream fis = new BufferedInputStream(Files.newInputStream(f.toPath()));
- TarArchiveInputStream ais = args.length > 1 ? new TarArchiveInputStream(fis, args[1]) : new TarArchiveInputStream(fis)) {
+ TarArchiveInputStream.Builder tarBuilder =
+ TarArchiveInputStream.builder().setFile(f);
+ if (args.length > 1) {
+ tarBuilder = tarBuilder.setCharset(args[1]);
+ }
+ try (TarArchiveInputStream ais = tarBuilder.get()) {
System.out.println("Created " + ais);
TarArchiveEntry ae;
while ((ae = ais.getNextTarEntry()) != null) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarMemoryFileSystemTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarMemoryFileSystemTest.java
index cf444cd98..1763fc493 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarMemoryFileSystemTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarMemoryFileSystemTest.java
@@ -25,7 +25,6 @@
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileSystem;
import java.nio.file.Files;
@@ -63,8 +62,8 @@ void testCheckUserInformationInTarEntry() throws IOException, ArchiveException {
tarOut.closeArchiveEntry();
}
- try (InputStream input = Files.newInputStream(target);
- TarArchiveInputStream tarIn = new TarArchiveInputStream(input)) {
+ try (TarArchiveInputStream tarIn =
+ TarArchiveInputStream.builder().setPath(target).get()) {
final TarArchiveEntry nextTarEntry = tarIn.getNextTarEntry();
assertEquals(user, nextTarEntry.getUserName());
diff --git a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
index 8b3a630d6..a3996f9b8 100644
--- a/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/tar/TarUtilsTest.java
@@ -389,8 +389,9 @@ void testParseSparse() {
@Test
void testParseTarWithSpecialPaxHeaders() throws IOException {
- try (InputStream in = newInputStream("COMPRESS-530-fail.tar");
- TarArchiveInputStream archive = new TarArchiveInputStream(in)) {
+ try (TarArchiveInputStream archive = TarArchiveInputStream.builder()
+ .setURI(getURI("COMPRESS-530-fail.tar"))
+ .get()) {
assertThrows(ArchiveException.class, () -> archive.getNextEntry());
// IOUtils.toByteArray(archive);
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java
index 4c8af21b8..67301ec88 100644
--- a/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStreamTest.java
@@ -101,7 +101,7 @@ void testCompress705() throws IOException {
assertEquals("Helm", gis.getMetaData().getComment());
assertEquals(41, gis.getMetaData().getExtraFieldXlen());
final List<String> fileNames = new ArrayList<>();
- try (TarArchiveInputStream tar = new TarArchiveInputStream(gis)) {
+ try (TarArchiveInputStream tar = TarArchiveInputStream.builder().setInputStream(gis).get()) {
TarArchiveEntry entry;
while ((entry = tar.getNextEntry()) != null) {
fileNames.add(entry.getName());
diff --git a/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
index 612bfdf0e..a08842234 100644
--- a/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
@@ -90,9 +90,8 @@ void testCompress675() throws Exception {
//
// If you use a zip archive instead of a tar archive you
// get a different number of bytes read, but still not the expected
- try (InputStream is = new FileInputStream(archiveFile);
- // Files.newInputStream(archiveFile.toPath());
- TarArchiveInputStream in = new TarArchiveInputStream(is)) {
+ try (TarArchiveInputStream in =
+ TarArchiveInputStream.builder().setFile(archiveFile).get()) {
ArchiveEntry entry = in.getNextEntry();
int entries = 0;
long count = 0;
(commons-compress) 08/08: docs: add changelog entry
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit e5e9dd8dc39d454716a0a564ea333adab3cb9edc
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 22:44:00 2025 +0200
docs: add changelog entry
---
src/changes/changes.xml | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a08753ef7..38d176f1b 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -115,8 +115,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" issue="COMPRESS-705" dev="ggregory" due-to="Gary Gregory">Add GzipCompressorInputStream.Builder.setIgnoreExtraField(boolean).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SnappyCompressorInputStream.getUncompressedSize() and deprecate getSize().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArchiveInputStream.ArchiveInputStream(InputStream, Charset) as a public constructor, it was private.</action>
- <action type="add" dev="ggregory" due-to="Gary Gregory">Add TarArchiveInputStream.builder().</action>
- <action type="add" dev="ggregory" due-to="Gary Gregory">Add TarArchiveInputStream.Builder and deprecate all but one constructor.</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory, Piotr P. Karwasz">Introduce builders for all ArchiveInputStream implementations.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 85 to 87.</action>
</release>
(commons-compress) 05/08: feat: add builder for `DumpArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 712833837b9ef53f10d10eac5b48c5d80d3ad941
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 19:00:50 2025 +0200
feat: add builder for `DumpArchiveInputStream`
Introduce a builder for `DumpArchiveInputStream` and refactor existing
constructor calls to use it.
---
.../compress/archivers/ArchiveStreamFactory.java | 5 +-
.../archivers/dump/DumpArchiveInputStream.java | 47 ++++++++++-
.../archivers/ArchiveStreamFactoryTest.java | 3 +-
.../commons/compress/archivers/DumpTest.java | 4 +-
.../archivers/dump/DumpArchiveInputStreamTest.java | 92 ++++++++++++----------
.../archivers/dump/TapeInputStreamTest.java | 12 ++-
6 files changed, 107 insertions(+), 56 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index 64b76288f..d80a2766f 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -465,10 +465,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) cpioBuilder.get();
}
if (DUMP.equalsIgnoreCase(archiverName)) {
+ final DumpArchiveInputStream.Builder dumpBuilder = DumpArchiveInputStream.builder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new DumpArchiveInputStream(in, actualEncoding);
+ dumpBuilder.setCharset(actualEncoding);
}
- return (I) new DumpArchiveInputStream(in);
+ return (I) dumpBuilder.get();
}
if (SEVEN_Z.equalsIgnoreCase(archiverName)) {
throw new StreamingNotSupportedException(SEVEN_Z);
diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
index bc6e9a8c5..b81432229 100644
--- a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java
@@ -47,6 +47,29 @@
*/
public class DumpArchiveInputStream extends ArchiveInputStream<DumpArchiveEntry> {
+ /**
+ * Builds a new {@link DumpArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * DumpArchiveInputStream in = DumpArchiveInputStream.builder()
+ * .setPath(inputPath)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static final class Builder extends ArchiveInputStream.Builder<DumpArchiveInputStream, Builder> {
+
+ private Builder() {
+ }
+
+ @Override
+ public DumpArchiveInputStream get() throws IOException {
+ return new DumpArchiveInputStream(this);
+ }
+ }
+
private static final String CURRENT_PATH_SEGMENT = ".";
private static final String PARENT_PATH_SEGMENT = "..";
@@ -73,6 +96,16 @@ public static boolean matches(final byte[] buffer, final int length) {
return DumpArchiveConstants.NFS_MAGIC == DumpArchiveUtil.convert32(buffer, 24);
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
private final DumpArchiveSummary summary;
private DumpArchiveEntry active;
private boolean isClosed;
@@ -111,7 +144,7 @@ public static boolean matches(final byte[] buffer, final int length) {
* @throws ArchiveException on error
*/
public DumpArchiveInputStream(final InputStream is) throws ArchiveException {
- this(is, null);
+ this(is, builder());
}
/**
@@ -123,10 +156,18 @@ public DumpArchiveInputStream(final InputStream is) throws ArchiveException {
* @since 1.6
*/
public DumpArchiveInputStream(final InputStream is, final String encoding) throws ArchiveException {
- super(is, encoding);
+ this(is, builder().setCharset(encoding));
+ }
+
+ private DumpArchiveInputStream(final Builder builder) throws IOException {
+ this(builder.getInputStream(), builder);
+ }
+
+ private DumpArchiveInputStream(final InputStream is, final Builder builder) throws ArchiveException {
+ super(is, builder.getCharset());
this.raw = new TapeInputStream(is);
this.hasHitEOF = false;
- this.zipEncoding = ZipEncodingHelper.getZipEncoding(encoding);
+ this.zipEncoding = ZipEncodingHelper.getZipEncoding(builder.getCharset());
try {
// read header, verify it's a dump archive.
diff --git a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
index a0c9ba083..47d04f357 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
@@ -111,7 +111,8 @@ public String toString() {
}
ARJ_DEFAULT = dflt;
dflt = UNKNOWN;
- try (DumpArchiveInputStream inputStream = new DumpArchiveInputStream(newInputStream("bla.dump"))) {
+ try (DumpArchiveInputStream inputStream =
+ DumpArchiveInputStream.builder().setURI(getURI("bla.dump")).get()) {
dflt = getCharsetName(inputStream);
} catch (final Exception e) {
e.printStackTrace();
diff --git a/src/test/java/org/apache/commons/compress/archivers/DumpTest.java b/src/test/java/org/apache/commons/compress/archivers/DumpTest.java
index 54a9b6829..5b3ced3af 100644
--- a/src/test/java/org/apache/commons/compress/archivers/DumpTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/DumpTest.java
@@ -46,8 +46,8 @@ private void checkDumpArchive(final File f) throws Exception {
expected.add("lost+found/");
expected.add("test1.xml");
expected.add("test2.xml");
- try (InputStream is = Files.newInputStream(f.toPath());
- DumpArchiveInputStream inputStream = new DumpArchiveInputStream(is);) {
+ try (DumpArchiveInputStream inputStream =
+ DumpArchiveInputStream.builder().setFile(f).get()) {
checkArchiveContent(inputStream, expected);
}
}
diff --git a/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
index 4535627d9..eaf674ffc 100644
--- a/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java
@@ -29,11 +29,8 @@
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
@@ -51,8 +48,9 @@ class DumpArchiveInputStreamTest extends AbstractTest {
@ParameterizedTest
@ValueSource(ints = {1, 10, 32, 1024})
void checkSupportedRecordSizes(final int ntrec) throws Exception {
- try (InputStream is = createArchive(ntrec);
- DumpArchiveInputStream dump = new DumpArchiveInputStream(is)) {
+ try (DumpArchiveInputStream dump = DumpArchiveInputStream.builder()
+ .setByteArray(createArchive(ntrec))
+ .get()) {
final DumpArchiveSummary summary = dump.getSummary();
assertNotNull(summary);
assertEquals(ntrec, summary.getNTRec());
@@ -62,16 +60,14 @@ void checkSupportedRecordSizes(final int ntrec) throws Exception {
@ParameterizedTest
@ValueSource(ints = {Integer.MIN_VALUE, -1, 0, 1025, Integer.MAX_VALUE})
void checkUnsupportedRecordSizes(final int ntrec) throws Exception {
- try (InputStream is = createArchive(ntrec)) {
- final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is));
- assertInstanceOf(ArchiveException.class, ex.getCause());
- assertTrue(
- ex.getMessage().contains(Integer.toString(ntrec)),
- "message should contain the invalid ntrec value");
- }
+ final ArchiveException ex = assertThrows(ArchiveException.class, () -> DumpArchiveInputStream.builder()
+ .setByteArray(createArchive(ntrec))
+ .get());
+ assertInstanceOf(ArchiveException.class, ex.getCause());
+ assertTrue(ex.getMessage().contains(Integer.toString(ntrec)), "message should contain the invalid ntrec value");
}
- private InputStream createArchive(final int ntrec) {
+ private byte[] createArchive(final int ntrec) {
final byte[] dump = new byte[1024 * TP_SIZE];
int offset = 0;
// summary
@@ -82,14 +78,15 @@ private InputStream createArchive(final int ntrec) {
offset += TP_SIZE;
// BITS segment
System.arraycopy(createSegment(DumpArchiveConstants.SEGMENT_TYPE.BITS), 0, dump, offset, TP_SIZE);
- return new ByteArrayInputStream(dump);
+ return dump;
}
@SuppressWarnings("deprecation")
@Test
void testConsumesArchiveCompletely() throws Exception {
try (InputStream is = DumpArchiveInputStreamTest.class.getResourceAsStream("/archive_with_trailer.dump");
- DumpArchiveInputStream dump = new DumpArchiveInputStream(is)) {
+ DumpArchiveInputStream dump =
+ DumpArchiveInputStream.builder().setInputStream(is).get()) {
while (dump.getNextDumpEntry() != null) {
// just consume the archive
}
@@ -102,33 +99,37 @@ void testConsumesArchiveCompletely() throws Exception {
@Test
void testDirectoryNullBytes() throws Exception {
- try (InputStream is = newInputStream("org/apache/commons/compress/dump/directory_null_bytes-fail.dump");
- DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
+ try (DumpArchiveInputStream archive = DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/directory_null_bytes-fail.dump"))
+ .get()) {
assertThrows(InvalidFormatException.class, archive::getNextEntry);
}
}
@Test
void testGetNextEntry() throws IOException {
- try (DumpArchiveInputStream inputStream = new DumpArchiveInputStream(
- Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/dump/getNextEntry.bin")))) {
+ try (DumpArchiveInputStream inputStream = DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/getNextEntry.bin"))
+ .get()) {
assertThrows(ArchiveException.class, inputStream::getNextEntry);
}
}
@Test
- void testInvalidCompressType() throws Exception {
- try (InputStream is = newInputStream("org/apache/commons/compress/dump/invalid_compression_type-fail.dump")) {
- final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close());
- assertInstanceOf(UnsupportedCompressionAlgorithmException.class, ex.getCause());
- }
+ void testInvalidCompressType() {
+ final ArchiveException ex = assertThrows(ArchiveException.class, () -> DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/invalid_compression_type-fail.dump"))
+ .get()
+ .close());
+ assertInstanceOf(UnsupportedCompressionAlgorithmException.class, ex.getCause());
}
@Test
@Timeout(value = 15, unit = TimeUnit.SECONDS, threadMode = ThreadMode.SEPARATE_THREAD)
void testLoopingInodes() throws Exception {
- try (InputStream is = newInputStream("org/apache/commons/compress/dump/looping_inodes-fail.dump");
- DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
+ try (DumpArchiveInputStream archive = DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/looping_inodes-fail.dump"))
+ .get()) {
archive.getNextEntry();
assertThrows(DumpArchiveException.class, archive::getNextEntry);
}
@@ -137,8 +138,8 @@ void testLoopingInodes() throws Exception {
@Test
void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.dump");
- DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
+ try (DumpArchiveInputStream archive =
+ DumpArchiveInputStream.builder().setURI(getURI("bla.dump")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read(buf));
@@ -147,25 +148,34 @@ void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
}
@Test
- void testNotADumpArchive() throws Exception {
- try (InputStream is = newInputStream("bla.zip")) {
- final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close(), "expected an exception");
- assertTrue(ex.getCause() instanceof ShortFileException);
- }
+ void testNotADumpArchive() {
+ final ArchiveException ex = assertThrows(
+ ArchiveException.class,
+ () -> DumpArchiveInputStream.builder()
+ .setURI(getURI("bla.zip"))
+ .get()
+ .close(),
+ "expected an exception");
+ assertTrue(ex.getCause() instanceof ShortFileException);
}
@Test
void testNotADumpArchiveButBigEnough() throws Exception {
- try (InputStream is = newInputStream("zip64support.tar.bz2")) {
- final ArchiveException ex = assertThrows(ArchiveException.class, () -> new DumpArchiveInputStream(is).close(), "expected an exception");
- assertInstanceOf(UnrecognizedFormatException.class, ex.getCause());
- }
+ final ArchiveException ex = assertThrows(
+ ArchiveException.class,
+ () -> DumpArchiveInputStream.builder()
+ .setURI(getURI("zip64support.tar.bz2"))
+ .get()
+ .close(),
+ "expected an exception");
+ assertInstanceOf(UnrecognizedFormatException.class, ex.getCause());
}
@Test
void testRecLenZeroLongExecution() throws Exception {
- try (InputStream is = newInputStream("org/apache/commons/compress/dump/reclen_zero-fail.dump");
- DumpArchiveInputStream archive = new DumpArchiveInputStream(is)) {
+ try (DumpArchiveInputStream archive = DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/reclen_zero-fail.dump"))
+ .get()) {
assertTimeoutPreemptively(Duration.ofSeconds(20), () -> {
assertThrows(DumpArchiveException.class, archive::getNextEntry);
});
@@ -174,8 +184,8 @@ void testRecLenZeroLongExecution() throws Exception {
@Test
void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
- try (InputStream in = newInputStream("bla.dump");
- DumpArchiveInputStream archive = new DumpArchiveInputStream(in)) {
+ try (DumpArchiveInputStream archive =
+ DumpArchiveInputStream.builder().setURI(getURI("bla.dump")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());
diff --git a/src/test/java/org/apache/commons/compress/archivers/dump/TapeInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/dump/TapeInputStreamTest.java
index aba882d35..38c88bcc9 100644
--- a/src/test/java/org/apache/commons/compress/archivers/dump/TapeInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/dump/TapeInputStreamTest.java
@@ -22,9 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Paths;
import org.apache.commons.compress.AbstractTest;
import org.apache.commons.compress.CompressException;
@@ -44,13 +41,14 @@ void testResetBlockSizeArchiveException(final int recsPerBlock) throws Exception
}
@Test
- void testResetBlockSizeBadSignature() throws IOException {
- assertThrows(ArchiveException.class,
- () -> new DumpArchiveInputStream(Files.newInputStream(Paths.get("src/test/resources/org/apache/commons/compress/dump/resetBlockSize.bin"))));
+ void testResetBlockSizeBadSignature() {
+ assertThrows(ArchiveException.class, () -> DumpArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/dump/resetBlockSize.bin"))
+ .get());
}
@ParameterizedTest
- @ValueSource(ints = { Integer.MAX_VALUE / 1000, Integer.MAX_VALUE })
+ @ValueSource(ints = {Integer.MAX_VALUE / 1000, Integer.MAX_VALUE})
void testResetBlockSizeMemoryLimit(final int recsPerBlock) throws Exception {
try (TapeInputStream tapeInputStream = new TapeInputStream(new ByteArrayInputStream(new byte[1]))) {
// CompressException works from both the Maven command line and within Eclipse
(commons-compress) 01/08: feat: add generic builder for `ArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 5e60d5660213e2881012ad7f79faa4ec145a9f40
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 18:01:14 2025 +0200
feat: add generic builder for `ArchiveInputStream`
Introduce a generic builder to configure options common to all `ArchiveInputStream` implementations, providing a unified way to introduce shared parameters.
---
.../commons/compress/archivers/ArchiveInputStream.java | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
index c036880a9..d0fc8a6a1 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveInputStream.java
@@ -26,6 +26,7 @@
import java.util.Objects;
import org.apache.commons.io.Charsets;
+import org.apache.commons.io.build.AbstractStreamBuilder;
import org.apache.commons.io.function.IOConsumer;
import org.apache.commons.io.function.IOIterator;
import org.apache.commons.io.input.NullInputStream;
@@ -88,6 +89,20 @@ public Iterator<E> unwrap() {
}
+ /**
+ * Generic builder for ArchiveInputStream instances.
+ *
+ * @param <T> The type of {@link ArchiveInputStream} to build.
+ * @param <B> The type of the concrete Builder.
+ * @since 1.29.0
+ */
+ public abstract static class Builder<T extends ArchiveInputStream<?>, B extends Builder<T, B>> extends AbstractStreamBuilder<T, B> {
+
+ protected Builder() {
+ // empty
+ }
+ }
+
private static final int BYTE_MASK = 0xFF;
private final byte[] single = new byte[1];
@@ -294,4 +309,5 @@ public int read() throws IOException {
public synchronized void reset() throws IOException {
// noop
}
+
}
(commons-compress) 03/08: feat: add builder for `ArjArchiveInputStream`
Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feat/archive-builders
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit c4140ba3ea8d4441137d33b62b2e5035c9c2d55a
Author: Piotr P. Karwasz <pk...@apache.org>
AuthorDate: Fri Sep 12 18:17:19 2025 +0200
feat: add builder for `ArjArchiveInputStream`
Introduce a builder for `ArjArchiveInputStream` and refactor existing
constructor calls to use it.
---
.../compress/archivers/ArchiveStreamFactory.java | 5 ++-
.../archivers/arj/ArjArchiveInputStream.java | 49 ++++++++++++++++++++--
.../archivers/ArchiveStreamFactoryTest.java | 2 +-
.../archivers/arj/ArjArchiveInputStreamTest.java | 43 ++++++++++---------
4 files changed, 71 insertions(+), 28 deletions(-)
diff --git a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
index ae105b00a..8559b8bd1 100644
--- a/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
+++ b/src/main/java/org/apache/commons/compress/archivers/ArchiveStreamFactory.java
@@ -433,10 +433,11 @@ public <I extends ArchiveInputStream<? extends ArchiveEntry>> I createArchiveInp
return (I) ArArchiveInputStream.builder().setInputStream(in).get();
}
if (ARJ.equalsIgnoreCase(archiverName)) {
+ final ArjArchiveInputStream.Builder arjBuilder = ArjArchiveInputStream.builder().setInputStream(in);
if (actualEncoding != null) {
- return (I) new ArjArchiveInputStream(in, actualEncoding);
+ arjBuilder.setCharset(actualEncoding);
}
- return (I) new ArjArchiveInputStream(in);
+ return (I) arjBuilder.get();
}
if (ZIP.equalsIgnoreCase(archiverName)) {
if (actualEncoding != null) {
diff --git a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
index 497b023b0..b796e0e1e 100644
--- a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
+++ b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java
@@ -46,6 +46,31 @@
*/
public class ArjArchiveInputStream extends ArchiveInputStream<ArjArchiveEntry> {
+ /**
+ * Builds a new {@link ArjArchiveInputStream}.
+ * <p>
+ * For example:
+ * </p>
+ * <pre>{@code
+ * ArjArchiveInputStream in = ArjArchiveInputStream.builder()
+ * .setPath(inputPath)
+ * .setCharset(StandardCharsets.UTF_8)
+ * .get();
+ * }</pre>
+ * @since 1.29.0
+ */
+ public static final class Builder extends ArchiveInputStream.Builder<ArjArchiveInputStream, Builder> {
+
+ private Builder() {
+ setCharset(ENCODING_NAME);
+ }
+
+ @Override
+ public ArjArchiveInputStream get() throws IOException {
+ return new ArjArchiveInputStream(this);
+ }
+ }
+
private static final String ENCODING_NAME = "CP437";
private static final int ARJ_MAGIC_1 = 0x60;
private static final int ARJ_MAGIC_2 = 0xEA;
@@ -61,6 +86,16 @@ public static boolean matches(final byte[] signature, final int length) {
return length >= 2 && (0xff & signature[0]) == ARJ_MAGIC_1 && (0xff & signature[1]) == ARJ_MAGIC_2;
}
+ /**
+ * Creates a new builder.
+ *
+ * @return A new builder
+ * @since 1.29.0
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
private final DataInputStream dis;
private final MainHeader mainHeader;
private LocalFileHeader currentLocalFileHeader;
@@ -73,7 +108,7 @@ public static boolean matches(final byte[] signature, final int length) {
* @throws ArchiveException if an exception occurs while reading
*/
public ArjArchiveInputStream(final InputStream inputStream) throws ArchiveException {
- this(inputStream, ENCODING_NAME);
+ this(inputStream, builder());
}
/**
@@ -84,8 +119,16 @@ public ArjArchiveInputStream(final InputStream inputStream) throws ArchiveExcept
* @throws ArchiveException if an exception occurs while reading
*/
public ArjArchiveInputStream(final InputStream inputStream, final String charsetName) throws ArchiveException {
- super(inputStream, charsetName);
- in = dis = new DataInputStream(inputStream);
+ this(inputStream, builder().setCharset(charsetName));
+ }
+
+ private ArjArchiveInputStream(final Builder builder) throws IOException {
+ this(builder.getInputStream(), builder);
+ }
+
+ private ArjArchiveInputStream(final InputStream inputStream, final Builder builder) throws ArchiveException {
+ super(new DataInputStream(inputStream), builder.getCharset());
+ dis = (DataInputStream) in;
try {
mainHeader = readMainHeader();
if ((mainHeader.arjFlags & MainHeader.Flags.GARBLED) != 0) {
diff --git a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
index 1fe084286..a0c9ba083 100644
--- a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java
@@ -104,7 +104,7 @@ public String toString() {
static {
String dflt;
dflt = UNKNOWN;
- try (ArjArchiveInputStream inputStream = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ try (ArjArchiveInputStream inputStream = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
dflt = getCharsetName(inputStream);
} catch (final Exception e) {
e.printStackTrace();
diff --git a/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
index d62a90e11..fc255817e 100644
--- a/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStreamTest.java
@@ -28,7 +28,6 @@
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
-import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.TimeZone;
@@ -81,16 +80,16 @@ private void assertForEach(final ArjArchiveInputStream archive) throws IOExcepti
}
@Test
- void testFirstHeaderSizeSetToZero() throws Exception {
- try (InputStream in = newInputStream("org/apache/commons/compress/arj/zero_sized_headers-fail.arj")) {
- final ArchiveException ex = assertThrows(ArchiveException.class, () -> {
- try (ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
- // Do nothing, ArchiveException already thrown
- fail("ArchiveException not thrown.");
- }
- });
- assertTrue(ex.getCause() instanceof IOException);
- }
+ void testFirstHeaderSizeSetToZero() {
+ final ArchiveException ex = assertThrows(ArchiveException.class, () -> {
+ try (ArjArchiveInputStream archive = ArjArchiveInputStream.builder()
+ .setURI(getURI("org/apache/commons/compress/arj/zero_sized_headers-fail.arj"))
+ .get()) {
+ // Do nothing, ArchiveException already thrown
+ fail("ArchiveException not thrown.");
+ }
+ });
+ assertTrue(ex.getCause() instanceof IOException);
}
@Test
@@ -100,7 +99,7 @@ void testForEach() throws Exception {
expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
- try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ try (ArjArchiveInputStream in = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
in.forEach(entry -> {
result.append(entry.getName());
int tmp;
@@ -122,7 +121,7 @@ void testGetNextEntry() throws Exception {
expected.append("<empty/>test2.xml<?xml version=\"1.0\"?>\n");
expected.append("<empty/>\n");
final StringBuilder result = new StringBuilder();
- try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ try (ArjArchiveInputStream in = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
ArjArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.append(entry.getName());
@@ -141,8 +140,7 @@ void testGetNextEntry() throws Exception {
@Test
void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
final byte[] buf = new byte[2];
- try (InputStream in = newInputStream("bla.arj");
- ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
+ try (ArjArchiveInputStream archive = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read(buf));
@@ -159,7 +157,7 @@ void testRead() throws Exception {
expected.append("<empty/>\n");
final Charset charset = Charset.defaultCharset();
try (ByteArrayOutputStream result = new ByteArrayOutputStream();
- ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ ArjArchiveInputStream in = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
ArjArchiveEntry entry;
while ((entry = in.getNextEntry()) != null) {
result.write(entry.getName().getBytes(charset));
@@ -184,7 +182,7 @@ void testReadByteArray() throws Exception {
expected.append("<empty/>\n");
final Charset charset = Charset.defaultCharset();
try (ByteArrayOutputStream result = new ByteArrayOutputStream();
- ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ ArjArchiveInputStream in = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
in.forEach(entry -> {
result.write(entry.getName().getBytes(charset));
final byte[] tmp = new byte[2];
@@ -209,7 +207,9 @@ void testReadByteArrayIndex() throws Exception {
expected.append("<empty/>\n");
final Charset charset = Charset.defaultCharset();
try (ByteArrayOutputStream result = new ByteArrayOutputStream();
- ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ ArjArchiveInputStream in = ArjArchiveInputStream.builder()
+ .setURI(getURI("bla.arj"))
+ .get()) {
in.forEach(entry -> {
result.write(entry.getName().getBytes(charset));
final byte[] tmp = new byte[10];
@@ -228,7 +228,7 @@ void testReadByteArrayIndex() throws Exception {
@Test
void testReadingOfAttributesDosVersion() throws Exception {
- try (ArjArchiveInputStream archive = new ArjArchiveInputStream(newInputStream("bla.arj"))) {
+ try (ArjArchiveInputStream archive = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
final ArjArchiveEntry entry = archive.getNextEntry();
assertEquals("test1.xml", entry.getName());
assertEquals(30, entry.getSize());
@@ -243,7 +243,7 @@ void testReadingOfAttributesDosVersion() throws Exception {
@Test
void testReadingOfAttributesUnixVersion() throws Exception {
- try (ArjArchiveInputStream in = new ArjArchiveInputStream(newInputStream("bla.unix.arj"))) {
+ try (ArjArchiveInputStream in = ArjArchiveInputStream.builder().setURI(getURI("bla.unix.arj")).get()) {
final ArjArchiveEntry entry = in.getNextEntry();
assertEquals("test1.xml", entry.getName());
assertEquals(30, entry.getSize());
@@ -258,8 +258,7 @@ void testReadingOfAttributesUnixVersion() throws Exception {
@Test
void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws Exception {
- try (InputStream in = newInputStream("bla.arj");
- ArjArchiveInputStream archive = new ArjArchiveInputStream(in)) {
+ try (ArjArchiveInputStream archive = ArjArchiveInputStream.builder().setURI(getURI("bla.arj")).get()) {
assertNotNull(archive.getNextEntry());
IOUtils.toByteArray(archive);
assertEquals(-1, archive.read());