| @@ -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. " | |||
| @@ -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 | |||
| @@ -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); | |||
| } | |||
| } | |||
| @@ -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; | |||
| } | |||
| } | |||
| } | |||
| @@ -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 | |||
| @@ -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()); | |||
| @@ -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) { | |||
| @@ -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 { | |||
| @@ -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) { | |||
| @@ -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 | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -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(); | |||
| @@ -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(); | |||
| } | |||
| @@ -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()); | |||
| } | |||
| @@ -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; | |||
| @@ -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); | |||
| } | |||
| } | |||
| @@ -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); | |||
| @@ -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 " | |||
| @@ -1696,7 +1696,7 @@ public class FileUtils { | |||
| */ | |||
| public String getDefaultEncoding() { | |||
| InputStreamReader is = new InputStreamReader( | |||
| new InputStream() { | |||
| new InputStream() { //NOSONAR | |||
| public int read() { | |||
| return -1; | |||
| } | |||
| @@ -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; | |||