garydgregory commented on code in PR #433:
URL: https://github.com/apache/commons-compress/pull/433#discussion_r1389696914


##########
src/test/java/org/apache/commons/compress/compressors/DetectCompressorTest.java:
##########
@@ -129,9 +140,13 @@ private InputStream createStreamFor(final String fileName, 
final int memoryLimit
     }
 
     private String detect(final String testFileName) throws IOException, 
CompressorException {
+        return detect(testFileName, null);
+    }
+
+    private String detect(final String testFileName, final Set<String> 
compressorNames) throws IOException, CompressorException {
         try (InputStream is = new BufferedInputStream(
                 Files.newInputStream(getFile(testFileName).toPath()))) {
-            return CompressorStreamFactory.detect(is);
+            return (compressorNames != null) ? 
CompressorStreamFactory.detect(is, compressorNames) : 
CompressorStreamFactory.detect(is);

Review Comment:
   No need for the extra `(` and `)` 



##########
src/test/java/org/apache/commons/compress/compressors/DetectCompressorTest.java:
##########
@@ -164,7 +179,43 @@ public void testDetect() throws Exception {
     }
 
     @Test
-    public void testDetection() throws Exception {
+    public void testDetectNullOrEmptyCompressorNames() throws Exception {
+        assertThrows(IllegalArgumentException.class, () -> 
CompressorStreamFactory.detect(createStreamFor("bla.txt.bz2"), (Set<String>) 
null));
+        assertThrows(IllegalArgumentException.class, () -> 
CompressorStreamFactory.detect(createStreamFor("bla.tgz"), new HashSet<>()));
+    }
+
+    @Test
+    public void testDetectLimitedByName() throws Exception {
+        assertEquals(CompressorStreamFactory.BZIP2, detect("bla.txt.bz2", 
Collections.singleton(CompressorStreamFactory.BZIP2)));
+        assertEquals(CompressorStreamFactory.GZIP, detect("bla.tgz", 
Collections.singleton(CompressorStreamFactory.GZIP)));
+        assertEquals(CompressorStreamFactory.PACK200, detect("bla.pack", 
Collections.singleton(CompressorStreamFactory.PACK200)));
+        assertEquals(CompressorStreamFactory.XZ, detect("bla.tar.xz", 
Collections.singleton(CompressorStreamFactory.XZ)));
+        assertEquals(CompressorStreamFactory.DEFLATE, 
detect("bla.tar.deflatez", 
Collections.singleton(CompressorStreamFactory.DEFLATE)));
+        assertEquals(CompressorStreamFactory.LZ4_FRAMED, detect("bla.tar.lz4", 
Collections.singleton(CompressorStreamFactory.LZ4_FRAMED)));
+        assertEquals(CompressorStreamFactory.LZMA, detect("bla.tar.lzma", 
Collections.singleton(CompressorStreamFactory.LZMA)));
+        assertEquals(CompressorStreamFactory.SNAPPY_FRAMED, 
detect("bla.tar.sz", 
Collections.singleton(CompressorStreamFactory.SNAPPY_FRAMED)));
+        assertEquals(CompressorStreamFactory.Z, detect("bla.tar.Z", 
Collections.singleton(CompressorStreamFactory.Z)));
+        assertEquals(CompressorStreamFactory.ZSTANDARD, detect("bla.tar.zst", 
Collections.singleton(CompressorStreamFactory.ZSTANDARD)));
+    }
+
+    @Test

Review Comment:
   You can remove a lot of boiler plate here by using JUnit's 
`@ParameterizedTest` and `@ValueSource`.



##########
src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java:
##########
@@ -226,10 +227,32 @@ private static Iterable<CompressorStreamProvider> 
archiveStreamProviderIterable(
      * @since 1.14
      */
     public static String detect(final InputStream inputStream) throws 
CompressorException {
+        final Set<String> defaultCompressorNamesForDetection =
+                Sets.newHashSet(BZIP2, GZIP, PACK200, SNAPPY_FRAMED, Z, 
DEFLATE, XZ, LZMA, LZ4_FRAMED, ZSTANDARD);
+        return detect(inputStream, defaultCompressorNamesForDetection);
+    }
+
+    /**
+     * Try to detect the type of compressor stream while limiting the type to 
the provided set of compressor names.
+     *
+     * @param inputStream input stream
+     * @param compressorNames compressor names to limit autodetection
+     * @return type of compressor stream detected
+     * @throws CompressorException if no compressor stream type was detected
+     *                             or if something else went wrong
+     * @throws IllegalArgumentException if stream is null or does not support 
mark
+     *
+     * @since 1.24

Review Comment:
   No need for an `@since` tag for package-private code.



##########
src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java:
##########
@@ -226,10 +227,31 @@ private static Iterable<CompressorStreamProvider> 
archiveStreamProviderIterable(
      * @since 1.14
      */
     public static String detect(final InputStream inputStream) throws 
CompressorException {
+        final Set<String> defaultCompressorNamesForDetection =
+                Sets.newHashSet(BZIP2, GZIP, PACK200, SNAPPY_FRAMED, Z, 
DEFLATE, XZ, LZMA, LZ4_FRAMED, ZSTANDARD);
+        return detect(inputStream, defaultCompressorNamesForDetection);
+    }
+    /**
+     * Try to detect the type of compressor stream while limiting the type to 
the provided set of compressor names.
+     *
+     * @param inputStream input stream
+     * @param compressorNames compressor names to limit autodetection
+     * @return type of compressor stream detected
+     * @throws CompressorException if no compressor stream type was detected
+     *                             or if something else went wrong
+     * @throws IllegalArgumentException if stream is null or does not support 
mark
+     *
+     * @since 1.24
+     */
+    public static String detect(final InputStream inputStream, final 
Set<String> compressorNames) throws CompressorException {

Review Comment:
   Up to you, as long as the _new_ code is 100% covered (or as as close to 100% 
as possible).



##########
src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java:
##########
@@ -522,6 +547,27 @@ public CompressorInputStream 
createCompressorInputStream(final InputStream in) t
         return createCompressorInputStream(detect(in), in);
     }
 
+    /**
+     * Create a compressor input stream from an input stream, auto-detecting 
the
+     * compressor type from the first few bytes of the stream while limiting 
the detected type
+     * to the provided set of compressor names. The InputStream must support 
marks, like BufferedInputStream.
+     *
+     * @param in
+     *            the input stream
+     * @param compressorNames
+     *            compressor names to limit autodetection
+     * @return the compressor input stream
+     * @throws CompressorException
+     *             if the autodetected compressor is not in the provided set 
of compressor names
+     * @throws IllegalArgumentException
+     *             if the stream is null or does not support mark
+     * @since 1.25

Review Comment:
   The next version will be 1.25.1 but this will be for 1.26.0 since this PR 
adds a new public API.



##########
src/test/java/org/apache/commons/compress/compressors/DetectCompressorTest.java:
##########
@@ -103,6 +106,14 @@ private CompressorInputStream createStreamFor(final String 
resource)
                        getFile(resource).toPath())));
     }
 
+    @SuppressWarnings("resource") // Caller closes.
+    private CompressorInputStream createStreamFor(final String resource, final 
Set<String> compressorNames)
+            throws CompressorException, IOException {
+        return factory.createCompressorInputStream(

Review Comment:
   Longer lines (everywhere).



##########
src/test/java/org/apache/commons/compress/compressors/DetectCompressorTest.java:
##########
@@ -164,7 +183,70 @@ public void testDetect() throws Exception {
     }
 
     @Test
-    public void testDetection() throws Exception {
+    public void testDetectNullOrEmptyCompressorNames() throws Exception {
+        try (CompressorInputStream bzip2 = createStreamFor("bla.txt.bz2")) {
+            assertThrows(IllegalArgumentException.class, () -> {
+                CompressorStreamFactory.detect(bzip2, (Set<String>) null);
+            });
+        }
+
+        try (CompressorInputStream gzip = createStreamFor("bla.tgz")) {
+            assertThrows(IllegalArgumentException.class, () -> {
+                CompressorStreamFactory.detect(gzip, new HashSet<>());
+            });
+        }
+    }
+
+    @Test
+    public void testDetectLimitedByName() throws Exception {
+        assertEquals(CompressorStreamFactory.BZIP2, detect("bla.txt.bz2",

Review Comment:
   All of it :-)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to