DefaultDeflateCompressionDiviner { public List<DivinationResult> divineDeflateParameters(File archiveFile) throws IOException { List<DivinationResult> results = new ArrayList<DivinationResult>(); for (MinimalZipEntry minimalZipEntry : MinimalZipArchive.listEntries(archiveFile)) { JreDeflateParameters divinedParameters = null; if (minimalZipEntry.isDeflateCompressed()) { RandomAccessFileInputStreamFactory rafisFactory = new RandomAccessFileInputStreamFactory( archiveFile, minimalZipEntry.getFileOffsetOfCompressedData(), minimalZipEntry.getCompressedSize()); divinedParameters = divineDeflateParameters(rafisFactory); } results.add(new DivinationResult(minimalZipEntry, divinedParameters)); } return results; }  List<DivinationResult> divineDeflateParameters(File archiveFile); JreDeflateParameters divineDeflateParameters(
      MultiViewInputStreamFactory<?> compressedDataInputStreamFactory);  }
@Test public void testDivineDeflateParameters_NoMarkInputStreamFactory() throws IOException { final JreDeflateParameters parameters = JreDeflateParameters.of(1, 0, true); final byte[] buffer = deflate(parameters); try { diviner.divineDeflateParameters(new ByteArrayInputStreamFactory(buffer, false, true)); Assert.fail("operating without a markable stream"); } catch (IllegalArgumentException expected) { } }
@Test public void testDivineDeflateParameters_BadCloseInputStreamFactory() throws IOException { final JreDeflateParameters parameters = JreDeflateParameters.of(1, 0, true); final byte[] buffer = deflate(parameters); JreDeflateParameters result = diviner.divineDeflateParameters(new ByteArrayInputStreamFactory(buffer, true, true)); Assert.assertEquals(result, parameters); }
@Test public void testDivineDeflateParameters_JunkData() throws IOException { final byte[] junk = new byte[] {0, 1, 2, 3, 4}; Assert.assertNull( diviner.divineDeflateParameters(new ByteArrayInputStreamFactory(junk, true, false))); }
@Test public void testDivineDeflateParameters_AllValidSettings() throws IOException { for (boolean nowrap : new boolean[] {true, false}) { for (int strategy : new int[] {0, 1, 2}) { for (int level : new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9}) { JreDeflateParameters trueParameters = JreDeflateParameters.of(level, strategy, nowrap); final byte[] buffer = deflate(trueParameters); ByteArrayInputStreamFactory factory = new ByteArrayInputStreamFactory(buffer, true, false); JreDeflateParameters divinedParameters = diviner.divineDeflateParameters(factory); Assert.assertNotNull(divinedParameters); if (strategy == 1 && level <= 3) { Assert.assertEquals(JreDeflateParameters.of(level, 0, nowrap), divinedParameters); } else if (strategy == 2) { Assert.assertEquals(nowrap, divinedParameters.nowrap); Assert.assertEquals(strategy, divinedParameters.strategy); } else { Assert.assertEquals(trueParameters, divinedParameters); } } } } }
@Test public void testDivineDeflateParameters_File() throws IOException { File tempFile = File.createTempFile("ddcdt", "tmp"); tempFile.deleteOnExit(); try { UnitTestZipArchive.saveTestZip(tempFile); List<DivinationResult> results = diviner.divineDeflateParameters(tempFile); Assert.assertEquals(UnitTestZipArchive.allEntriesInFileOrder.size(), results.size()); for (int x = 0; x < results.size(); x++) { UnitTestZipEntry expected = UnitTestZipArchive.allEntriesInFileOrder.get(x); DivinationResult actual = results.get(x); Assert.assertEquals(expected.path, actual.minimalZipEntry.getFileName()); int expectedLevel = expected.level; if (expectedLevel > 0) { Assert.assertTrue(actual.minimalZipEntry.isDeflateCompressed()); Assert.assertNotNull(actual.divinedParameters); Assert.assertEquals(expectedLevel, actual.divinedParameters.level); Assert.assertEquals(0, actual.divinedParameters.strategy); Assert.assertTrue(actual.divinedParameters.nowrap); } else { Assert.assertFalse(actual.minimalZipEntry.isDeflateCompressed()); Assert.assertNull(actual.divinedParameters); } } } finally { try { tempFile.delete(); } catch (Exception ignoreD) { } } }