| @@ -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); | |||
| } | |||
| } | |||
| @@ -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,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 | |||
| @@ -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 | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -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(); | |||
| } | |||
| } | |||
| @@ -116,6 +116,7 @@ public class AntSoundPlayer implements LineListener, BuildListener { | |||
| AudioFormat format = audioInputStream.getFormat(); | |||
| DataLine.Info info = new DataLine.Info(Clip.class, format, | |||
| AudioSystem.NOT_SPECIFIED); | |||
| try { | |||
| try { | |||
| audioClip = (Clip) AudioSystem.getLine(info); | |||
| audioClip.addLineListener(this); | |||
| @@ -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()); | |||
| } | |||
| @@ -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) { | |||
| @@ -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); | |||
| } | |||
| } | |||