| @@ -355,7 +355,10 @@ public class Main implements AntMain { | |||||
| try { | try { | ||||
| final File logFile = new File(args[i + 1]); | final File logFile = new File(args[i + 1]); | ||||
| i++; | 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; | isLogFileUsed = true; | ||||
| } catch (final IOException ioe) { | } catch (final IOException ioe) { | ||||
| final String msg = "Cannot write on the specified log file. " | final String msg = "Cannot write on the specified log 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 | ||||
| @@ -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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -167,7 +167,7 @@ public class Truncate extends Task { | |||||
| } | } | ||||
| RandomAccessFile raf = null; | RandomAccessFile raf = null; | ||||
| try { | try { | ||||
| raf = new RandomAccessFile(f, READ_WRITE); | |||||
| raf = new RandomAccessFile(f, READ_WRITE); //NOSONAR | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| throw new BuildException("Could not open " + f + " for writing", e); | throw new BuildException("Could not open " + f + " for writing", e); | ||||
| } | } | ||||
| @@ -202,4 +202,4 @@ public class Truncate extends Task { | |||||
| return path; | return path; | ||||
| } | } | ||||
| } | |||||
| } | |||||
| @@ -299,7 +299,7 @@ public class CvsTagDiff extends AbstractCvsTask { | |||||
| BufferedReader reader = null; | BufferedReader reader = null; | ||||
| try { | try { | ||||
| reader = new BufferedReader(new FileReader(tmpFile)); | |||||
| reader = new BufferedReader(new FileReader(tmpFile)); //NOSONAR | |||||
| // entries are of the form: | // entries are of the form: | ||||
| //CVS 1.11 | //CVS 1.11 | ||||
| @@ -249,7 +249,7 @@ public class PropertyFile extends Task { | |||||
| throw new BuildException(x, getLocation()); | throw new BuildException(x, getLocation()); | ||||
| } | } | ||||
| try { | try { | ||||
| OutputStream os = new FileOutputStream(propertyfile); | |||||
| OutputStream os = new FileOutputStream(propertyfile); //NOSONAR | |||||
| try { | try { | ||||
| try { | try { | ||||
| os.write(baos.toByteArray()); | os.write(baos.toByteArray()); | ||||
| @@ -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) { | ||||
| @@ -133,15 +133,19 @@ public class DirectoryIterator implements ClassFileIterator { | |||||
| FileInputStream inFileStream | FileInputStream inFileStream | ||||
| = new FileInputStream(element); | = 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 { | } else { | ||||
| @@ -727,7 +727,8 @@ public class IPlanetEjbc { | |||||
| } else { | } else { | ||||
| location = (String) fileDtds.get(publicId); | location = (String) fileDtds.get(publicId); | ||||
| if (location != null) { | if (location != null) { | ||||
| inputStream = new FileInputStream(location); | |||||
| // closed when the InputSource is closed | |||||
| inputStream = new FileInputStream(location); //NOSONAR | |||||
| } | } | ||||
| } | } | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| @@ -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 | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -459,7 +459,7 @@ public class JDependTask extends Task { | |||||
| PrintWriter pw = null; | PrintWriter pw = null; | ||||
| if (getOutputFile() != null) { | if (getOutputFile() != null) { | ||||
| try { | try { | ||||
| fw = new FileWriter(getOutputFile().getPath()); | |||||
| fw = new FileWriter(getOutputFile().getPath()); //NOSONAR | |||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| String msg = "JDepend Failed when creating the output file: " | String msg = "JDepend Failed when creating the output file: " | ||||
| + e.getMessage(); | + e.getMessage(); | ||||
| @@ -223,51 +223,54 @@ public class jlink { | |||||
| return; | return; | ||||
| } | } | ||||
| ZipFile zipf = new ZipFile(f); | 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(); | |||||
| } | } | ||||
| @@ -117,23 +117,26 @@ public class AntSoundPlayer implements LineListener, BuildListener { | |||||
| 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 { | ||||
| 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 { | } else { | ||||
| project.log("Can't get data from file " + file.getName()); | project.log("Can't get data from file " + file.getName()); | ||||
| } | } | ||||
| @@ -195,14 +195,17 @@ 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(); | ||||
| 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; | return streamResources; | ||||
| @@ -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); | ||||
| } | } | ||||
| } | } | ||||
| @@ -184,7 +184,7 @@ public class ContainsRegexpSelector extends BaseExtendSelector | |||||
| } | } | ||||
| try { | try { | ||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream())); | |||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream())); //NOSONAR | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| throw new BuildException("Could not get InputStream from " | throw new BuildException("Could not get InputStream from " | ||||
| + r.toLongString(), e); | + r.toLongString(), e); | ||||
| @@ -187,9 +187,9 @@ public class ContainsSelector extends BaseExtendSelector implements ResourceSele | |||||
| BufferedReader in = null; | BufferedReader in = null; | ||||
| try { | try { | ||||
| if (encoding != null) { | if (encoding != null) { | ||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream(), encoding)); | |||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream(), encoding)); //NOSONAR | |||||
| } else { | } else { | ||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream())); | |||||
| in = new BufferedReader(new InputStreamReader(r.getInputStream())); //NOSONAR | |||||
| } | } | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| throw new BuildException("Could not get InputStream from " | throw new BuildException("Could not get InputStream from " | ||||
| @@ -1696,7 +1696,7 @@ public class FileUtils { | |||||
| */ | */ | ||||
| public String getDefaultEncoding() { | public String getDefaultEncoding() { | ||||
| InputStreamReader is = new InputStreamReader( | InputStreamReader is = new InputStreamReader( | ||||
| new InputStream() { | |||||
| new InputStream() { //NOSONAR | |||||
| public int read() { | public int read() { | ||||
| return -1; | return -1; | ||||
| } | } | ||||
| @@ -370,8 +370,10 @@ public class ZipFile implements Closeable { | |||||
| final OffsetEntry offsetEntry = ((Entry) ze).getOffsetEntry(); | final OffsetEntry offsetEntry = ((Entry) ze).getOffsetEntry(); | ||||
| ZipUtil.checkRequestedFeatures(ze); | ZipUtil.checkRequestedFeatures(ze); | ||||
| final long start = offsetEntry.dataOffset; | 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 = | final BoundedInputStream bis = | ||||
| new BoundedInputStream(start, ze.getCompressedSize()); | |||||
| new BoundedInputStream(start, ze.getCompressedSize()); //NOSONAR | |||||
| switch (ze.getMethod()) { | switch (ze.getMethod()) { | ||||
| case ZipEntry.STORED: | case ZipEntry.STORED: | ||||
| return bis; | return bis; | ||||