More java7 language features
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/32c30f6f Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/32c30f6f Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/32c30f6f Branch: refs/heads/master Commit: 32c30f6f072ccfea6ead90f8eef0c205d88d00d3 Parents: 77388c8 Author: Kristian Rosenvold <[email protected]> Authored: Mon Jun 27 10:02:45 2016 +0200 Committer: Kristian Rosenvold <[email protected]> Committed: Mon Jun 27 10:02:45 2016 +0200 ---------------------------------------------------------------------- .../archivers/cpio/CpioArchiveInputStream.java | 20 +- .../compress/archivers/sevenz/SevenZFile.java | 5 +- .../compress/archivers/zip/ExtraFieldUtils.java | 4 +- .../commons/compress/utils/ArchiveUtils.java | 4 +- .../commons/compress/AbstractTestCase.java | 21 +- .../archivers/ArchiveStreamFactoryTest.java | 57 ++--- .../compress/archivers/DumpTestCase.java | 18 +- .../compress/archivers/SevenZTestCase.java | 19 +- .../commons/compress/archivers/ZipTestCase.java | 16 +- .../dump/DumpArchiveInputStreamTest.java | 10 +- .../archivers/sevenz/SevenZFileTest.java | 60 ++--- .../archivers/sevenz/SevenZOutputFileTest.java | 70 ++---- .../tar/TarArchiveInputStreamTest.java | 35 +-- .../commons/compress/archivers/zip/Lister.java | 20 +- .../compress/archivers/zip/Zip64SupportIT.java | 225 ++++++++----------- .../zip/ZipArchiveInputStreamTest.java | 25 +-- .../compress/compressors/BZip2TestCase.java | 36 +-- .../compress/compressors/DeflateTestCase.java | 34 +-- .../compressors/FramedSnappyTestCase.java | 17 +- .../compress/compressors/GZipTestCase.java | 65 ++---- .../compress/compressors/LZMATestCase.java | 12 +- .../compress/compressors/Pack200TestCase.java | 58 ++--- .../compress/compressors/XZTestCase.java | 42 +--- .../commons/compress/compressors/ZTestCase.java | 5 +- .../bzip2/BZip2CompressorInputStreamTest.java | 5 +- .../DeflateCompressorInputStreamTest.java | 28 +-- .../compressors/pack200/Pack200UtilsTest.java | 7 +- .../FramedSnappyCompressorInputStreamTest.java | 49 +--- 28 files changed, 290 insertions(+), 677 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java ---------------------------------------------------------------------- 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 6e4d09e..5f2e586 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 @@ -246,14 +246,18 @@ public class CpioArchiveInputStream extends ArchiveInputStream implements readFully(SIX_BYTES_BUF, TWO_BYTES_BUF.length, FOUR_BYTES_BUF.length); final String magicString = ArchiveUtils.toAsciiString(SIX_BYTES_BUF); - if (magicString.equals(MAGIC_NEW)) { - this.entry = readNewEntry(false); - } else if (magicString.equals(MAGIC_NEW_CRC)) { - this.entry = readNewEntry(true); - } else if (magicString.equals(MAGIC_OLD_ASCII)) { - this.entry = readOldAsciiEntry(); - } else { - throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getBytesRead()); + switch (magicString) { + case MAGIC_NEW: + this.entry = readNewEntry(false); + break; + case MAGIC_NEW_CRC: + this.entry = readNewEntry(true); + break; + case MAGIC_OLD_ASCII: + this.entry = readOldAsciiEntry(); + break; + default: + throw new IOException("Unknown magic [" + magicString + "]. Occured at byte: " + getBytesRead()); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java index 1a7014b..51ba9e6 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java @@ -301,11 +301,8 @@ public class SevenZFile implements Closeable { folder.getUnpackSize(), folder.crc); } final byte[] nextHeader = new byte[(int)folder.getUnpackSize()]; - final DataInputStream nextHeaderInputStream = new DataInputStream(inputStreamStack); - try { + try (DataInputStream nextHeaderInputStream = new DataInputStream(inputStreamStack)) { nextHeaderInputStream.readFully(nextHeader); - } finally { - nextHeaderInputStream.close(); } return new DataInputStream(new ByteArrayInputStream(nextHeader)); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/main/java/org/apache/commons/compress/archivers/zip/ExtraFieldUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ExtraFieldUtils.java b/src/main/java/org/apache/commons/compress/archivers/zip/ExtraFieldUtils.java index c4d07f4..b59aa39 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ExtraFieldUtils.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ExtraFieldUtils.java @@ -181,10 +181,8 @@ public class ExtraFieldUtils { length); } v.add(ze); - } catch (final InstantiationException ie) { + } catch (final InstantiationException | IllegalAccessException ie) { throw (ZipException) new ZipException(ie.getMessage()).initCause(ie); - } catch (final IllegalAccessException iae) { - throw (ZipException) new ZipException(iae.getMessage()).initCause(iae); } start += length + WORD; } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java b/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java index 38a4cf8..25e5301 100644 --- a/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java +++ b/src/main/java/org/apache/commons/compress/utils/ArchiveUtils.java @@ -278,10 +278,8 @@ public class ArchiveUtils { chars[i] = '.'; } } - final int len = chars.length; final StringBuilder sb = new StringBuilder(); - for (int i = 0; i < len; i++) { - final char c = chars[i]; + for (final char c : chars) { if (!Character.isISOControl(c)) { final Character.UnicodeBlock block = Character.UnicodeBlock.of(c); if (block != null && block != Character.UnicodeBlock.SPECIALS) { http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/AbstractTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/AbstractTestCase.java b/src/test/java/org/apache/commons/compress/AbstractTestCase.java index 15326f5..5a52aea 100644 --- a/src/test/java/org/apache/commons/compress/AbstractTestCase.java +++ b/src/test/java/org/apache/commons/compress/AbstractTestCase.java @@ -284,13 +284,10 @@ public abstract class AbstractTestCase { */ protected void checkArchiveContent(final File archive, final List<String> expected) throws Exception { - final InputStream is = new FileInputStream(archive); - try { + try (InputStream is = new FileInputStream(archive)) { final BufferedInputStream buf = new BufferedInputStream(is); final ArchiveInputStream in = factory.createArchiveInputStream(buf); this.checkArchiveContent(in, expected); - } finally { - is.close(); } } @@ -330,11 +327,8 @@ public abstract class AbstractTestCase { outfile.mkdirs(); } else { outfile.getParentFile().mkdirs(); - final OutputStream out = new FileOutputStream(outfile); - try { - copied=IOUtils.copy(in, out); - } finally { - out.close(); + try (OutputStream out = new FileOutputStream(outfile)) { + copied = IOUtils.copy(in, out); } } final long size = entry.getSize(); @@ -384,12 +378,9 @@ public abstract class AbstractTestCase { final File tmpDir = createTempDir(); final File tmpFile = File.createTempFile("testfile", "", tmpDir); tmpFile.deleteOnExit(); - final FileOutputStream fos = new FileOutputStream(tmpFile); - try { - fos.write(new byte[] {'f', 'o', 'o'}); - return new File[] {tmpDir, tmpFile}; - } finally { - fos.close(); + try (FileOutputStream fos = new FileOutputStream(tmpFile)) { + fos.write(new byte[] { 'f', 'o', 'o' }); + return new File[] { tmpDir, tmpFile }; } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java ---------------------------------------------------------------------- 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 4c78f93..f7b236b 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/ArchiveStreamFactoryTest.java @@ -63,38 +63,26 @@ public class ArchiveStreamFactoryTest { */ @Test public void aiffFilesAreNoTARs() throws Exception { - final FileInputStream fis = new FileInputStream("src/test/resources/testAIFF.aif"); - try { - final InputStream is = new BufferedInputStream(fis); - try { + try (FileInputStream fis = new FileInputStream("src/test/resources/testAIFF.aif")) { + try (InputStream is = new BufferedInputStream(fis)) { new ArchiveStreamFactory().createArchiveInputStream(is); fail("created an input stream for a non-archive"); } catch (final ArchiveException ae) { assertTrue(ae.getMessage().startsWith("No Archiver found")); - } finally { - is.close(); } - } finally { - fis.close(); - } + } } @Test public void testCOMPRESS209() throws Exception { - final FileInputStream fis = new FileInputStream("src/test/resources/testCompress209.doc"); - try { - final InputStream bis = new BufferedInputStream(fis); - try { + try (FileInputStream fis = new FileInputStream("src/test/resources/testCompress209.doc")) { + try (InputStream bis = new BufferedInputStream(fis)) { new ArchiveStreamFactory().createArchiveInputStream(bis); fail("created an input stream for a non-archive"); } catch (final ArchiveException ae) { assertTrue(ae.getMessage().startsWith("No Archiver found")); - } finally { - bis.close(); } - } finally { - fis.close(); - } + } } @Test(expected = StreamingNotSupportedException.class) @@ -118,20 +106,14 @@ public class ArchiveStreamFactoryTest { */ @Test public void detectsAndThrowsFor7z() throws Exception { - final FileInputStream fis = new FileInputStream("src/test/resources/bla.7z"); - try { - final InputStream bis = new BufferedInputStream(fis); - try { + try (FileInputStream fis = new FileInputStream("src/test/resources/bla.7z")) { + try (InputStream bis = new BufferedInputStream(fis)) { new ArchiveStreamFactory().createArchiveInputStream(bis); fail("Expected a StreamingNotSupportedException"); } catch (final StreamingNotSupportedException ex) { assertEquals(ArchiveStreamFactory.SEVEN_Z, ex.getFormat()); - } finally { - bis.close(); } - } finally { - fis.close(); - } + } } /** @@ -141,22 +123,13 @@ public class ArchiveStreamFactoryTest { */ @Test public void skipsPK00Prefix() throws Exception { - final FileInputStream fis = new FileInputStream("src/test/resources/COMPRESS-208.zip"); - try { - final InputStream bis = new BufferedInputStream(fis); - try { - final ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(bis); - try { + try (FileInputStream fis = new FileInputStream("src/test/resources/COMPRESS-208.zip")) { + try (InputStream bis = new BufferedInputStream(fis)) { + try (ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(bis)) { assertTrue(ais instanceof ZipArchiveInputStream); - } finally { - ais.close(); } - } finally { - bis.close(); } - } finally { - fis.close(); - } + } } @Test @@ -233,8 +206,6 @@ public class ArchiveStreamFactoryTest { dflt = UNKNOWN; try { dflt = getField(new ArjArchiveInputStream(new FileInputStream(getFile("bla.arj"))), "charsetName"); - } catch (final ArchiveException e) { - e.printStackTrace(); } catch (final Exception e) { e.printStackTrace(); } @@ -242,8 +213,6 @@ public class ArchiveStreamFactoryTest { dflt = UNKNOWN; try { dflt = getField(new DumpArchiveInputStream(new FileInputStream(getFile("bla.dump"))), "encoding"); - } catch (final ArchiveException e) { - e.printStackTrace(); } catch (final Exception e) { e.printStackTrace(); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/DumpTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/DumpTestCase.java b/src/test/java/org/apache/commons/compress/archivers/DumpTestCase.java index 2e4a652..8675902 100644 --- a/src/test/java/org/apache/commons/compress/archivers/DumpTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/DumpTestCase.java @@ -90,14 +90,11 @@ public final class DumpTestCase extends AbstractTestCase { } private void archiveDetection(final File f) throws Exception { - final InputStream is = new FileInputStream(f); - try { + try (InputStream is = new FileInputStream(f)) { assertEquals(DumpArchiveInputStream.class, - new ArchiveStreamFactory() - .createArchiveInputStream(new BufferedInputStream(is)) - .getClass()); - } finally { - is.close(); + new ArchiveStreamFactory() + .createArchiveInputStream(new BufferedInputStream(is)) + .getClass()); } } @@ -117,12 +114,9 @@ public final class DumpTestCase extends AbstractTestCase { expected.add("lost+found/"); expected.add("test1.xml"); expected.add("test2.xml"); - final InputStream is = new FileInputStream(f); - try { + try (InputStream is = new FileInputStream(f)) { checkArchiveContent(new DumpArchiveInputStream(is), - expected); - } finally { - is.close(); + expected); } } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java index 505be6b..79e0019 100644 --- a/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java @@ -74,22 +74,19 @@ public class SevenZTestCase extends AbstractTestCase { } finally { outArchive.close(); } - - final SevenZFile archive = new SevenZFile(output); - try { + + try (SevenZFile archive = new SevenZFile(output)) { SevenZArchiveEntry entry; - + entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals(entry.getName(), file1.getName()); - + entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals(entry.getName(), file2.getName()); - - assert(archive.getNextEntry() == null); - } finally { - archive.close(); + + assert (archive.getNextEntry() == null); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java index 23479ea..00015ad 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java +++ b/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java @@ -89,11 +89,8 @@ public final class ZipTestCase extends AbstractTestCase { while((entry = (ZipArchiveEntry)in.getNextEntry()) != null) { final File outfile = new File(resultDir.getCanonicalPath() + "/result/" + entry.getName()); outfile.getParentFile().mkdirs(); - final OutputStream o = new FileOutputStream(outfile); - try { + try (OutputStream o = new FileOutputStream(outfile)) { IOUtils.copy(in, o); - } finally { - o.close(); } results.add(outfile); } @@ -135,14 +132,11 @@ public final class ZipTestCase extends AbstractTestCase { @Test public void testSkipsPK00Prefix() throws Exception { final File input = getFile("COMPRESS-208.zip"); - final InputStream is = new FileInputStream(input); final ArrayList<String> al = new ArrayList<>(); al.add("test1.xml"); al.add("test2.xml"); - try { + try (InputStream is = new FileInputStream(input)) { checkArchiveContent(new ZipArchiveInputStream(is), al); - } finally { - is.close(); } } @@ -177,9 +171,7 @@ public final class ZipTestCase extends AbstractTestCase { @Test public void testSkipEntryWithUnsupportedCompressionMethod() throws IOException { - final ZipArchiveInputStream zip = - new ZipArchiveInputStream(new FileInputStream(getFile("moby.zip"))); - try { + try (ZipArchiveInputStream zip = new ZipArchiveInputStream(new FileInputStream(getFile("moby.zip")))) { final ZipArchiveEntry entry = zip.getNextZipEntry(); assertEquals("method", ZipMethod.TOKENIZATION.getCode(), entry.getMethod()); assertEquals("README", entry.getName()); @@ -190,8 +182,6 @@ public final class ZipTestCase extends AbstractTestCase { e.printStackTrace(); fail("COMPRESS-93: Unable to skip an unsupported zip entry"); } - } finally { - zip.close(); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStreamTest.java ---------------------------------------------------------------------- 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 bc6df9e..0875913 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 @@ -31,29 +31,23 @@ public class DumpArchiveInputStreamTest extends AbstractTestCase { @Test public void testNotADumpArchive() throws Exception { - final FileInputStream is = new FileInputStream(getFile("bla.zip")); - try { + try (FileInputStream is = new FileInputStream(getFile("bla.zip"))) { new DumpArchiveInputStream(is); fail("expected an exception"); } catch (final ArchiveException ex) { // expected assertTrue(ex.getCause() instanceof ShortFileException); - } finally { - is.close(); } } @Test public void testNotADumpArchiveButBigEnough() throws Exception { - final FileInputStream is = new FileInputStream(getFile("zip64support.tar.bz2")); - try { + try (FileInputStream is = new FileInputStream(getFile("zip64support.tar.bz2"))) { new DumpArchiveInputStream(is); fail("expected an exception"); } catch (final ArchiveException ex) { // expected assertTrue(ex.getCause() instanceof UnrecognizedFormatException); - } finally { - is.close(); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java index 0b88725..76687d8 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java @@ -103,11 +103,8 @@ public class SevenZFileTest extends AbstractTestCase { @Test public void testAllEmptyFilesArchive() throws Exception { - final SevenZFile archive = new SevenZFile(getFile("7z-empty-mhc-off.7z")); - try { + try (SevenZFile archive = new SevenZFile(getFile("7z-empty-mhc-off.7z"))) { assertNotNull(archive.getNextEntry()); - } finally { - archive.close(); } } @@ -164,15 +161,12 @@ public class SevenZFileTest extends AbstractTestCase { */ @Test public void testCompressedHeaderWithNonDefaultDictionarySize() throws Exception { - final SevenZFile sevenZFile = new SevenZFile(getFile("COMPRESS-256.7z")); - try { + try (SevenZFile sevenZFile = new SevenZFile(getFile("COMPRESS-256.7z"))) { int count = 0; while (sevenZFile.getNextEntry() != null) { count++; } assertEquals(446, count); - } finally { - sevenZFile.close(); } } @@ -194,60 +188,47 @@ public class SevenZFileTest extends AbstractTestCase { @Test public void testReadingBackLZMA2DictSize() throws Exception { final File output = new File(dir, "lzma2-dictsize.7z"); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { outArchive.setContentMethods(Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.LZMA2, 1 << 20))); final SevenZArchiveEntry entry = new SevenZArchiveEntry(); entry.setName("foo.txt"); outArchive.putArchiveEntry(entry); outArchive.write(new byte[] { 'A' }); outArchive.closeArchiveEntry(); - } finally { - outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { final SevenZArchiveEntry entry = archive.getNextEntry(); final SevenZMethodConfiguration m = entry.getContentMethods().iterator().next(); assertEquals(SevenZMethod.LZMA2, m.getMethod()); assertEquals(1 << 20, m.getOptions()); - } finally { - archive.close(); } } @Test public void testReadingBackDeltaDistance() throws Exception { final File output = new File(dir, "delta-distance.7z"); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { outArchive.setContentMethods(Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.DELTA_FILTER, 32), - new SevenZMethodConfiguration(SevenZMethod.LZMA2))); + new SevenZMethodConfiguration(SevenZMethod.LZMA2))); final SevenZArchiveEntry entry = new SevenZArchiveEntry(); entry.setName("foo.txt"); outArchive.putArchiveEntry(entry); outArchive.write(new byte[] { 'A' }); outArchive.closeArchiveEntry(); - } finally { - outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { final SevenZArchiveEntry entry = archive.getNextEntry(); final SevenZMethodConfiguration m = entry.getContentMethods().iterator().next(); assertEquals(SevenZMethod.DELTA_FILTER, m.getMethod()); assertEquals(32, m.getOptions()); - } finally { - archive.close(); } } @Test public void getEntriesOfUnarchiveTest() throws IOException { - final SevenZFile sevenZFile = new SevenZFile(getFile("bla.7z")); - try { + try (SevenZFile sevenZFile = new SevenZFile(getFile("bla.7z"))) { final Iterable<SevenZArchiveEntry> entries = sevenZFile.getEntries(); final Iterator<SevenZArchiveEntry> iter = entries.iterator(); SevenZArchiveEntry entry = iter.next(); @@ -255,8 +236,6 @@ public class SevenZFileTest extends AbstractTestCase { entry = iter.next(); assertEquals("test2.xml", entry.getName()); assertFalse(iter.hasNext()); - } finally { - sevenZFile.close(); } } @@ -265,8 +244,7 @@ public class SevenZFileTest extends AbstractTestCase { */ @Test public void readEntriesOfSize0() throws IOException { - final SevenZFile sevenZFile = new SevenZFile(getFile("COMPRESS-348.7z")); - try { + try (SevenZFile sevenZFile = new SevenZFile(getFile("COMPRESS-348.7z"))) { int entries = 0; SevenZArchiveEntry entry = sevenZFile.getNextEntry(); while (entry != null) { @@ -280,50 +258,42 @@ public class SevenZFileTest extends AbstractTestCase { entry = sevenZFile.getNextEntry(); } assertEquals(5, entries); - } finally { - sevenZFile.close(); } } private void test7zUnarchive(final File f, final SevenZMethod m, final byte[] password) throws Exception { - final SevenZFile sevenZFile = new SevenZFile(f, password); - try { + try (SevenZFile sevenZFile = new SevenZFile(f, password)) { SevenZArchiveEntry entry = sevenZFile.getNextEntry(); assertEquals("test1.xml", entry.getName()); assertEquals(m, entry.getContentMethods().iterator().next().getMethod()); entry = sevenZFile.getNextEntry(); assertEquals("test2.xml", entry.getName()); assertEquals(m, entry.getContentMethods().iterator().next().getMethod()); - final byte[] contents = new byte[(int)entry.getSize()]; + final byte[] contents = new byte[(int) entry.getSize()]; int off = 0; while ((off < contents.length)) { final int bytesRead = sevenZFile.read(contents, off, contents.length - off); - assert(bytesRead >= 0); + assert (bytesRead >= 0); off += bytesRead; } assertEquals(TEST2_CONTENT, new String(contents, "UTF-8")); assertNull(sevenZFile.getNextEntry()); - } finally { - sevenZFile.close(); } } private void checkHelloWorld(final String filename) throws Exception { - final SevenZFile sevenZFile = new SevenZFile(getFile(filename)); - try { + try (SevenZFile sevenZFile = new SevenZFile(getFile(filename))) { final SevenZArchiveEntry entry = sevenZFile.getNextEntry(); assertEquals("Hello world.txt", entry.getName()); - final byte[] contents = new byte[(int)entry.getSize()]; + final byte[] contents = new byte[(int) entry.getSize()]; int off = 0; while ((off < contents.length)) { final int bytesRead = sevenZFile.read(contents, off, contents.length - off); - assert(bytesRead >= 0); + assert (bytesRead >= 0); off += bytesRead; } assertEquals("Hello, world!\n", new String(contents, "UTF-8")); assertNull(sevenZFile.getNextEntry()); - } finally { - sevenZFile.close(); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java index dd97190..989aa4d 100644 --- a/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java @@ -61,8 +61,7 @@ public class SevenZOutputFileTest extends AbstractTestCase { cal.add(Calendar.HOUR, -1); final Date creationDate = cal.getTime(); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { SevenZArchiveEntry entry = outArchive.createArchiveEntry(dir, "foo/"); outArchive.putArchiveEntry(entry); outArchive.closeArchiveEntry(); @@ -96,20 +95,17 @@ public class SevenZOutputFileTest extends AbstractTestCase { outArchive.closeArchiveEntry(); outArchive.finish(); - } finally { - outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { SevenZArchiveEntry entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("foo/", entry.getName()); assertTrue(entry.isDirectory()); assertFalse(entry.isAntiItem()); entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("foo/bar", entry.getName()); assertFalse(entry.isDirectory()); assertFalse(entry.isAntiItem()); @@ -119,7 +115,7 @@ public class SevenZOutputFileTest extends AbstractTestCase { assertEquals(creationDate, entry.getCreationDate()); entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("xyzzy", entry.getName()); assertEquals(1, entry.getSize()); assertFalse(entry.getHasAccessDate()); @@ -127,13 +123,13 @@ public class SevenZOutputFileTest extends AbstractTestCase { assertEquals(0, archive.read()); entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("baz/", entry.getName()); assertTrue(entry.isDirectory()); assertTrue(entry.isAntiItem()); entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("dada", entry.getName()); assertEquals(2, entry.getSize()); final byte[] content = new byte[2]; @@ -142,9 +138,7 @@ public class SevenZOutputFileTest extends AbstractTestCase { assertEquals(42, content[1]); assertEquals(17, entry.getWindowsAttributes()); - assert(archive.getNextEntry() == null); - } finally { - archive.close(); + assert (archive.getNextEntry() == null); } } @@ -152,28 +146,22 @@ public class SevenZOutputFileTest extends AbstractTestCase { @Test public void testDirectoriesOnly() throws Exception { output = new File(dir, "dirs.7z"); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { final SevenZArchiveEntry entry = new SevenZArchiveEntry(); entry.setName("foo/"); entry.setDirectory(true); outArchive.putArchiveEntry(entry); outArchive.closeArchiveEntry(); - } finally { - outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { final SevenZArchiveEntry entry = archive.getNextEntry(); - assert(entry != null); + assert (entry != null); assertEquals("foo/", entry.getName()); assertTrue(entry.isDirectory()); assertFalse(entry.isAntiItem()); - assert(archive.getNextEntry() == null); - } finally { - archive.close(); + assert (archive.getNextEntry() == null); } } @@ -181,15 +169,12 @@ public class SevenZOutputFileTest extends AbstractTestCase { @Test public void testCantFinishTwice() throws Exception { output = new File(dir, "finish.7z"); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { outArchive.finish(); outArchive.finish(); fail("shouldn't be able to call finish twice"); } catch (final IOException ex) { assertEquals("This archive has already been finished", ex.getMessage()); - } finally { - outArchive.close(); } } @@ -350,22 +335,16 @@ public class SevenZOutputFileTest extends AbstractTestCase { @Test public void testArchiveWithMixedMethods() throws Exception { output = new File(dir, "mixed-methods.7z"); - final SevenZOutputFile outArchive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile outArchive = new SevenZOutputFile(output)) { addFile(outArchive, 0, true); addFile(outArchive, 1, true, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.BZIP2))); - } finally { - outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { assertEquals(Boolean.TRUE, - verifyFile(archive, 0, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.LZMA2)))); + verifyFile(archive, 0, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.LZMA2)))); assertEquals(Boolean.TRUE, - verifyFile(archive, 1, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.BZIP2)))); - } finally { - archive.close(); + verifyFile(archive, 1, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.BZIP2)))); } } @@ -376,25 +355,21 @@ public class SevenZOutputFileTest extends AbstractTestCase { : numberOfFiles + 1; int nonEmptyFilesAdded = 0; output = new File(dir, "COMPRESS252-" + numberOfFiles + "-" + numberOfNonEmptyFiles + ".7z"); - final SevenZOutputFile archive = new SevenZOutputFile(output); - try { + try (SevenZOutputFile archive = new SevenZOutputFile(output)) { addDir(archive); for (int i = 0; i < numberOfFiles; i++) { addFile(archive, i, (i + 1) % nonEmptyModulus == 0 && nonEmptyFilesAdded++ < numberOfNonEmptyFiles); } - } finally { - archive.close(); } verifyCompress252(output, numberOfFiles, numberOfNonEmptyFiles); } private void verifyCompress252(final File output, final int numberOfFiles, final int numberOfNonEmptyFiles) throws Exception { - final SevenZFile archive = new SevenZFile(output); int filesFound = 0; int nonEmptyFilesFound = 0; - try { + try (SevenZFile archive = new SevenZFile(output)) { verifyDir(archive); Boolean b = verifyFile(archive, filesFound++); while (b != null) { @@ -403,8 +378,6 @@ public class SevenZOutputFileTest extends AbstractTestCase { } b = verifyFile(archive, filesFound++); } - } finally { - archive.close(); } assertEquals(numberOfFiles + 1, filesFound); assertEquals(numberOfNonEmptyFiles, nonEmptyFilesFound); @@ -486,11 +459,8 @@ public class SevenZOutputFileTest extends AbstractTestCase { outArchive.close(); } - final SevenZFile archive = new SevenZFile(output); - try { + try (SevenZFile archive = new SevenZFile(output)) { assertEquals(Boolean.TRUE, verifyFile(archive, 0, methods)); - } finally { - archive.close(); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStreamTest.java ---------------------------------------------------------------------- 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 161ee12..8e0c7a5 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 @@ -156,16 +156,13 @@ public class TarArchiveInputStreamTest { @Test public void testCompress197() throws Exception { - final TarArchiveInputStream tar = getTestStream("/COMPRESS-197.tar"); - try { + try (TarArchiveInputStream tar = getTestStream("/COMPRESS-197.tar")) { TarArchiveEntry entry = tar.getNextTarEntry(); while (entry != null) { entry = tar.getNextTarEntry(); } } catch (final IOException e) { fail("COMPRESS-197: " + e.getMessage()); - } finally { - tar.close(); } } @@ -214,9 +211,8 @@ public class TarArchiveInputStreamTest { @Test public void readsArchiveCompletely_COMPRESS245() throws Exception { - final InputStream is = TarArchiveInputStreamTest.class - .getResourceAsStream("/COMPRESS-245.tar.gz"); - try { + try (InputStream is = TarArchiveInputStreamTest.class + .getResourceAsStream("/COMPRESS-245.tar.gz")) { final InputStream gin = new GZIPInputStream(is); final TarArchiveInputStream tar = new TarArchiveInputStream(gin); int count = 0; @@ -229,8 +225,6 @@ public class TarArchiveInputStreamTest { tar.close(); } catch (final IOException e) { fail("COMPRESS-245: " + e.getMessage()); - } finally { - is.close(); } } @@ -285,16 +279,13 @@ public class TarArchiveInputStreamTest { */ @Test public void shouldReadGNULongNameEntryWithWrongName() throws Exception { - final TarArchiveInputStream is = getTestStream("/COMPRESS-324.tar"); - try { + try (TarArchiveInputStream is = getTestStream("/COMPRESS-324.tar")) { final TarArchiveEntry entry = is.getNextTarEntry(); assertEquals("1234567890123456789012345678901234567890123456789012345678901234567890" - + "1234567890123456789012345678901234567890123456789012345678901234567890" - + "1234567890123456789012345678901234567890123456789012345678901234567890" - + "1234567890123456789012345678901234567890.txt", - entry.getName()); - } finally { - is.close(); + + "1234567890123456789012345678901234567890123456789012345678901234567890" + + "1234567890123456789012345678901234567890123456789012345678901234567890" + + "1234567890123456789012345678901234567890.txt", + entry.getName()); } } @@ -303,13 +294,10 @@ public class TarArchiveInputStreamTest { */ @Test public void survivesBlankLinesInPaxHeader() throws Exception { - final TarArchiveInputStream is = getTestStream("/COMPRESS-355.tar"); - try { + try (TarArchiveInputStream is = getTestStream("/COMPRESS-355.tar")) { final TarArchiveEntry entry = is.getNextTarEntry(); assertEquals("package/package.json", entry.getName()); assertNull(is.getNextTarEntry()); - } finally { - is.close(); } } @@ -318,13 +306,10 @@ public class TarArchiveInputStreamTest { */ @Test public void survivesPaxHeaderWithNameEndingInSlash() throws Exception { - final TarArchiveInputStream is = getTestStream("/COMPRESS-356.tar"); - try { + try (TarArchiveInputStream is = getTestStream("/COMPRESS-356.tar")) { final TarArchiveEntry entry = is.getNextTarEntry(); assertEquals("package/package.json", entry.getName()); assertNull(is.getNextTarEntry()); - } finally { - is.close(); } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/zip/Lister.java ---------------------------------------------------------------------- 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 6638648..a633715 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 @@ -54,12 +54,10 @@ public final class Lister { usage(); } if (cl.useStream) { - final BufferedInputStream fs = - new BufferedInputStream(new FileInputStream(f)); - try { + try (BufferedInputStream fs = new BufferedInputStream(new FileInputStream(f))) { final ZipArchiveInputStream zs = - new ZipArchiveInputStream(fs, cl.encoding, true, - cl.allowStoredEntriesWithDataDescriptor); + new ZipArchiveInputStream(fs, cl.encoding, true, + cl.allowStoredEntriesWithDataDescriptor); for (ArchiveEntry entry = zs.getNextEntry(); entry != null; entry = zs.getNextEntry()) { @@ -69,27 +67,19 @@ public final class Lister { extract(cl.dir, ze, zs); } } - } finally { - fs.close(); } } else { - final ZipFile zf = new ZipFile(f, cl.encoding); - try { + try (ZipFile zf = new ZipFile(f, cl.encoding)) { for (final Enumeration<ZipArchiveEntry> entries = zf.getEntries(); entries.hasMoreElements(); ) { final ZipArchiveEntry ze = entries.nextElement(); list(ze); if (cl.dir != null) { - final InputStream is = zf.getInputStream(ze); - try { + try (InputStream is = zf.getInputStream(ze)) { extract(cl.dir, ze, is); - } finally { - is.close(); } } } - } finally { - zf.close(); } } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportIT.java ---------------------------------------------------------------------- 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 6a39623..783ed46 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 @@ -211,15 +211,14 @@ public class Zip64SupportIT { zos.setUseZip64(mode); } write100KFilesToStream(zos); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { final long end = a.length(); // validate "end of central directory" is at // the end of the file and contains the magic // value 0xFFFF as "number of entries". a.seek(end - - 22 /* length of EOCD without file comment */); + - 22 /* length of EOCD without file comment */); final byte[] eocd = new byte[12]; a.readFully(eocd); assertArrayEquals(new byte[] { @@ -230,17 +229,17 @@ public class Zip64SupportIT { // entries (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, - }, eocd); + }, eocd); // validate "Zip64 end of central directory // locator" is right in front of the EOCD and // the location of the "Zip64 end of central // directory record" seems correct final long expectedZ64EocdOffset = end - 22 /* eocd.length */ - - 20 /* z64 eocd locator.length */ - - 56 /* z64 eocd without extensible data sector */; + - 20 /* z64 eocd locator.length */ + - 56 /* z64 eocd without extensible data sector */; final byte[] loc = - ZipEightByteInteger.getBytes(expectedZ64EocdOffset); + ZipEightByteInteger.getBytes(expectedZ64EocdOffset); a.seek(end - 22 - 20); final byte[] z64EocdLoc = new byte[20]; a.readFully(z64EocdLoc); @@ -254,7 +253,7 @@ public class Zip64SupportIT { loc[4], loc[5], loc[6], loc[7], // total number of disks 1, 0, 0, 0, - }, z64EocdLoc); + }, z64EocdLoc); // validate "Zip64 end of central directory // record" is where it is supposed to be, the @@ -281,7 +280,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, (byte) 0xA0, (byte) 0x86, 1, 0, 0, 0, 0, 0, - }, z64EocdStart); + }, z64EocdStart); a.seek(expectedZ64EocdOffset + 48 /* skip size */); final byte[] cdOffset = new byte[8]; a.readFully(cdOffset); @@ -294,9 +293,7 @@ public class Zip64SupportIT { a.readFully(sig); assertArrayEquals(new byte[] { (byte) 0x50, (byte) 0x4b, 1, 2, - }, sig); - } finally { - a.close(); + }, sig); } } }; @@ -383,16 +380,15 @@ public class Zip64SupportIT { } write3EntriesCreatingBigArchiveToStream(zos); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // skip first two entries a.skipBytes(2 * 47 /* CD entry of file with file name length 1 and no extra data */ - + 2 * (mode == Zip64Mode.Always ? 4 : 0) + + 2 * (mode == Zip64Mode.Always ? 4 : 0) /* empty ZIP64 extra fields if mode is Always */ - ); + ); // grab third entry, verify offset is // 0xFFFFFFFF and it has a ZIP64 extended @@ -410,7 +406,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp, CRC, compressed size a.skipBytes(12); final byte[] rest = new byte[23]; @@ -434,7 +430,7 @@ public class Zip64SupportIT { (byte) 0xFF, (byte) 0xFF, // file name (byte) '2' - }, rest); + }, rest); final byte[] extra = new byte[4]; a.readFully(extra); assertArrayEquals(new byte[] { @@ -442,7 +438,7 @@ public class Zip64SupportIT { 1, 0, // size 8, 0 - }, extra); + }, extra); // read offset of LFH final byte[] offset = new byte[8]; @@ -453,9 +449,7 @@ public class Zip64SupportIT { a.readFully(sig); assertArrayEquals(new byte[] { (byte) 0x50, (byte) 0x4b, 3, 4, - }, sig); - } finally { - a.close(); + }, sig); } } }; @@ -542,13 +536,9 @@ public class Zip64SupportIT { } else { assertEquals(1, zae.getSize()); - final InputStream i = - zf.getInputStream(zae); - try { + try (InputStream i = zf.getInputStream(zae)) { assertNotNull(i); assertEquals(42, i.read()); - } finally { - i.close(); } } } @@ -596,8 +586,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first entry, verify sizes are 0xFFFFFFFF @@ -616,7 +605,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] rest = new byte[31]; @@ -643,7 +632,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); final byte[] extra = new byte[20]; a.readFully(extra); // 5e9 == 0x12A05F200 @@ -658,7 +647,7 @@ public class Zip64SupportIT { // compressed size 0, (byte) 0xF2, 5, (byte) 0x2A, 1, 0, 0, 0, - }, extra); + }, extra); // and now validate local file header a.seek(0); @@ -673,7 +662,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -691,7 +680,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); a.readFully(extra); // 5e9 == 0x12A05F200 assertArrayEquals(new byte[] { @@ -705,9 +694,7 @@ public class Zip64SupportIT { // compressed size 0, (byte) 0xF2, 5, (byte) 0x2A, 1, 0, 0, 0, - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -836,9 +823,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = - new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); final long cfhPos = a.getFilePointer(); @@ -859,7 +844,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] rest = new byte[31]; @@ -886,7 +871,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); final byte[] extra = new byte[20]; a.readFully(extra); // 5e9 == 0x12A05F200 @@ -901,7 +886,7 @@ public class Zip64SupportIT { // compressed size (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0, 0, 0, 0, 0, - }, extra); + }, extra); // validate data descriptor a.seek(cfhPos - 24); @@ -912,7 +897,7 @@ public class Zip64SupportIT { (byte) 0x50, (byte) 0x4b, 7, 8, // CRC (byte) 0x50, (byte) 0x6F, (byte) 0x31, (byte) 0x5c, - }, dd); + }, dd); dd = new byte[16]; a.readFully(dd); assertArrayEquals(new byte[] { @@ -922,7 +907,7 @@ public class Zip64SupportIT { // original size 0, (byte) 0xF2, 5, (byte) 0x2A, 1, 0, 0, 0, - }, dd); + }, dd); // and now validate local file header a.seek(0); @@ -937,7 +922,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -955,7 +940,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); a.readFully(extra); assertArrayEquals(new byte[] { // Header-ID @@ -968,9 +953,7 @@ public class Zip64SupportIT { // compressed size 0, 0, 0, 0, 0, 0, 0, 0, - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -1081,8 +1064,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first entry, verify @@ -1102,7 +1084,7 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] rest = new byte[31]; @@ -1129,7 +1111,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); byte[] extra = new byte[20]; a.readFully(extra); // 5e9 == 0x12A05F200 @@ -1144,7 +1126,7 @@ public class Zip64SupportIT { // compressed size (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0, 0, 0, 0, 0, - }, extra); + }, extra); // and now validate local file header a.seek(0); @@ -1159,7 +1141,7 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -1177,7 +1159,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); extra = new byte[20]; a.readFully(extra); assertArrayEquals(new byte[] { @@ -1191,9 +1173,7 @@ public class Zip64SupportIT { // compressed size (byte) 0x68, (byte) 0x27, (byte) 0x4A, 0, 0, 0, 0, 0, - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -1340,8 +1320,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first CF entry, verify sizes are 1e6 and it @@ -1360,7 +1339,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] rest = new byte[31]; @@ -1388,14 +1367,14 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); // and now validate local file header: this one // has a ZIP64 extra field if and only if size was // unknown and mode was not Never or the mode was // Always (regardless of size) final boolean hasExtra = mode == Zip64Mode.Always - || (mode == Zip64Mode.AsNeeded && !knownSize); + || (mode == Zip64Mode.AsNeeded && !knownSize); a.seek(0); header = new byte[10]; a.readFully(header); @@ -1408,7 +1387,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -1427,7 +1406,7 @@ public class Zip64SupportIT { (byte) (!hasExtra ? 0 : 20), 0, // file name (byte) '0' - }, rest); + }, rest); if (hasExtra) { final byte[] extra = new byte[20]; a.readFully(extra); @@ -1442,10 +1421,8 @@ public class Zip64SupportIT { // compressed size (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0, 0, 0, 0, 0, - }, extra); + }, extra); } - } finally { - a.close(); } } }; @@ -1517,8 +1494,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first CF entry, verify sizes are 1e6 and it @@ -1536,7 +1512,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] rest = new byte[31]; @@ -1564,7 +1540,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); byte[] extra = new byte[4]; a.readFully(extra); @@ -1573,7 +1549,7 @@ public class Zip64SupportIT { 1, 0, // size of extra 0, 0, - }, extra); + }, extra); // and now validate local file header: this one // has a ZIP64 extra field as the mode was @@ -1590,7 +1566,7 @@ public class Zip64SupportIT { 0, 8, // method 0, 0 - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -1609,7 +1585,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); extra = new byte[20]; a.readFully(extra); @@ -1624,9 +1600,7 @@ public class Zip64SupportIT { // compressed size (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0, 0, 0, 0, 0, - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -1681,8 +1655,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); final long cfhPos = a.getFilePointer(); @@ -1702,7 +1675,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); final byte[] crc = new byte[4]; @@ -1710,7 +1683,7 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); // skip compressed size a.skipBytes(4); byte[] rest = new byte[23]; @@ -1734,7 +1707,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); // validate data descriptor a.seek(cfhPos - 16); @@ -1745,7 +1718,7 @@ public class Zip64SupportIT { (byte) 0x50, (byte) 0x4b, 7, 8, // CRC (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, dd); + }, dd); // skip uncompressed size a.skipBytes(4); dd = new byte[4]; @@ -1753,7 +1726,7 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { // original size (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0, - }, dd); + }, dd); // and now validate local file header a.seek(0); @@ -1768,7 +1741,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -1786,9 +1759,7 @@ public class Zip64SupportIT { 0, 0, // file name (byte) '0' - }, rest); - } finally { - a.close(); + }, rest); } } }; @@ -1852,8 +1823,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); final long cfhPos = a.getFilePointer(); @@ -1873,7 +1843,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); final byte[] crc = new byte[4]; @@ -1881,7 +1851,7 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); // skip compressed size a.skipBytes(4); byte[] rest = new byte[23]; @@ -1905,7 +1875,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); byte[] extra = new byte[4]; a.readFully(extra); assertArrayEquals(new byte[] { @@ -1913,7 +1883,7 @@ public class Zip64SupportIT { 1, 0, // size of extra 0, 0, - }, extra); + }, extra); // validate data descriptor a.seek(cfhPos - 24); @@ -1924,7 +1894,7 @@ public class Zip64SupportIT { (byte) 0x50, (byte) 0x4b, 7, 8, // CRC (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, dd); + }, dd); // skip compressed size a.skipBytes(8); dd = new byte[8]; @@ -1933,7 +1903,7 @@ public class Zip64SupportIT { // original size (byte) 0x40, (byte) 0x42, (byte) 0x0F, 0, 0, 0, 0, 0 - }, dd); + }, dd); // and now validate local file header a.seek(0); @@ -1948,7 +1918,7 @@ public class Zip64SupportIT { 8, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); rest = new byte[17]; @@ -1966,7 +1936,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); extra = new byte[20]; a.readFully(extra); @@ -1981,9 +1951,7 @@ public class Zip64SupportIT { // compressed size 0, 0, 0, 0, 0, 0, 0, 0, - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -2037,8 +2005,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first CD entry, verify sizes are not @@ -2057,7 +2024,7 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] crc = new byte[4]; @@ -2065,7 +2032,7 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); // skip compressed size a.skipBytes(4); byte[] rest = new byte[23]; @@ -2088,7 +2055,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); // and now validate local file header a.seek(0); @@ -2103,7 +2070,7 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); crc = new byte[4]; @@ -2111,14 +2078,14 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); // skip compressed size a.skipBytes(4); rest = new byte[9]; a.readFully(rest); - final boolean hasExtra = - mode == Zip64Mode.AsNeeded && !knownSize; + final boolean hasExtra = + mode == Zip64Mode.AsNeeded && !knownSize; assertArrayEquals(new byte[] { // Original Size @@ -2129,7 +2096,7 @@ public class Zip64SupportIT { (byte) (!hasExtra ? 0 : 20), 0, // file name (byte) '0' - }, rest); + }, rest); if (hasExtra) { final byte[] extra = new byte[12]; a.readFully(extra); @@ -2145,10 +2112,8 @@ public class Zip64SupportIT { // compressed size, // don't want to // hard-code it - }, extra); + }, extra); } - } finally { - a.close(); } } }; @@ -2210,8 +2175,7 @@ public class Zip64SupportIT { zos.closeArchiveEntry(); zos.close(); - final RandomAccessFile a = new RandomAccessFile(f, "r"); - try { + try (RandomAccessFile a = new RandomAccessFile(f, "r")) { getLengthAndPositionAtCentralDirectory(a); // grab first CD entry, verify sizes are not @@ -2230,14 +2194,14 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); byte[] crc = new byte[4]; a.readFully(crc); assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); // skip compressed size a.skipBytes(4); byte[] rest = new byte[23]; @@ -2260,7 +2224,7 @@ public class Zip64SupportIT { 0, 0, 0, 0, // file name (byte) '0' - }, rest); + }, rest); byte[] extra = new byte[4]; a.readFully(extra); assertArrayEquals(new byte[] { @@ -2268,7 +2232,7 @@ public class Zip64SupportIT { 1, 0, // size of extra 0, 0, - }, extra); + }, extra); // and now validate local file header a.seek(0); @@ -2283,7 +2247,7 @@ public class Zip64SupportIT { 0, 8, // method 8, 0, - }, header); + }, header); // ignore timestamp a.skipBytes(4); crc = new byte[4]; @@ -2291,7 +2255,7 @@ public class Zip64SupportIT { assertArrayEquals(new byte[] { (byte) 0x9E, (byte) 0xCB, (byte) 0x79, (byte) 0x12, - }, crc); + }, crc); rest = new byte[13]; a.readFully(rest); @@ -2306,7 +2270,7 @@ public class Zip64SupportIT { 20, 0, // file name (byte) '0' - }, rest); + }, rest); extra = new byte[12]; a.readFully(extra); @@ -2322,9 +2286,7 @@ public class Zip64SupportIT { // compressed size, // don't want to // hard-code it - }, extra); - } finally { - a.close(); + }, extra); } } }; @@ -2479,8 +2441,7 @@ public class Zip64SupportIT { long read = 0; final Random r = new Random(System.currentTimeMillis()); int readNow; - final InputStream zin = zf.getInputStream(zae); - try { + try (InputStream zin = zf.getInputStream(zae)) { while ((readNow = zin.read(buf, 0, buf.length)) > 0) { // testing all bytes for a value of 0 is going to take // too long, just pick a few ones randomly @@ -2490,8 +2451,6 @@ public class Zip64SupportIT { } read += readNow; } - } finally { - zin.close(); } assertEquals(FIVE_BILLION, read); assertFalse(e.hasMoreElements()); http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStreamTest.java ---------------------------------------------------------------------- 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 587daf0..931387c 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 @@ -161,15 +161,12 @@ public class ZipArchiveInputStreamTest { */ @Test public void testReadingOfFirstStoredEntry() throws Exception { - final ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-264.zip"))); - - try { + + try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-264.zip")))) { final ZipArchiveEntry ze = in.getNextZipEntry(); assertEquals(5, ze.getSize()); - assertArrayEquals(new byte[] {'d', 'a', 't', 'a', '\n'}, - IOUtils.toByteArray(in)); - } finally { - in.close(); + assertArrayEquals(new byte[] { 'd', 'a', 't', 'a', '\n' }, + IOUtils.toByteArray(in)); } } @@ -180,8 +177,7 @@ public class ZipArchiveInputStreamTest { */ @Test public void testMessageWithCorruptFileName() throws Exception { - final ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-351.zip"))); - try { + try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-351.zip")))) { ZipArchiveEntry ze = in.getNextZipEntry(); while (ze != null) { ze = in.getNextZipEntry(); @@ -190,23 +186,18 @@ public class ZipArchiveInputStreamTest { } catch (final EOFException ex) { final String m = ex.getMessage(); assertTrue(m.startsWith("Truncated ZIP entry: ?2016")); // the first character is not printable - } finally { - in.close(); } } @Test public void testUnzipBZip2CompressedEntry() throws Exception { - final ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("bzip2-zip.zip"))); - - try { + + try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("bzip2-zip.zip")))) { final ZipArchiveEntry ze = in.getNextZipEntry(); assertEquals(42, ze.getSize()); final byte[] expected = new byte[42]; - Arrays.fill(expected , (byte)'a'); + Arrays.fill(expected, (byte) 'a'); assertArrayEquals(expected, IOUtils.toByteArray(in)); - } finally { - in.close(); } } } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/32c30f6f/src/test/java/org/apache/commons/compress/compressors/BZip2TestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/compressors/BZip2TestCase.java b/src/test/java/org/apache/commons/compress/compressors/BZip2TestCase.java index 0ae8dee..88aca8d 100644 --- a/src/test/java/org/apache/commons/compress/compressors/BZip2TestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/BZip2TestCase.java @@ -77,59 +77,39 @@ public final class BZip2TestCase extends AbstractTestCase { @Test public void testConcatenatedStreamsReadFirstOnly() throws Exception { final File input = getFile("multiple.bz2"); - final InputStream is = new FileInputStream(input); - try { - final CompressorInputStream in = new CompressorStreamFactory() - .createCompressorInputStream("bzip2", is); - try { + try (InputStream is = new FileInputStream(input)) { + try (CompressorInputStream in = new CompressorStreamFactory() + .createCompressorInputStream("bzip2", is)) { assertEquals('a', in.read()); assertEquals(-1, in.read()); - } finally { - in.close(); } - } finally { - is.close(); } } @Test public void testConcatenatedStreamsReadFully() throws Exception { final File input = getFile("multiple.bz2"); - final InputStream is = new FileInputStream(input); - try { - final CompressorInputStream in = - new BZip2CompressorInputStream(is, true); - try { + try (InputStream is = new FileInputStream(input)) { + try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) { assertEquals('a', in.read()); assertEquals('b', in.read()); assertEquals(0, in.available()); assertEquals(-1, in.read()); - } finally { - in.close(); } - } finally { - is.close(); } } @Test public void testCOMPRESS131() throws Exception { final File input = getFile("COMPRESS-131.bz2"); - final InputStream is = new FileInputStream(input); - try { - final CompressorInputStream in = - new BZip2CompressorInputStream(is, true); - try { + try (InputStream is = new FileInputStream(input)) { + try (CompressorInputStream in = new BZip2CompressorInputStream(is, true)) { int l = 0; - while(in.read() != -1) { + while (in.read() != -1) { l++; } assertEquals(539, l); - } finally { - in.close(); } - } finally { - is.close(); } }
