From 93f11da846cb97cd940de5826925fe15b1200e94 Mon Sep 17 00:00:00 2001 From: Gintas Grigelionis Date: Fri, 3 Aug 2018 13:54:24 +0200 Subject: [PATCH] Use try with resources --- src/java/org/apache/ivy/ant/IvyReport.java | 22 ++-------------------- test/java/org/apache/ivy/TestHelper.java | 12 +----------- 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/src/java/org/apache/ivy/ant/IvyReport.java b/src/java/org/apache/ivy/ant/IvyReport.java index 62217d56..cfac8dff 100644 --- a/src/java/org/apache/ivy/ant/IvyReport.java +++ b/src/java/org/apache/ivy/ant/IvyReport.java @@ -341,31 +341,13 @@ public class IvyReport extends IvyTask { } } - InputStream inStream = null; - OutputStream outStream = null; - try { - inStream = new BufferedInputStream(new FileInputStream(reportFile)); - outStream = new BufferedOutputStream(new FileOutputStream(outFile)); + try (InputStream inStream = new BufferedInputStream(new FileInputStream(reportFile)); + OutputStream outStream = new BufferedOutputStream(new FileOutputStream(outFile))) { StreamResult res = new StreamResult(outStream); Source src = new StreamSource(inStream, JAXPUtils.getSystemId(style)); transformer.transform(src, res); } catch (TransformerException e) { throw new BuildException(e); - } finally { - if (inStream != null) { - try { - inStream.close(); - } catch (IOException e) { - // ignore - } - } - if (outStream != null) { - try { - outStream.close(); - } catch (IOException e) { - // ignore - } - } } } } catch (TransformerConfigurationException e) { diff --git a/test/java/org/apache/ivy/TestHelper.java b/test/java/org/apache/ivy/TestHelper.java index 5ffe8ac1..066e4196 100644 --- a/test/java/org/apache/ivy/TestHelper.java +++ b/test/java/org/apache/ivy/TestHelper.java @@ -497,9 +497,7 @@ public class TestHelper { * after this method since the associated socket is released and some other process can now use it. */ public static int getMaybeAvailablePort() { - ServerSocket s = null; - try { - s = new ServerSocket(0); + try (ServerSocket s = new ServerSocket(0)) { s.setReuseAddress(true); int port = s.getLocalPort(); try { @@ -510,14 +508,6 @@ public class TestHelper { return port; } catch (IOException e) { // ignore - } finally { - if (s != null) { - try { - s.close(); - } catch (IOException e) { - // ignore - } - } } throw new IllegalStateException("Not TCP/IP port available"); }