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


##########
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, () -> {

Review Comment:
   No need for a block and put it all on one line.



##########
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, () -> {

Review Comment:
   No need for a block and put it all on one line.



##########
src/test/java/org/apache/commons/compress/compressors/DetectCompressorTest.java:
##########
@@ -129,9 +140,17 @@ 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);
+            if (compressorNames != null) {
+                return CompressorStreamFactory.detect(is, compressorNames);
+            } else {
+                return CompressorStreamFactory.detect(is);

Review Comment:
   Use a single return with ternary expression.



##########
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);
+    }

Review Comment:
   Missing space between methods.



##########
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:
   Use longer lines (160), let's avoid "newspaper" formatting. I'll have to 
create a checktyle file for this component...



##########
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",
+                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
+    public void testDetectLimitedByNameNotFound() throws Exception {
+        assertThrows(CompressorException.class, () ->

Review Comment:
   Reduce duplication. How many 
`Collections.singleton(CompressorStreamFactory.DEFLATE)` do we really need?



##########
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:
   Make package-private, no need to grow the public/protected API footprint.



-- 
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