diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java index 7c176ecc4..81ed07f4a 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ssh/ScpToMessage.java @@ -347,7 +347,6 @@ public class ScpToMessage extends AbstractSshMessage { waitForAck(in); // send a content of lfile - final InputStream fis = Files.newInputStream(localFile.toPath()); final byte[] buf = new byte[BUFFER_SIZE]; final long startTime = System.currentTimeMillis(); long totalLength = 0; @@ -359,7 +358,7 @@ public class ScpToMessage extends AbstractSshMessage { final long initFilesize = filesize; int percentTransmitted = 0; - try { + try (InputStream fis = Files.newInputStream(localFile.toPath())) { if (this.getVerbose()) { log("Sending: " + localFile.getName() + " : " + localFile.length()); } @@ -385,7 +384,6 @@ public class ScpToMessage extends AbstractSshMessage { final long endTime = System.currentTimeMillis(); logStats(startTime, endTime, totalLength); } - fis.close(); } } diff --git a/src/tests/junit/org/apache/tools/ant/FileUtilities.java b/src/tests/junit/org/apache/tools/ant/FileUtilities.java index 321d8d445..0b7e0ce77 100644 --- a/src/tests/junit/org/apache/tools/ant/FileUtilities.java +++ b/src/tests/junit/org/apache/tools/ant/FileUtilities.java @@ -46,15 +46,8 @@ public class FileUtilities { * @throws IOException on error reading the file (not existing, not readable etc) */ public static String getFileContents(File file) throws IOException { - FileReader rdr = null; - try { - rdr = new FileReader(file); - return FileUtils.readFully(rdr); - } - finally { - if (rdr != null) { - rdr.close(); - } + try (FileReader rdr = new FileReader(file)) { + return FileUtils.readFully(rdr); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java index 961a473db..5b36d0553 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/CopyTest.java @@ -298,11 +298,8 @@ public class CopyTest { assertTrue("Source file " + srcFile + " was expected to be a file", srcFile.isFile()); final long originalFileSize = srcFile.length(); final String originalContent; - final BufferedReader reader = new BufferedReader(new FileReader(srcFile)); - try { + try (BufferedReader reader = new BufferedReader(new FileReader(srcFile))) { originalContent = FileUtils.readFully(reader); - } finally { - reader.close(); } assertTrue("Content missing in file " + srcFile, originalContent != null && originalContent.length() > 0); @@ -324,12 +321,9 @@ public class CopyTest { private void assertSizeAndContent(final File file, final long expectedSize, final String expectedContent) throws IOException { assertTrue(file + " was expected to be a file", file.isFile()); assertEquals("Unexpected size of file " + file, expectedSize, file.length()); - final BufferedReader reader = new BufferedReader(new FileReader(file)); final String content; - try { + try (BufferedReader reader = new BufferedReader(new FileReader(file))) { content = FileUtils.readFully(reader); - } finally { - reader.close(); } assertEquals("Unexpected content in file " + file, expectedContent, content); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java index 7a519469d..99cb01837 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ExecStreamRedirectorTest.java @@ -75,16 +75,13 @@ public class ExecStreamRedirectorTest { } private static byte[] readAllBytes(final File file) throws IOException { - final FileInputStream fis = new FileInputStream(file); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try { + try (FileInputStream fis = new FileInputStream(file)) { final byte[] dataChunk = new byte[1024]; int numRead = -1; while ((numRead = fis.read(dataChunk)) > 0) { bos.write(dataChunk, 0, numRead); } - } finally { - fis.close(); } return bos.toByteArray(); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java index 451a951fc..7ba55563a 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/FixCrLfTest.java @@ -237,11 +237,8 @@ public class FixCrLfTest { fail("Expected file " + result + " doesn\'t exist"); } - InputStream inExpect = null; - InputStream inResult = null; - try { - inExpect = new BufferedInputStream(new FileInputStream(expect)); - inResult = new BufferedInputStream(new FileInputStream(result)); + try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect)); + InputStream inResult = new BufferedInputStream(new FileInputStream(result))) { int expectedByte = inExpect.read(); while (expectedByte != -1) { @@ -249,13 +246,6 @@ public class FixCrLfTest { expectedByte = inExpect.read(); } assertEquals("End of file", -1, inResult.read()); - } finally { - if (inResult != null) { - inResult.close(); - } - if (inExpect != null) { - inExpect.close(); - } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java index a44303f92..04aa15378 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/InitializeClassTest.java @@ -56,15 +56,12 @@ public class InitializeClassTest { buildRule.executeTarget("forked"); synchronized (System.out) { PrintStream ps = System.out; - PrintStream newps = new PrintStream(new FileOutputStream(f2)); - try { - System.setOut(newps); - buildRule.getProject().executeTarget("unforked"); - } finally { - System.setOut(ps); - - newps.close(); - } + try (PrintStream newps = new PrintStream(new FileOutputStream(f2))) { + System.setOut(newps); + buildRule.getProject().executeTarget("unforked"); + } finally { + System.setOut(ps); + } } assertEquals(FileUtilities.getFileContents(f1), FileUtilities.getFileContents(f2)); } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java index a9ab25495..0c4617203 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JarTest.java @@ -261,10 +261,8 @@ public class JarTest { // bugzilla report 10262 @Test public void testNoDuplicateIndex() throws IOException { - ZipFile archive = null; - try { - buildRule.executeTarget("testIndexTests"); - archive = new ZipFile(new File(getOutputDir(), tempJar)); + buildRule.executeTarget("testIndexTests"); + try (ZipFile archive = new ZipFile(new File(getOutputDir(), tempJar))) { Enumeration e = archive.entries(); int numberOfIndexLists = 0; while (e.hasMoreElements()) { @@ -274,24 +272,17 @@ public class JarTest { } } assertEquals(1, numberOfIndexLists); - } finally { - if (archive != null) { - archive.close(); - } } } // bugzilla report 16972 @Test public void testRootFilesInIndex() throws IOException { - ZipFile archive = null; - try { - buildRule.executeTarget("testIndexTests"); - archive = new ZipFile(new File(getOutputDir(), tempJar)); + buildRule.executeTarget("testIndexTests"); + try (ZipFile archive = new ZipFile(new File(getOutputDir(), tempJar))) { ZipEntry ze = archive.getEntry("META-INF/INDEX.LIST"); InputStream is = archive.getInputStream(ze); - BufferedReader r = new BufferedReader(new InputStreamReader(is, - "UTF8")); + BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF8")); boolean foundSub = false; boolean foundSubFoo = false; boolean foundFoo = false; @@ -311,10 +302,6 @@ public class JarTest { assertTrue(foundSub); assertTrue(!foundSubFoo); assertTrue(foundFoo); - } finally { - if (archive != null) { - archive.close(); - } } } @Test diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java index bb6630587..59d3cf642 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/JavaTest.java @@ -506,22 +506,13 @@ public class JavaTest { if (argv.length >= 2) { logFile = argv[1]; } - OutputStreamWriter out = null; Thread.sleep(sleepTime * 1000); - try { - File dest = new File(logFile); - FileOutputStream fos = new FileOutputStream(dest); - out = new OutputStreamWriter(fos); + File dest = new File(logFile); + try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(dest))) { out.write("bye bye\n"); } catch (Exception ex) { - } finally { - try { - out.close(); - } catch (IOException ioe) { - } } - } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java index 690d27e71..f7749bcd2 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ManifestTest.java @@ -287,8 +287,7 @@ public class ManifestTest { assertEquals("NOT_LONG_NAME_VALUE_MISMATCH", VALUE, value); Set set = new HashSet<>(); - FileReader fin = new FileReader(expandedManifest); - try { + try (FileReader fin = new FileReader(expandedManifest)) { BufferedReader in = new BufferedReader(fin); String read = in.readLine(); @@ -297,8 +296,6 @@ public class ManifestTest { read = in.readLine(); } in.close(); - } finally { - fin.close(); } assertTrue("Manifest file should have contained string ", @@ -465,11 +462,8 @@ public class ManifestTest { * Reads mftest.mf. */ private Manifest getManifest(File file) throws IOException, ManifestException { - FileReader r = new FileReader(file); - try { + try (FileReader r = new FileReader(file)) { return new Manifest(r); - } finally { - r.close(); } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java index 7dabd3dfe..35190e7eb 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ReplaceTest.java @@ -157,25 +157,14 @@ public class ReplaceTest { fail("Expected file " + result + " doesn\'t exist"); } - InputStream inExpect = null; - InputStream inResult = null; - try { - inExpect = new BufferedInputStream(new FileInputStream(expect)); - inResult = new BufferedInputStream(new FileInputStream(result)); - + try (InputStream inExpect = new BufferedInputStream(new FileInputStream(expect)); + InputStream inResult = new BufferedInputStream(new FileInputStream(result))) { int expectedByte = inExpect.read(); while (expectedByte != -1) { assertEquals(expectedByte, inResult.read()); expectedByte = inExpect.read(); } assertEquals("End of file", -1, inResult.read()); - } finally { - if (inResult != null) { - inResult.close(); - } - if (inExpect != null) { - inExpect.close(); - } } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java index 9f93706e8..75b3e1d23 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/ZipTest.java @@ -102,7 +102,6 @@ public class ZipTest { if (zfPrefixAddsDir != null) { zfPrefixAddsDir.close(); } - } catch (IOException e) { //ignored } @@ -217,15 +216,8 @@ public class ZipTest { @Test public void testDefaultExcludesAndUpdate() throws IOException { buildRule.executeTarget("testDefaultExcludesAndUpdate"); - ZipFile f = null; - try { - f = new ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip")); - assertNotNull("ziptest~ should be included", - f.getEntry("ziptest~")); - } finally { - if (f != null) { - f.close(); - } + try (ZipFile f = new ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) { + assertNotNull("ziptest~ should be included", f.getEntry("ziptest~")); } } @@ -242,60 +234,36 @@ public class ZipTest { @Test public void testTarFileSet() throws IOException { buildRule.executeTarget("testTarFileSet"); - org.apache.tools.zip.ZipFile zf = null; - try { - zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip")); + try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) { org.apache.tools.zip.ZipEntry ze = zf.getEntry("asf-logo.gif"); assertEquals(UnixStat.FILE_FLAG | 0446, ze.getUnixMode()); - } finally { - if (zf != null) { - zf.close(); - } } } @Test public void testRewriteZeroPermissions() throws IOException { buildRule.executeTarget("rewriteZeroPermissions"); - org.apache.tools.zip.ZipFile zf = null; - try { - zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip")); + try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) { org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt"); assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode()); - } finally { - if (zf != null) { - zf.close(); - } } } @Test public void testAcceptZeroPermissions() throws IOException { buildRule.executeTarget("acceptZeroPermissions"); - org.apache.tools.zip.ZipFile zf = null; - try { - zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip")); + try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) { org.apache.tools.zip.ZipEntry ze = zf.getEntry("testdir/test.txt"); assertEquals(0000, ze.getUnixMode()); - } finally { - if (zf != null) { - zf.close(); - } } } @Test public void testForBugzilla34764() throws IOException { buildRule.executeTarget("testForBugzilla34764"); - org.apache.tools.zip.ZipFile zf = null; - try { - zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip")); + try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(new File(buildRule.getProject().getProperty("output"), "test3.zip"))) { org.apache.tools.zip.ZipEntry ze = zf.getEntry("file1"); assertEquals(UnixStat.FILE_FLAG | 0644, ze.getUnixMode()); - } finally { - if (zf != null) { - zf.close(); - } } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java index 1c2d445d2..07c653769 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java @@ -221,17 +221,8 @@ public class EchoPropertiesTest { throws IOException { File f = createRelativeFile(relativeFilename); Properties props = new Properties(); - InputStream in = null; - try { - in = new BufferedInputStream(new FileInputStream(f)); + try (InputStream in = new BufferedInputStream(new FileInputStream(f))) { props.load(in); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - } - } } return props; } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java index 1221ba4a1..8d03a7287 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLFormatterWithCDATAOnSystemOut.java @@ -65,16 +65,10 @@ public class XMLFormatterWithCDATAOnSystemOut { // avoid endless loop buildRule.executeTarget("run-junit"); File f = buildRule.getProject().resolveFile(REPORT); - FileReader reader = null; - try { - reader = new FileReader(f); + try (FileReader reader = new FileReader(f)) { String content = FileUtils.readFully(reader); - assertTrue(content.indexOf("]]>" - + "") > 0); + assertTrue(content.contains("]]>")); } finally { - if (reader != null) { - reader.close(); - } f.delete(); } } diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java index b4d2728e2..2c21e1a86 100644 --- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java +++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregatorTest.java @@ -50,14 +50,11 @@ public class XMLResultAggregatorTest { } assertTrue(d.getAbsolutePath(), d.mkdir()); File xml = new File(d, "x.xml"); - PrintWriter pw = new PrintWriter(new FileOutputStream(xml)); - try { + try (PrintWriter pw = new PrintWriter(new FileOutputStream(xml))) { pw.println(""); pw.println(" "); pw.println(""); pw.flush(); - } finally { - pw.close(); } XMLResultAggregator task = new XMLResultAggregator(); task.setTodir(d); diff --git a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java index f2a875a82..39828a56a 100644 --- a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java +++ b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java @@ -126,25 +126,23 @@ public class UTF8ZipFilesTest { ZipEncoding zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); - ZipOutputStream zos = null; - try { - zos = new ZipOutputStream(file); + try (ZipOutputStream zos = new ZipOutputStream(file)) { zos.setEncoding(encoding); zos.setUseLanguageEncodingFlag(withEFS); zos.setCreateUnicodeExtraFields(withExplicitUnicodeExtra - ? ZipOutputStream.UnicodeExtraFieldPolicy.NEVER - : ZipOutputStream.UnicodeExtraFieldPolicy.ALWAYS); + ? ZipOutputStream.UnicodeExtraFieldPolicy.NEVER + : ZipOutputStream.UnicodeExtraFieldPolicy.ALWAYS); ZipEntry ze = new ZipEntry(OIL_BARREL_TXT); if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { + && !zipEncoding.canEncode(ze.getName())) { ByteBuffer en = zipEncoding.encode(ze.getName()); ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); + en.array(), + en.arrayOffset(), + en.limit())); } zos.putNextEntry(ze); @@ -153,14 +151,14 @@ public class UTF8ZipFilesTest { ze = new ZipEntry(EURO_FOR_DOLLAR_TXT); if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { + && !zipEncoding.canEncode(ze.getName())) { ByteBuffer en = zipEncoding.encode(ze.getName()); ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); + en.array(), + en.arrayOffset(), + en.limit())); } zos.putNextEntry(ze); @@ -170,27 +168,19 @@ public class UTF8ZipFilesTest { ze = new ZipEntry(ASCII_TXT); if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { + && !zipEncoding.canEncode(ze.getName())) { ByteBuffer en = zipEncoding.encode(ze.getName()); ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); + en.array(), + en.arrayOffset(), + en.limit())); } zos.putNextEntry(ze); zos.write("ascii".getBytes("US-ASCII")); zos.closeEntry(); - } finally { - if (zos != null) { - try { - zos.close(); - } catch (IOException e) { - /* swallow */ - } - } } } @@ -247,12 +237,9 @@ public class UTF8ZipFilesTest { private static void assertCanRead(ZipFile zf, String fileName) throws IOException { ZipEntry entry = zf.getEntry(fileName); assertNotNull("Entry " + fileName + " doesn't exist", entry); - InputStream is = zf.getInputStream(entry); - assertNotNull("InputStream is null", is); - try { + try (InputStream is = zf.getInputStream(entry)) { + assertNotNull("InputStream is null", is); is.read(); - } finally { - is.close(); } }