ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [2/3] ant git commit: plug resource leaks detected by Sonar
Date Tue, 20 Dec 2016 19:44:53 GMT
plug resource leaks detected by Sonar


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/032e888a
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/032e888a
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/032e888a

Branch: refs/heads/1.9.x
Commit: 032e888aace67eb95884b2ae40230f2656c4e0d0
Parents: ac1b765
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Tue Dec 20 20:41:19 2016 +0100
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Tue Dec 20 20:41:19 2016 +0100

----------------------------------------------------------------------
 .../apache/tools/ant/taskdefs/AntStructure.java |  6 +++++-
 .../apache/tools/ant/taskdefs/Available.java    | 22 +++++++++++++-------
 .../apache/tools/ant/taskdefs/optional/Rpm.java | 12 +++++++----
 .../optional/depend/DirectoryIterator.java      |  4 ++++
 .../optional/extension/ExtensionUtil.java       | 20 ++++++++++++++++--
 .../ant/taskdefs/optional/jlink/jlink.java      |  3 +++
 .../taskdefs/optional/sound/AntSoundPlayer.java |  3 +++
 .../tools/ant/types/resources/ResourceList.java |  7 +++++--
 .../tools/ant/types/resources/Tokens.java       |  3 ++-
 9 files changed, 62 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
index 20f811a..707f4b9 100644
--- a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
+++ b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java
@@ -36,6 +36,7 @@ import org.apache.tools.ant.Task;
 import org.apache.tools.ant.TaskContainer;
 import org.apache.tools.ant.types.EnumeratedAttribute;
 import org.apache.tools.ant.types.Reference;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Creates a partial DTD for Ant from the currently known tasks.
@@ -84,9 +85,12 @@ public class AntStructure extends Task {
 
         PrintWriter out = null;
         try {
+            FileOutputStream fos = null;
             try {
-                out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output),
"UTF8"));
+                fos = new FileOutputStream(output);
+                out = new PrintWriter(new OutputStreamWriter(fos, "UTF8"));
             } catch (final UnsupportedEncodingException ue) {
+                FileUtils.close(fos);
                 /*
                  * Plain impossible with UTF8, see
                  * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/Available.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/Available.java b/src/main/org/apache/tools/ant/taskdefs/Available.java
index 816568e..f4919a1 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Available.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Available.java
@@ -19,6 +19,7 @@
 package org.apache.tools.ant.taskdefs;
 
 import java.io.File;
+import java.io.InputStream;
 
 import org.apache.tools.ant.AntClassLoader;
 import org.apache.tools.ant.BuildException;
@@ -421,16 +422,21 @@ public class Available extends Task implements Condition {
      * Check if a given resource can be loaded.
      */
     private boolean checkResource(String resource) {
-        if (loader != null) {
-            return (loader.getResourceAsStream(resource) != null);
-        } else {
-            ClassLoader cL = this.getClass().getClassLoader();
-            if (cL != null) {
-                return (cL.getResourceAsStream(resource) != null);
+        InputStream is = null;
+        try {
+            if (loader != null) {
+                is = loader.getResourceAsStream(resource);
             } else {
-                return
-                    (ClassLoader.getSystemResourceAsStream(resource) != null);
+                ClassLoader cL = this.getClass().getClassLoader();
+                if (cL != null) {
+                    is = cL.getResourceAsStream(resource);
+                } else {
+                    is = ClassLoader.getSystemResourceAsStream(resource);
+                }
             }
+            return is != null;
+        } finally {
+            FileUtils.close(is);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java b/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
index b395a16..a48ed96 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
@@ -149,11 +149,13 @@ public class Rpm extends Task {
             }
         } else {
             if (output != null) {
+                FileOutputStream fos = null;
                 try {
-                    BufferedOutputStream bos
-                        = new BufferedOutputStream(new FileOutputStream(output));
+                    fos = new FileOutputStream(output);
+                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                     outputstream = new PrintStream(bos);
                 } catch (IOException e) {
+                    FileUtils.close(fos);
                     throw new BuildException(e, getLocation());
                 }
             } else if (!quiet) {
@@ -162,11 +164,13 @@ public class Rpm extends Task {
                 outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
             }
             if (error != null) {
+                FileOutputStream fos = null;
                 try {
-                    BufferedOutputStream bos
-                        = new BufferedOutputStream(new FileOutputStream(error));
+                    fos = new FileOutputStream(error);
+                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                     errorstream = new PrintStream(bos);
                 }  catch (IOException e) {
+                    FileUtils.close(fos);
                     throw new BuildException(e, getLocation());
                 }
             } else if (!quiet) {

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
b/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
index ebf244a..e12f684 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
@@ -133,6 +133,7 @@ public class DirectoryIterator implements ClassFileIterator {
                         FileInputStream inFileStream
                             = new FileInputStream(element);
 
+                        try {
                         if (element.getName().endsWith(".class")) {
 
                             // create a data input stream from the jar
@@ -143,6 +144,9 @@ public class DirectoryIterator implements ClassFileIterator {
 
                             nextElement = javaClass;
                         }
+                        } finally {
+                            inFileStream.close();
+                        }
                     }
                 } else {
                     // this iterator is exhausted. Can we pop one off the stack

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
b/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
index 089c789..ce66050 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
@@ -130,8 +130,9 @@ public final class ExtensionUtil {
                                         final boolean includeImpl,
                                         final boolean includeURL)
         throws BuildException {
+        JarFile jarFile = null;
         try {
-            final JarFile jarFile = new JarFile(file);
+            jarFile = new JarFile(file);
             final Extension[] extensions =
                 Extension.getAvailable(jarFile.getManifest());
             for (int i = 0; i < extensions.length; i++) {
@@ -140,6 +141,8 @@ public final class ExtensionUtil {
             }
         } catch (final Exception e) {
             throw new BuildException(e.getMessage(), e);
+        } finally {
+            close(jarFile);
         }
     }
 
@@ -201,8 +204,9 @@ public final class ExtensionUtil {
      */
     static Manifest getManifest(final File file)
         throws BuildException {
+        JarFile jarFile = null;
         try {
-            final JarFile jarFile = new JarFile(file);
+            jarFile = new JarFile(file);
             Manifest m = jarFile.getManifest();
             if (m == null) {
                 throw new BuildException(file + " doesn't have a MANIFEST");
@@ -210,6 +214,18 @@ public final class ExtensionUtil {
             return m;
         } catch (final IOException ioe) {
             throw new BuildException(ioe.getMessage(), ioe);
+        } finally {
+            close(jarFile);
+        }
+    }
+
+    private static void close(JarFile device) {
+        if (null != device) {
+            try {
+                device.close();
+            } catch (IOException e) {
+                //ignore
+            }
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java b/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java
index a788727..3916477 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java
@@ -223,6 +223,7 @@ public class jlink {
             return;
         }
         ZipFile zipf = new ZipFile(f);
+        try {
         Enumeration entries = zipf.entries();
 
         while (entries.hasMoreElements()) {
@@ -267,7 +268,9 @@ public class jlink {
                 output.closeEntry();
             }
         }
+        } finally {
         zipf.close();
+        }
     }
 
 

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java b/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
index 7988bc6..2a99124 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
@@ -117,6 +117,7 @@ public class AntSoundPlayer implements LineListener, BuildListener {
             DataLine.Info   info = new DataLine.Info(Clip.class, format,
                                              AudioSystem.NOT_SPECIFIED);
             try {
+            try {
                 audioClip = (Clip) AudioSystem.getLine(info);
                 audioClip.addLineListener(this);
                 audioClip.open(audioInputStream);
@@ -133,7 +134,9 @@ public class AntSoundPlayer implements LineListener, BuildListener {
                 playClip(audioClip, loops);
             }
             audioClip.drain();
+            } finally {
             audioClip.close();
+            }
         } else {
             project.log("Can't get data from file " + file.getName());
         }

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/types/resources/ResourceList.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/types/resources/ResourceList.java b/src/main/org/apache/tools/ant/types/resources/ResourceList.java
index 8b77e1b..6c16b5e 100644
--- a/src/main/org/apache/tools/ant/types/resources/ResourceList.java
+++ b/src/main/org/apache/tools/ant/types/resources/ResourceList.java
@@ -195,15 +195,18 @@ public class ResourceList extends DataType implements ResourceCollection
{
             crh.setPrimaryReader(input);
             crh.setFilterChains(filterChains);
             crh.setProject(getProject());
-            BufferedReader reader = new BufferedReader(crh.getAssembledReader());
-
             Union streamResources = new Union();
+            BufferedReader reader = new BufferedReader(crh.getAssembledReader());
+            try {
             streamResources.setCache(true);
 
             String line = null;
             while ((line = reader.readLine()) != null) {
                 streamResources.add(parse(line));
             }
+            } finally {
+                reader.close();
+            }
 
             return streamResources;
         } catch (final IOException ioe) {

http://git-wip-us.apache.org/repos/asf/ant/blob/032e888a/src/main/org/apache/tools/ant/types/resources/Tokens.java
----------------------------------------------------------------------
diff --git a/src/main/org/apache/tools/ant/types/resources/Tokens.java b/src/main/org/apache/tools/ant/types/resources/Tokens.java
index 0a518c3..458f8c1 100644
--- a/src/main/org/apache/tools/ant/types/resources/Tokens.java
+++ b/src/main/org/apache/tools/ant/types/resources/Tokens.java
@@ -60,8 +60,8 @@ public class Tokens extends BaseResourceCollectionWrapper {
         ConcatResourceInputStream cat = new ConcatResourceInputStream(rc);
         cat.setManagingComponent(this);
 
+        InputStreamReader rdr = null;
         try {
-            InputStreamReader rdr = null;
             if (encoding == null) {
                 rdr = new InputStreamReader(cat);
             } else {
@@ -81,6 +81,7 @@ public class Tokens extends BaseResourceCollectionWrapper {
         } catch (IOException e) {
             throw new BuildException("Error reading tokens", e);
         } finally {
+            FileUtils.close(rdr);
             FileUtils.close(cat);
         }
     }


Mime
View raw message