diff --git a/src/main/org/apache/tools/ant/Main.java b/src/main/org/apache/tools/ant/Main.java index edce9c1af..81e4ef147 100644 --- a/src/main/org/apache/tools/ant/Main.java +++ b/src/main/org/apache/tools/ant/Main.java @@ -355,7 +355,10 @@ public class Main implements AntMain { try { final File logFile = new File(args[i + 1]); i++; - logTo = new PrintStream(new FileOutputStream(logFile)); + // life-cycle of FileOutputStream is controlled by + // logTo which becomes "out" and is closed in + // handleLogfile + logTo = new PrintStream(new FileOutputStream(logFile)); //NOSONAR isLogFileUsed = true; } catch (final IOException ioe) { final String msg = "Cannot write on the specified log file. " diff --git a/src/main/org/apache/tools/ant/taskdefs/AntStructure.java b/src/main/org/apache/tools/ant/taskdefs/AntStructure.java index 20f811a85..707f4b9c0 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 diff --git a/src/main/org/apache/tools/ant/taskdefs/Available.java b/src/main/org/apache/tools/ant/taskdefs/Available.java index 816568e00..f4919a16c 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); } } diff --git a/src/main/org/apache/tools/ant/taskdefs/Truncate.java b/src/main/org/apache/tools/ant/taskdefs/Truncate.java index 6f43c0503..cb8fd4a1c 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Truncate.java +++ b/src/main/org/apache/tools/ant/taskdefs/Truncate.java @@ -167,7 +167,7 @@ public class Truncate extends Task { } RandomAccessFile raf = null; try { - raf = new RandomAccessFile(f, READ_WRITE); + raf = new RandomAccessFile(f, READ_WRITE); //NOSONAR } catch (Exception e) { throw new BuildException("Could not open " + f + " for writing", e); } @@ -202,4 +202,4 @@ public class Truncate extends Task { return path; } -} \ No newline at end of file +} diff --git a/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java b/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java index a90bcc01a..6f17402d8 100644 --- a/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java +++ b/src/main/org/apache/tools/ant/taskdefs/cvslib/CvsTagDiff.java @@ -299,7 +299,7 @@ public class CvsTagDiff extends AbstractCvsTask { BufferedReader reader = null; try { - reader = new BufferedReader(new FileReader(tmpFile)); + reader = new BufferedReader(new FileReader(tmpFile)); //NOSONAR // entries are of the form: //CVS 1.11 diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java index 1c9f6653d..92f947a86 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java @@ -249,7 +249,7 @@ public class PropertyFile extends Task { throw new BuildException(x, getLocation()); } try { - OutputStream os = new FileOutputStream(propertyfile); + OutputStream os = new FileOutputStream(propertyfile); //NOSONAR try { try { os.write(baos.toByteArray()); 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 b395a16cb..a48ed962b 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) { 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 ebf244a55..af28c9642 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,15 +133,19 @@ public class DirectoryIterator implements ClassFileIterator { FileInputStream inFileStream = new FileInputStream(element); - if (element.getName().endsWith(".class")) { + try { + if (element.getName().endsWith(".class")) { - // create a data input stream from the jar - // input stream - ClassFile javaClass = new ClassFile(); + // create a data input stream from the jar + // input stream + ClassFile javaClass = new ClassFile(); - javaClass.read(inFileStream); + javaClass.read(inFileStream); - nextElement = javaClass; + nextElement = javaClass; + } + } finally { + inFileStream.close(); } } } else { diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/IPlanetEjbc.java b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/IPlanetEjbc.java index 41e90d0f0..984fc3785 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/ejb/IPlanetEjbc.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/ejb/IPlanetEjbc.java @@ -727,7 +727,8 @@ public class IPlanetEjbc { } else { location = (String) fileDtds.get(publicId); if (location != null) { - inputStream = new FileInputStream(location); + // closed when the InputSource is closed + inputStream = new FileInputStream(location); //NOSONAR } } } catch (IOException e) { 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 089c78947..ce660501f 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 + } } } } diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java b/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java index 21f03fe7d..9d2e9ae68 100644 --- a/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/optional/jdepend/JDependTask.java @@ -459,7 +459,7 @@ public class JDependTask extends Task { PrintWriter pw = null; if (getOutputFile() != null) { try { - fw = new FileWriter(getOutputFile().getPath()); + fw = new FileWriter(getOutputFile().getPath()); //NOSONAR } catch (IOException e) { String msg = "JDepend Failed when creating the output file: " + e.getMessage(); 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 a78872712..b2c5d0f52 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,51 +223,54 @@ public class jlink { return; } ZipFile zipf = new ZipFile(f); - Enumeration entries = zipf.entries(); - - while (entries.hasMoreElements()) { - ZipEntry inputEntry = (ZipEntry) entries.nextElement(); - //Ignore manifest entries. They're bound to cause conflicts between - //files that are being merged. User should supply their own - //manifest file when doing the merge. - String inputEntryName = inputEntry.getName(); - int index = inputEntryName.indexOf("META-INF"); - - if (index < 0) { - //META-INF not found in the name of the entry. Go ahead and process it. - try { - output.putNextEntry(processEntry(zipf, inputEntry)); - } catch (ZipException ex) { - //If we get here, it could be because we are trying to put a - //directory entry that already exists. - //For example, we're trying to write "com", but a previous - //entry from another mergefile was called "com". - //In that case, just ignore the error and go on to the - //next entry. - String mess = ex.getMessage(); - - if (mess.indexOf("duplicate") >= 0) { - //It was the duplicate entry. - continue; - } else { - // I hate to admit it, but we don't know what happened - // here. Throw the Exception. - throw ex; + try { + Enumeration entries = zipf.entries(); + + while (entries.hasMoreElements()) { + ZipEntry inputEntry = (ZipEntry) entries.nextElement(); + //Ignore manifest entries. They're bound to cause conflicts between + //files that are being merged. User should supply their own + //manifest file when doing the merge. + String inputEntryName = inputEntry.getName(); + int index = inputEntryName.indexOf("META-INF"); + + if (index < 0) { + //META-INF not found in the name of the entry. Go ahead and process it. + try { + output.putNextEntry(processEntry(zipf, inputEntry)); + } catch (ZipException ex) { + //If we get here, it could be because we are trying to put a + //directory entry that already exists. + //For example, we're trying to write "com", but a previous + //entry from another mergefile was called "com". + //In that case, just ignore the error and go on to the + //next entry. + String mess = ex.getMessage(); + + if (mess.indexOf("duplicate") >= 0) { + //It was the duplicate entry. + continue; + } else { + // I hate to admit it, but we don't know what happened + // here. Throw the Exception. + throw ex; + } } - } - InputStream in = zipf.getInputStream(inputEntry); - int len = buffer.length; - int count = -1; + InputStream in = zipf.getInputStream(inputEntry); + int len = buffer.length; + int count = -1; - while ((count = in.read(buffer, 0, len)) > 0) { - output.write(buffer, 0, count); + while ((count = in.read(buffer, 0, len)) > 0) { + output.write(buffer, 0, count); + } + in.close(); + output.closeEntry(); } - in.close(); - output.closeEntry(); } + } finally { + zipf.close(); } - zipf.close(); } 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 7988bc60b..5007c4594 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,23 +117,26 @@ public class AntSoundPlayer implements LineListener, BuildListener { DataLine.Info info = new DataLine.Info(Clip.class, format, AudioSystem.NOT_SPECIFIED); try { - audioClip = (Clip) AudioSystem.getLine(info); - audioClip.addLineListener(this); - audioClip.open(audioInputStream); - } catch (LineUnavailableException e) { - project.log("The sound device is currently unavailable"); - return; - } catch (IOException e) { - e.printStackTrace(); - } + try { + audioClip = (Clip) AudioSystem.getLine(info); + audioClip.addLineListener(this); + audioClip.open(audioInputStream); + } catch (LineUnavailableException e) { + project.log("The sound device is currently unavailable"); + return; + } catch (IOException e) { + e.printStackTrace(); + } - if (duration != null) { - playClip(audioClip, duration.longValue()); - } else { - playClip(audioClip, loops); + if (duration != null) { + playClip(audioClip, duration.longValue()); + } else { + playClip(audioClip, loops); + } + audioClip.drain(); + } finally { + audioClip.close(); } - audioClip.drain(); - audioClip.close(); } else { project.log("Can't get data from file " + file.getName()); } 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 8b77e1bbf..da83ea9dd 100644 --- a/src/main/org/apache/tools/ant/types/resources/ResourceList.java +++ b/src/main/org/apache/tools/ant/types/resources/ResourceList.java @@ -195,14 +195,17 @@ 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(); - streamResources.setCache(true); + BufferedReader reader = new BufferedReader(crh.getAssembledReader()); + try { + streamResources.setCache(true); - String line = null; - while ((line = reader.readLine()) != null) { - streamResources.add(parse(line)); + String line = null; + while ((line = reader.readLine()) != null) { + streamResources.add(parse(line)); + } + } finally { + reader.close(); } return streamResources; 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 0a518c3dc..458f8c1f0 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); } } diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java index 4da3b6ffe..334f37ec9 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ContainsRegexpSelector.java @@ -184,7 +184,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector } try { - in = new BufferedReader(new InputStreamReader(r.getInputStream())); + in = new BufferedReader(new InputStreamReader(r.getInputStream())); //NOSONAR } catch (Exception e) { throw new BuildException("Could not get InputStream from " + r.toLongString(), e); diff --git a/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java b/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java index 6dabaf4cb..a1004a8f2 100644 --- a/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/ContainsSelector.java @@ -187,9 +187,9 @@ public class ContainsSelector extends BaseExtendSelector implements ResourceSele BufferedReader in = null; try { if (encoding != null) { - in = new BufferedReader(new InputStreamReader(r.getInputStream(), encoding)); + in = new BufferedReader(new InputStreamReader(r.getInputStream(), encoding)); //NOSONAR } else { - in = new BufferedReader(new InputStreamReader(r.getInputStream())); + in = new BufferedReader(new InputStreamReader(r.getInputStream())); //NOSONAR } } catch (Exception e) { throw new BuildException("Could not get InputStream from " diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 891dafe6d..48f5ca65b 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -1696,7 +1696,7 @@ public class FileUtils { */ public String getDefaultEncoding() { InputStreamReader is = new InputStreamReader( - new InputStream() { + new InputStream() { //NOSONAR public int read() { return -1; } diff --git a/src/main/org/apache/tools/zip/ZipFile.java b/src/main/org/apache/tools/zip/ZipFile.java index f34231040..1be53cdcc 100644 --- a/src/main/org/apache/tools/zip/ZipFile.java +++ b/src/main/org/apache/tools/zip/ZipFile.java @@ -370,8 +370,10 @@ public class ZipFile implements Closeable { final OffsetEntry offsetEntry = ((Entry) ze).getOffsetEntry(); ZipUtil.checkRequestedFeatures(ze); final long start = offsetEntry.dataOffset; + // doesn't get closed if the method is not supported, but + // doesn't hold any resources either final BoundedInputStream bis = - new BoundedInputStream(start, ze.getCompressedSize()); + new BoundedInputStream(start, ze.getCompressedSize()); //NOSONAR switch (ze.getMethod()) { case ZipEntry.STORED: return bis;