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:37 UTC

(commons-compress) 05/08: feat: add builder for `DumpArchiveInputStream`

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