Browse Source

plug resource leaks detected by Sonar

master
Stefan Bodewig 8 years ago
parent
commit
032e888aac
9 changed files with 62 additions and 18 deletions
  1. +5
    -1
      src/main/org/apache/tools/ant/taskdefs/AntStructure.java
  2. +14
    -8
      src/main/org/apache/tools/ant/taskdefs/Available.java
  3. +8
    -4
      src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java
  4. +4
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java
  5. +18
    -2
      src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java
  6. +3
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java
  7. +3
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java
  8. +5
    -2
      src/main/org/apache/tools/ant/types/resources/ResourceList.java
  9. +2
    -1
      src/main/org/apache/tools/ant/types/resources/Tokens.java

+ 5
- 1
src/main/org/apache/tools/ant/taskdefs/AntStructure.java View File

@@ -36,6 +36,7 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.TaskContainer; import org.apache.tools.ant.TaskContainer;
import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Reference; 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. * Creates a partial DTD for Ant from the currently known tasks.
@@ -84,9 +85,12 @@ public class AntStructure extends Task {


PrintWriter out = null; PrintWriter out = null;
try { try {
FileOutputStream fos = null;
try { 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) { } catch (final UnsupportedEncodingException ue) {
FileUtils.close(fos);
/* /*
* Plain impossible with UTF8, see * Plain impossible with UTF8, see
* http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html


+ 14
- 8
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -19,6 +19,7 @@
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import java.io.File; import java.io.File;
import java.io.InputStream;


import org.apache.tools.ant.AntClassLoader; import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException; 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. * Check if a given resource can be loaded.
*/ */
private boolean checkResource(String resource) { 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 { } 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);
} }
} }




+ 8
- 4
src/main/org/apache/tools/ant/taskdefs/optional/Rpm.java View File

@@ -149,11 +149,13 @@ public class Rpm extends Task {
} }
} else { } else {
if (output != null) { if (output != null) {
FileOutputStream fos = null;
try { try {
BufferedOutputStream bos
= new BufferedOutputStream(new FileOutputStream(output));
fos = new FileOutputStream(output);
BufferedOutputStream bos = new BufferedOutputStream(fos);
outputstream = new PrintStream(bos); outputstream = new PrintStream(bos);
} catch (IOException e) { } catch (IOException e) {
FileUtils.close(fos);
throw new BuildException(e, getLocation()); throw new BuildException(e, getLocation());
} }
} else if (!quiet) { } else if (!quiet) {
@@ -162,11 +164,13 @@ public class Rpm extends Task {
outputstream = new LogOutputStream(this, Project.MSG_DEBUG); outputstream = new LogOutputStream(this, Project.MSG_DEBUG);
} }
if (error != null) { if (error != null) {
FileOutputStream fos = null;
try { try {
BufferedOutputStream bos
= new BufferedOutputStream(new FileOutputStream(error));
fos = new FileOutputStream(error);
BufferedOutputStream bos = new BufferedOutputStream(fos);
errorstream = new PrintStream(bos); errorstream = new PrintStream(bos);
} catch (IOException e) { } catch (IOException e) {
FileUtils.close(fos);
throw new BuildException(e, getLocation()); throw new BuildException(e, getLocation());
} }
} else if (!quiet) { } else if (!quiet) {


+ 4
- 0
src/main/org/apache/tools/ant/taskdefs/optional/depend/DirectoryIterator.java View File

@@ -133,6 +133,7 @@ public class DirectoryIterator implements ClassFileIterator {
FileInputStream inFileStream FileInputStream inFileStream
= new FileInputStream(element); = new FileInputStream(element);


try {
if (element.getName().endsWith(".class")) { if (element.getName().endsWith(".class")) {


// create a data input stream from the jar // create a data input stream from the jar
@@ -143,6 +144,9 @@ public class DirectoryIterator implements ClassFileIterator {


nextElement = javaClass; nextElement = javaClass;
} }
} finally {
inFileStream.close();
}
} }
} else { } else {
// this iterator is exhausted. Can we pop one off the stack // this iterator is exhausted. Can we pop one off the stack


+ 18
- 2
src/main/org/apache/tools/ant/taskdefs/optional/extension/ExtensionUtil.java View File

@@ -130,8 +130,9 @@ public final class ExtensionUtil {
final boolean includeImpl, final boolean includeImpl,
final boolean includeURL) final boolean includeURL)
throws BuildException { throws BuildException {
JarFile jarFile = null;
try { try {
final JarFile jarFile = new JarFile(file);
jarFile = new JarFile(file);
final Extension[] extensions = final Extension[] extensions =
Extension.getAvailable(jarFile.getManifest()); Extension.getAvailable(jarFile.getManifest());
for (int i = 0; i < extensions.length; i++) { for (int i = 0; i < extensions.length; i++) {
@@ -140,6 +141,8 @@ public final class ExtensionUtil {
} }
} catch (final Exception e) { } catch (final Exception e) {
throw new BuildException(e.getMessage(), e); throw new BuildException(e.getMessage(), e);
} finally {
close(jarFile);
} }
} }


@@ -201,8 +204,9 @@ public final class ExtensionUtil {
*/ */
static Manifest getManifest(final File file) static Manifest getManifest(final File file)
throws BuildException { throws BuildException {
JarFile jarFile = null;
try { try {
final JarFile jarFile = new JarFile(file);
jarFile = new JarFile(file);
Manifest m = jarFile.getManifest(); Manifest m = jarFile.getManifest();
if (m == null) { if (m == null) {
throw new BuildException(file + " doesn't have a MANIFEST"); throw new BuildException(file + " doesn't have a MANIFEST");
@@ -210,6 +214,18 @@ public final class ExtensionUtil {
return m; return m;
} catch (final IOException ioe) { } catch (final IOException ioe) {
throw new BuildException(ioe.getMessage(), 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
}
} }
} }
} }

+ 3
- 0
src/main/org/apache/tools/ant/taskdefs/optional/jlink/jlink.java View File

@@ -223,6 +223,7 @@ public class jlink {
return; return;
} }
ZipFile zipf = new ZipFile(f); ZipFile zipf = new ZipFile(f);
try {
Enumeration entries = zipf.entries(); Enumeration entries = zipf.entries();


while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
@@ -267,7 +268,9 @@ public class jlink {
output.closeEntry(); output.closeEntry();
} }
} }
} finally {
zipf.close(); zipf.close();
}
} }






+ 3
- 0
src/main/org/apache/tools/ant/taskdefs/optional/sound/AntSoundPlayer.java View File

@@ -116,6 +116,7 @@ public class AntSoundPlayer implements LineListener, BuildListener {
AudioFormat format = audioInputStream.getFormat(); AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format, DataLine.Info info = new DataLine.Info(Clip.class, format,
AudioSystem.NOT_SPECIFIED); AudioSystem.NOT_SPECIFIED);
try {
try { try {
audioClip = (Clip) AudioSystem.getLine(info); audioClip = (Clip) AudioSystem.getLine(info);
audioClip.addLineListener(this); audioClip.addLineListener(this);
@@ -133,7 +134,9 @@ public class AntSoundPlayer implements LineListener, BuildListener {
playClip(audioClip, loops); playClip(audioClip, loops);
} }
audioClip.drain(); audioClip.drain();
} finally {
audioClip.close(); audioClip.close();
}
} else { } else {
project.log("Can't get data from file " + file.getName()); project.log("Can't get data from file " + file.getName());
} }


+ 5
- 2
src/main/org/apache/tools/ant/types/resources/ResourceList.java View File

@@ -195,15 +195,18 @@ public class ResourceList extends DataType implements ResourceCollection {
crh.setPrimaryReader(input); crh.setPrimaryReader(input);
crh.setFilterChains(filterChains); crh.setFilterChains(filterChains);
crh.setProject(getProject()); crh.setProject(getProject());
BufferedReader reader = new BufferedReader(crh.getAssembledReader());

Union streamResources = new Union(); Union streamResources = new Union();
BufferedReader reader = new BufferedReader(crh.getAssembledReader());
try {
streamResources.setCache(true); streamResources.setCache(true);


String line = null; String line = null;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
streamResources.add(parse(line)); streamResources.add(parse(line));
} }
} finally {
reader.close();
}


return streamResources; return streamResources;
} catch (final IOException ioe) { } catch (final IOException ioe) {


+ 2
- 1
src/main/org/apache/tools/ant/types/resources/Tokens.java View File

@@ -60,8 +60,8 @@ public class Tokens extends BaseResourceCollectionWrapper {
ConcatResourceInputStream cat = new ConcatResourceInputStream(rc); ConcatResourceInputStream cat = new ConcatResourceInputStream(rc);
cat.setManagingComponent(this); cat.setManagingComponent(this);


InputStreamReader rdr = null;
try { try {
InputStreamReader rdr = null;
if (encoding == null) { if (encoding == null) {
rdr = new InputStreamReader(cat); rdr = new InputStreamReader(cat);
} else { } else {
@@ -81,6 +81,7 @@ public class Tokens extends BaseResourceCollectionWrapper {
} catch (IOException e) { } catch (IOException e) {
throw new BuildException("Error reading tokens", e); throw new BuildException("Error reading tokens", e);
} finally { } finally {
FileUtils.close(rdr);
FileUtils.close(cat); FileUtils.close(cat);
} }
} }


Loading…
Cancel
Save