More java7 language features
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 @@
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());
}
}
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 @@
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));
}
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 @@
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;
}
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 @@
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) {
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 @@
*/
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 @@
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 @@
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 };
}
}
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 @@
*/
@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 @@
*/
@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 @@
*/
@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 @@
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 @@
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();
}
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 @@
}
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 @@
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);
}
}
}
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 @@
} 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);
}
}
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 @@
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 @@
@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 @@
@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 @@
e.printStackTrace();
fail("COMPRESS-93: Unable to skip an unsupported zip entry");
}
- } finally {
- zip.close();
}
}
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 @@
@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();
}
}
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 @@
@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 @@
*/
@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 @@
@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 @@
entry = iter.next();
assertEquals("test2.xml", entry.getName());
assertFalse(iter.hasNext());
- } finally {
- sevenZFile.close();
}
}
@@ -265,8 +244,7 @@
*/
@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 @@
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();
}
}
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 @@
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 @@
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 @@
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 @@
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 @@
assertEquals(42, content[1]);
assertEquals(17, entry.getWindowsAttributes());
- assert(archive.getNextEntry() == null);
- } finally {
- archive.close();
+ assert (archive.getNextEntry() == null);
}
}
@@ -152,28 +146,22 @@
@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 @@
@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 @@
@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 @@
: 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 @@
}
b = verifyFile(archive, filesFound++);
}
- } finally {
- archive.close();
}
assertEquals(numberOfFiles + 1, filesFound);
assertEquals(numberOfNonEmptyFiles, nonEmptyFilesFound);
@@ -486,11 +459,8 @@
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();
}
}
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 @@
@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 @@
@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 @@
tar.close();
} catch (final IOException e) {
fail("COMPRESS-245: " + e.getMessage());
- } finally {
- is.close();
}
}
@@ -285,16 +279,13 @@
*/
@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 @@
*/
@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 @@
*/
@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();
}
}
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 @@
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 @@
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();
}
}
}
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 @@
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 @@
// 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 @@
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 @@
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 @@
a.readFully(sig);
assertArrayEquals(new byte[] {
(byte) 0x50, (byte) 0x4b, 1, 2,
- }, sig);
- } finally {
- a.close();
+ }, sig);
}
}
};
@@ -383,16 +380,15 @@
}
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 @@
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 @@
(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 @@
1, 0,
// size
8, 0
- }, extra);
+ }, extra);
// read offset of LFH
final byte[] offset = new byte[8];
@@ -453,9 +449,7 @@
a.readFully(sig);
assertArrayEquals(new byte[] {
(byte) 0x50, (byte) 0x4b, 3, 4,
- }, sig);
- } finally {
- a.close();
+ }, sig);
}
}
};
@@ -542,13 +536,9 @@
} 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 @@
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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] rest = new byte[31];
@@ -643,7 +632,7 @@
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 @@
// 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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -691,7 +680,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
a.readFully(extra);
// 5e9 == 0x12A05F200
assertArrayEquals(new byte[] {
@@ -705,9 +694,7 @@
// compressed size
0, (byte) 0xF2, 5, (byte) 0x2A,
1, 0, 0, 0,
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -836,9 +823,7 @@
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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] rest = new byte[31];
@@ -886,7 +871,7 @@
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 @@
// 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 @@
(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 @@
// 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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -955,7 +940,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
a.readFully(extra);
assertArrayEquals(new byte[] {
// Header-ID
@@ -968,9 +953,7 @@
// compressed size
0, 0, 0, 0,
0, 0, 0, 0,
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -1081,8 +1064,7 @@
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 @@
0, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] rest = new byte[31];
@@ -1129,7 +1111,7 @@
0, 0, 0, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
byte[] extra = new byte[20];
a.readFully(extra);
// 5e9 == 0x12A05F200
@@ -1144,7 +1126,7 @@
// 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 @@
0, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -1177,7 +1159,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
extra = new byte[20];
a.readFully(extra);
assertArrayEquals(new byte[] {
@@ -1191,9 +1173,7 @@
// compressed size
(byte) 0x68, (byte) 0x27, (byte) 0x4A, 0,
0, 0, 0, 0,
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -1340,8 +1320,7 @@
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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] rest = new byte[31];
@@ -1388,14 +1367,14 @@
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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -1427,7 +1406,7 @@
(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 @@
// compressed size
(byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
0, 0, 0, 0,
- }, extra);
+ }, extra);
}
- } finally {
- a.close();
}
}
};
@@ -1517,8 +1494,7 @@
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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] rest = new byte[31];
@@ -1564,7 +1540,7 @@
0, 0, 0, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
byte[] extra = new byte[4];
a.readFully(extra);
@@ -1573,7 +1549,7 @@
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 @@
0, 8,
// method
0, 0
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -1609,7 +1585,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
extra = new byte[20];
a.readFully(extra);
@@ -1624,9 +1600,7 @@
// compressed size
(byte) 0x40, (byte) 0x42, (byte) 0x0F, 0,
0, 0, 0, 0,
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -1681,8 +1655,7 @@
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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
final byte[] crc = new byte[4];
@@ -1710,7 +1683,7 @@
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 @@
0, 0, 0, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
// validate data descriptor
a.seek(cfhPos - 16);
@@ -1745,7 +1718,7 @@
(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 @@
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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -1786,9 +1759,7 @@
0, 0,
// file name
(byte) '0'
- }, rest);
- } finally {
- a.close();
+ }, rest);
}
}
};
@@ -1852,8 +1823,7 @@
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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
final byte[] crc = new byte[4];
@@ -1881,7 +1851,7 @@
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 @@
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 @@
1, 0,
// size of extra
0, 0,
- }, extra);
+ }, extra);
// validate data descriptor
a.seek(cfhPos - 24);
@@ -1924,7 +1894,7 @@
(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 @@
// 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 @@
8, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
rest = new byte[17];
@@ -1966,7 +1936,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
extra = new byte[20];
a.readFully(extra);
@@ -1981,9 +1951,7 @@
// compressed size
0, 0, 0, 0,
0, 0, 0, 0,
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -2037,8 +2005,7 @@
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 @@
0, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
byte[] crc = new byte[4];
@@ -2065,7 +2032,7 @@
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 @@
0, 0, 0, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
// and now validate local file header
a.seek(0);
@@ -2103,7 +2070,7 @@
0, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
crc = new byte[4];
@@ -2111,14 +2078,14 @@
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 @@
(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 @@
// compressed size,
// don't want to
// hard-code it
- }, extra);
+ }, extra);
}
- } finally {
- a.close();
}
}
};
@@ -2210,8 +2175,7 @@
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 @@
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 @@
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 @@
1, 0,
// size of extra
0, 0,
- }, extra);
+ }, extra);
// and now validate local file header
a.seek(0);
@@ -2283,7 +2247,7 @@
0, 8,
// method
8, 0,
- }, header);
+ }, header);
// ignore timestamp
a.skipBytes(4);
crc = new byte[4];
@@ -2291,7 +2255,7 @@
assertArrayEquals(new byte[] {
(byte) 0x9E, (byte) 0xCB,
(byte) 0x79, (byte) 0x12,
- }, crc);
+ }, crc);
rest = new byte[13];
a.readFully(rest);
@@ -2306,7 +2270,7 @@
20, 0,
// file name
(byte) '0'
- }, rest);
+ }, rest);
extra = new byte[12];
a.readFully(extra);
@@ -2322,9 +2286,7 @@
// compressed size,
// don't want to
// hard-code it
- }, extra);
- } finally {
- a.close();
+ }, extra);
}
}
};
@@ -2479,8 +2441,7 @@
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 @@
}
read += readNow;
}
- } finally {
- zin.close();
}
assertEquals(FIVE_BILLION, read);
assertFalse(e.hasMoreElements());
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 @@
*/
@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 @@
*/
@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 @@
} 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();
}
}
}
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 @@
@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();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/DeflateTestCase.java b/src/test/java/org/apache/commons/compress/compressors/DeflateTestCase.java
index b713ed3..81f0d77 100644
--- a/src/test/java/org/apache/commons/compress/compressors/DeflateTestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/DeflateTestCase.java
@@ -42,17 +42,11 @@
public void testDeflateCreation() throws Exception {
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.deflatez");
- final OutputStream out = new FileOutputStream(output);
- try {
- final CompressorOutputStream cos = new CompressorStreamFactory()
- .createCompressorOutputStream("deflate", out); // zlib header is used by default
- try {
+ try (OutputStream out = new FileOutputStream(output)) {
+ try (CompressorOutputStream cos = new CompressorStreamFactory()
+ .createCompressorOutputStream("deflate", out)) {
IOUtils.copy(new FileInputStream(input), cos);
- } finally {
- cos.close();
}
- } finally {
- out.close();
}
}
@@ -65,18 +59,12 @@
public void testRawDeflateCreation() throws Exception {
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.deflate");
- final OutputStream out = new FileOutputStream(output);
- try {
+ try (OutputStream out = new FileOutputStream(output)) {
final DeflateParameters params = new DeflateParameters();
params.setWithZlibHeader(false);
- final CompressorOutputStream cos = new DeflateCompressorOutputStream(out, params);
- try {
+ try (CompressorOutputStream cos = new DeflateCompressorOutputStream(out, params)) {
IOUtils.copy(new FileInputStream(input), cos);
- } finally {
- cos.close();
}
- } finally {
- out.close();
}
}
@@ -89,10 +77,9 @@
public void testDeflateUnarchive() throws Exception {
final File input = getFile("bla.tar.deflatez");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream("deflate", is); // zlib header is expected by default
+ .createCompressorInputStream("deflate", is); // zlib header is expected by default
FileOutputStream out = null;
try {
out = new FileOutputStream(output);
@@ -103,8 +90,6 @@
}
in.close();
}
- } finally {
- is.close();
}
}
@@ -117,8 +102,7 @@
public void testRawDeflateUnarchive() throws Exception {
final File input = getFile("bla.tar.deflate");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final DeflateParameters params = new DeflateParameters();
params.setWithZlibHeader(false);
final CompressorInputStream in = new DeflateCompressorInputStream(is, params);
@@ -132,8 +116,6 @@
}
in.close();
}
- } finally {
- is.close();
}
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java b/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
index f8f04c4..9af2518 100644
--- a/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/FramedSnappyTestCase.java
@@ -70,8 +70,7 @@
private void testUnarchive(final StreamWrapper<CompressorInputStream> wrapper) throws Exception {
final File input = getFile("bla.tar.sz");
final File output = new File(dir, "bla.tar");
- final FileInputStream is = new FileInputStream(input);
- try {
+ try (FileInputStream is = new FileInputStream(input)) {
// the intermediate BufferedInputStream is there for mark
// support in the autodetection test
final CompressorInputStream in = wrapper.wrap(new BufferedInputStream(is));
@@ -86,21 +85,13 @@
}
in.close();
}
- } finally {
- is.close();
}
final File original = getFile("bla.tar");
- final FileInputStream written = new FileInputStream(output);
- try {
- final FileInputStream orig = new FileInputStream(original);
- try {
+ try (FileInputStream written = new FileInputStream(output)) {
+ try (FileInputStream orig = new FileInputStream(original)) {
assertArrayEquals(IOUtils.toByteArray(written),
- IOUtils.toByteArray(orig));
- } finally {
- orig.close();
+ IOUtils.toByteArray(orig));
}
- } finally {
- written.close();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java b/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
index 624abeb..62c7b15 100644
--- a/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/GZipTestCase.java
@@ -45,17 +45,11 @@
public void testGzipCreation() throws Exception {
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.gz");
- final OutputStream out = new FileOutputStream(output);
- try {
- final CompressorOutputStream cos = new CompressorStreamFactory()
- .createCompressorOutputStream("gz", out);
- try {
+ try (OutputStream out = new FileOutputStream(output)) {
+ try (CompressorOutputStream cos = new CompressorStreamFactory()
+ .createCompressorOutputStream("gz", out)) {
IOUtils.copy(new FileInputStream(input), cos);
- } finally {
- cos.close();
}
- } finally {
- out.close();
}
}
@@ -63,10 +57,9 @@
public void testGzipUnarchive() throws Exception {
final File input = getFile("bla.tgz");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream("gz", is);
+ .createCompressorInputStream("gz", is);
FileOutputStream out = null;
try {
out = new FileOutputStream(output);
@@ -77,46 +70,31 @@
}
in.close();
}
- } finally {
- is.close();
}
}
@Test
public void testConcatenatedStreamsReadFirstOnly() throws Exception {
final File input = getFile("multiple.gz");
- final InputStream is = new FileInputStream(input);
- try {
- final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream("gz", is);
- try {
+ try (InputStream is = new FileInputStream(input)) {
+ try (CompressorInputStream in = new CompressorStreamFactory()
+ .createCompressorInputStream("gz", 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.gz");
- final InputStream is = new FileInputStream(input);
- try {
- final CompressorInputStream in =
- new GzipCompressorInputStream(is, true);
- try {
+ try (InputStream is = new FileInputStream(input)) {
+ try (CompressorInputStream in = new GzipCompressorInputStream(is, true)) {
assertEquals('a', in.read());
assertEquals('b', in.read());
assertEquals(0, in.available());
assertEquals(-1, in.read());
- } finally {
- in.close();
}
- } finally {
- is.close();
}
}
@@ -163,12 +141,9 @@
@Test
public void testInteroperabilityWithGzipCompressorInputStream() throws Exception {
- final FileInputStream fis = new FileInputStream(getFile("test3.xml"));
byte[] content;
- try {
+ try (FileInputStream fis = new FileInputStream(getFile("test3.xml"))) {
content = IOUtils.toByteArray(fis);
- } finally {
- fis.close();
}
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -192,12 +167,9 @@
@Test
public void testInteroperabilityWithGZIPInputStream() throws Exception {
- final FileInputStream fis = new FileInputStream(getFile("test3.xml"));
byte[] content;
- try {
+ try (FileInputStream fis = new FileInputStream(getFile("test3.xml"))) {
content = IOUtils.toByteArray(fis);
- } finally {
- fis.close();
}
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -238,12 +210,9 @@
}
private void testExtraFlags(final int compressionLevel, final int flag) throws Exception {
- final FileInputStream fis = new FileInputStream(getFile("test3.xml"));
byte[] content;
- try {
+ try (FileInputStream fis = new FileInputStream(getFile("test3.xml"))) {
content = IOUtils.toByteArray(fis);
- } finally {
- fis.close();
}
final ByteArrayOutputStream bout = new ByteArrayOutputStream();
@@ -295,13 +264,9 @@
parameters.setOperatingSystem(13);
parameters.setFilename("test3.xml");
parameters.setComment("Umlaute möglich?");
- final GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
- final FileInputStream fis = new FileInputStream(getFile("test3.xml"));
- try {
+ try (GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters); FileInputStream fis = new FileInputStream(getFile("test3" +
+ ".xml"))) {
IOUtils.copy(fis, out);
- } finally {
- fis.close();
- out.close();
}
final GzipCompressorInputStream input =
diff --git a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java
index 12381bd..a880cf3 100644
--- a/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/LZMATestCase.java
@@ -36,12 +36,9 @@
public void testLZMAUnarchive() throws Exception {
final File input = getFile("bla.tar.lzma");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final CompressorInputStream in = new LZMACompressorInputStream(is);
copy(in, output);
- } finally {
- is.close();
}
}
@@ -49,13 +46,10 @@
public void testLZMAUnarchiveWithAutodetection() throws Exception {
final File input = getFile("bla.tar.lzma");
final File output = new File(dir, "bla.tar");
- final InputStream is = new BufferedInputStream(new FileInputStream(input));
- try {
+ try (InputStream is = new BufferedInputStream(new FileInputStream(input))) {
final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream(is);
+ .createCompressorInputStream(is);
copy(in, output);
- } finally {
- is.close();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
index 6c11dd0..1896d10 100644
--- a/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/Pack200TestCase.java
@@ -67,13 +67,12 @@
private void jarUnarchiveAll(final boolean useFile, final Pack200Strategy mode)
throws Exception {
final File input = getFile("bla.pack");
- final InputStream is = useFile
- ? new Pack200CompressorInputStream(input, mode)
- : new Pack200CompressorInputStream(new FileInputStream(input),
- mode);
- try {
+ try (InputStream is = useFile
+ ? new Pack200CompressorInputStream(input, mode)
+ : new Pack200CompressorInputStream(new FileInputStream(input),
+ mode)) {
final ArchiveInputStream in = new ArchiveStreamFactory()
- .createArchiveInputStream("jar", is);
+ .createArchiveInputStream("jar", is);
ArchiveEntry entry = in.getNextEntry();
while (entry != null) {
@@ -91,8 +90,6 @@
}
in.close();
- } finally {
- is.close();
}
}
@@ -112,12 +109,10 @@
final File file1 = getFile("test1.xml");
final File file2 = getFile("test2.xml");
- final OutputStream out =
- new Pack200CompressorOutputStream(new FileOutputStream(output),
- mode);
- try {
+ try (OutputStream out = new Pack200CompressorOutputStream(new FileOutputStream(output),
+ mode)) {
final ArchiveOutputStream os = new ArchiveStreamFactory()
- .createArchiveOutputStream("jar", out);
+ .createArchiveOutputStream("jar", out);
os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
IOUtils.copy(new FileInputStream(file1), os);
@@ -128,57 +123,43 @@
os.closeArchiveEntry();
os.close();
- } finally {
- out.close();
}
- final InputStream is = new Pack200CompressorInputStream(output);
- try {
+ try (InputStream is = new Pack200CompressorInputStream(output)) {
final ArchiveInputStream in = new ArchiveStreamFactory()
- .createArchiveInputStream("jar", is);
+ .createArchiveInputStream("jar", is);
final List<String> files = new ArrayList<>();
files.add("testdata/test1.xml");
files.add("testdata/test2.xml");
checkArchiveContent(in, files);
in.close();
- } finally {
- is.close();
}
}
@Test
public void testGoodSignature() throws Exception {
- final InputStream is = new FileInputStream(getFile("bla.pack"));
- try {
+ try (InputStream is = new FileInputStream(getFile("bla.pack"))) {
final byte[] sig = new byte[4];
is.read(sig);
assertTrue(Pack200CompressorInputStream.matches(sig, 4));
- } finally {
- is.close();
}
}
@Test
public void testBadSignature() throws Exception {
- final InputStream is = new FileInputStream(getFile("bla.jar"));
- try {
+ try (InputStream is = new FileInputStream(getFile("bla.jar"))) {
final byte[] sig = new byte[4];
is.read(sig);
assertFalse(Pack200CompressorInputStream.matches(sig, 4));
- } finally {
- is.close();
}
}
@Test
public void testShortSignature() throws Exception {
- final InputStream is = new FileInputStream(getFile("bla.pack"));
- try {
+ try (InputStream is = new FileInputStream(getFile("bla.pack"))) {
final byte[] sig = new byte[2];
is.read(sig);
assertFalse(Pack200CompressorInputStream.matches(sig, 2));
- } finally {
- is.close();
}
}
@@ -186,10 +167,8 @@
public void testInputStreamMethods() throws Exception {
final Map<String, String> m = new HashMap<>();
m.put("foo", "bar");
- final InputStream is =
- new Pack200CompressorInputStream(new FileInputStream(getFile("bla.jar")),
- m);
- try {
+ try (InputStream is = new Pack200CompressorInputStream(new FileInputStream(getFile("bla.jar")),
+ m)) {
// packed file is a jar, which is a zip so it starts with
// a local file header
assertTrue(is.markSupported());
@@ -204,8 +183,6 @@
is.reset();
assertEquals(0x50, is.read());
assertTrue(is.available() > 0);
- } finally {
- is.close();
}
}
@@ -214,14 +191,11 @@
final File output = new File(dir, "bla.pack");
final Map<String, String> m = new HashMap<>();
m.put("foo", "bar");
- final OutputStream out = new FileOutputStream(output);
- try {
+ try (OutputStream out = new FileOutputStream(output)) {
final OutputStream os = new Pack200CompressorOutputStream(out, m);
os.write(1);
os.write(new byte[] { 2, 3 });
os.close();
- } finally {
- out.close();
}
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/XZTestCase.java b/src/test/java/org/apache/commons/compress/compressors/XZTestCase.java
index 6718340..cd605a9 100644
--- a/src/test/java/org/apache/commons/compress/compressors/XZTestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/XZTestCase.java
@@ -40,17 +40,11 @@
System.out.println("XZTestCase: HeapMax="+max+" bytes "+(double)max/(1024*1024)+" MB");
final File input = getFile("test1.xml");
final File output = new File(dir, "test1.xml.xz");
- final OutputStream out = new FileOutputStream(output);
- try {
- final CompressorOutputStream cos = new CompressorStreamFactory()
- .createCompressorOutputStream("xz", out);
- try {
+ try (OutputStream out = new FileOutputStream(output)) {
+ try (CompressorOutputStream cos = new CompressorStreamFactory()
+ .createCompressorOutputStream("xz", out)) {
IOUtils.copy(new FileInputStream(input), cos);
- } finally {
- cos.close();
}
- } finally {
- out.close();
}
}
@@ -58,10 +52,9 @@
public void testXZUnarchive() throws Exception {
final File input = getFile("bla.tar.xz");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream("xz", is);
+ .createCompressorInputStream("xz", is);
FileOutputStream out = null;
try {
out = new FileOutputStream(output);
@@ -72,46 +65,31 @@
}
in.close();
}
- } finally {
- is.close();
}
}
@Test
public void testConcatenatedStreamsReadFirstOnly() throws Exception {
final File input = getFile("multiple.xz");
- final InputStream is = new FileInputStream(input);
- try {
- final CompressorInputStream in = new CompressorStreamFactory()
- .createCompressorInputStream("xz", is);
- try {
+ try (InputStream is = new FileInputStream(input)) {
+ try (CompressorInputStream in = new CompressorStreamFactory()
+ .createCompressorInputStream("xz", 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.xz");
- final InputStream is = new FileInputStream(input);
- try {
- final CompressorInputStream in =
- new XZCompressorInputStream(is, true);
- try {
+ try (InputStream is = new FileInputStream(input)) {
+ try (CompressorInputStream in = new XZCompressorInputStream(is, true)) {
assertEquals('a', in.read());
assertEquals('b', in.read());
assertEquals(0, in.available());
assertEquals(-1, in.read());
- } finally {
- in.close();
}
- } finally {
- is.close();
}
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java b/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
index f2804a9..b80a4a0 100644
--- a/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
+++ b/src/test/java/org/apache/commons/compress/compressors/ZTestCase.java
@@ -81,8 +81,7 @@
private void testUnarchive(final StreamWrapper<CompressorInputStream> wrapper) throws Exception {
final File input = getFile("bla.tar.Z");
final File output = new File(dir, "bla.tar");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final InputStream in = wrapper.wrap(is);
FileOutputStream out = null;
try {
@@ -94,8 +93,6 @@
}
in.close();
}
- } finally {
- is.close();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
index 0a2a3b5..c045a7d 100644
--- a/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStreamTest.java
@@ -31,11 +31,8 @@
@Test(expected = IOException.class)
public void shouldThrowAnIOExceptionWhenAppliedToAZipFile() throws Exception {
- final FileInputStream in = new FileInputStream(getFile("bla.zip"));
- try {
+ try (FileInputStream in = new FileInputStream(getFile("bla.zip"))) {
new BZip2CompressorInputStream(in);
- } finally {
- in.close();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java
index e17b6a0..1d8261e 100644
--- a/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStreamTest.java
@@ -33,58 +33,46 @@
@Test
public void availableShouldReturnNonZero() throws IOException {
final File input = AbstractTestCase.getFile("bla.tar.deflatez");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final DeflateCompressorInputStream in =
- new DeflateCompressorInputStream(is);
+ new DeflateCompressorInputStream(is);
Assert.assertTrue(in.available() > 0);
in.close();
- } finally {
- is.close();
}
}
@Test
public void shouldBeAbleToSkipAByte() throws IOException {
final File input = AbstractTestCase.getFile("bla.tar.deflatez");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final DeflateCompressorInputStream in =
- new DeflateCompressorInputStream(is);
+ new DeflateCompressorInputStream(is);
Assert.assertEquals(1, in.skip(1));
in.close();
- } finally {
- is.close();
}
}
@Test
public void singleByteReadWorksAsExpected() throws IOException {
final File input = AbstractTestCase.getFile("bla.tar.deflatez");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final DeflateCompressorInputStream in =
- new DeflateCompressorInputStream(is);
+ new DeflateCompressorInputStream(is);
// tar header starts with filename "test1.xml"
Assert.assertEquals('t', in.read());
in.close();
- } finally {
- is.close();
}
}
@Test
public void singleByteReadReturnsMinusOneAtEof() throws IOException {
final File input = AbstractTestCase.getFile("bla.tar.deflatez");
- final InputStream is = new FileInputStream(input);
- try {
+ try (InputStream is = new FileInputStream(input)) {
final DeflateCompressorInputStream in =
- new DeflateCompressorInputStream(is);
+ new DeflateCompressorInputStream(is);
IOUtils.toByteArray(in);
Assert.assertEquals(-1, in.read());
in.close();
- } finally {
- is.close();
}
}
diff --git a/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
index 83f8d32..3874f75 100644
--- a/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java
@@ -40,10 +40,9 @@
try {
Pack200Utils.normalize(input, output[1],
new HashMap<String, String>());
- final FileInputStream is = new FileInputStream(output[1]);
- try {
+ try (FileInputStream is = new FileInputStream(output[1])) {
final ArchiveInputStream in = new ArchiveStreamFactory()
- .createArchiveInputStream("jar", is);
+ .createArchiveInputStream("jar", is);
ArchiveEntry entry = in.getNextEntry();
while (entry != null) {
@@ -61,8 +60,6 @@
}
in.close();
- } finally {
- is.close();
}
} finally {
output[1].delete();
diff --git a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
index a1992a0..cca9495 100644
--- a/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
+++ b/src/test/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStreamTest.java
@@ -41,11 +41,8 @@
assertFalse(FramedSnappyCompressorInputStream.matches(new byte[10], 10));
final byte[] b = new byte[12];
final File input = getFile("bla.tar.sz");
- final FileInputStream in = new FileInputStream(input);
- try {
+ try (FileInputStream in = new FileInputStream(input)) {
IOUtils.readFully(in, b);
- } finally {
- in.close();
}
assertFalse(FramedSnappyCompressorInputStream.matches(b, 9));
assertTrue(FramedSnappyCompressorInputStream.matches(b, 10));
@@ -57,10 +54,9 @@
*/
@Test
public void testLoremIpsum() throws Exception {
- final FileInputStream isSz = new FileInputStream(getFile("lorem-ipsum.txt.sz"));
final File outputSz = new File(dir, "lorem-ipsum.1");
final File outputGz = new File(dir, "lorem-ipsum.2");
- try {
+ try (FileInputStream isSz = new FileInputStream(getFile("lorem-ipsum.txt.sz"))) {
InputStream in = new FramedSnappyCompressorInputStream(isSz);
FileOutputStream out = null;
try {
@@ -72,8 +68,7 @@
}
in.close();
}
- final FileInputStream isGz = new FileInputStream(getFile("lorem-ipsum.txt.gz"));
- try {
+ try (FileInputStream isGz = new FileInputStream(getFile("lorem-ipsum.txt.gz"))) {
in = new GzipCompressorInputStream(isGz);
try {
out = new FileOutputStream(outputGz);
@@ -84,37 +79,24 @@
}
in.close();
}
- } finally {
- isGz.close();
}
- } finally {
- isSz.close();
}
- final FileInputStream sz = new FileInputStream(outputSz);
- try {
- final FileInputStream gz = new FileInputStream(outputGz);
- try {
+ try (FileInputStream sz = new FileInputStream(outputSz)) {
+ try (FileInputStream gz = new FileInputStream(outputGz)) {
assertArrayEquals(IOUtils.toByteArray(sz),
- IOUtils.toByteArray(gz));
- } finally {
- gz.close();
+ IOUtils.toByteArray(gz));
}
- } finally {
- sz.close();
}
}
@Test
public void testRemainingChunkTypes() throws Exception {
- final FileInputStream isSz = new FileInputStream(getFile("mixed.txt.sz"));
final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try {
+ try (FileInputStream isSz = new FileInputStream(getFile("mixed.txt.sz"))) {
final FramedSnappyCompressorInputStream in = new FramedSnappyCompressorInputStream(isSz);
IOUtils.copy(in, out);
out.close();
- } finally {
- isSz.close();
}
assertArrayEquals(new byte[] { '1', '2', '3', '4',
@@ -130,8 +112,7 @@
@Test
public void testAvailable() throws Exception {
- final FileInputStream isSz = new FileInputStream(getFile("mixed.txt.sz"));
- try {
+ try (FileInputStream isSz = new FileInputStream(getFile("mixed.txt.sz"))) {
final FramedSnappyCompressorInputStream in = new FramedSnappyCompressorInputStream(isSz);
assertEquals(0, in.available()); // no chunk read so far
assertEquals('1', in.read());
@@ -143,8 +124,6 @@
assertEquals('5', in.read());
assertEquals(19, in.available()); // remainder of copy
in.close();
- } finally {
- isSz.close();
}
}
@@ -173,12 +152,10 @@
@Test
public void readIWAFile() throws Exception {
- final ZipFile zip = new ZipFile(getFile("testNumbersNew.numbers"));
- try {
- final InputStream is = zip.getInputStream(zip.getEntry("Index/Document.iwa"));
- try {
+ try (ZipFile zip = new ZipFile(getFile("testNumbersNew.numbers"))) {
+ try (InputStream is = zip.getInputStream(zip.getEntry("Index/Document.iwa"))) {
final FramedSnappyCompressorInputStream in =
- new FramedSnappyCompressorInputStream(is, FramedSnappyDialect.IWORK_ARCHIVE);
+ new FramedSnappyCompressorInputStream(is, FramedSnappyDialect.IWORK_ARCHIVE);
FileOutputStream out = null;
try {
out = new FileOutputStream(new File(dir, "snappyIWATest.raw"));
@@ -189,11 +166,7 @@
}
in.close();
}
- } finally {
- is.close();
}
- } finally {
- zip.close();
}
}