| @@ -1343,9 +1343,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo | |||
| // we can find the class we want. | |||
| final String classFilename = getClassFilename(name); | |||
| for (final File pathComponent : pathComponents) { | |||
| InputStream stream = null; | |||
| try { | |||
| stream = getResourceStream(pathComponent, classFilename); | |||
| try (InputStream stream = getResourceStream(pathComponent, classFilename)) { | |||
| if (stream != null) { | |||
| log("Loaded from " + pathComponent + " " | |||
| + classFilename, Project.MSG_DEBUG); | |||
| @@ -1357,8 +1355,6 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener, Clo | |||
| // ioe.printStackTrace(); | |||
| log("Exception reading component " + pathComponent + " (reason: " | |||
| + ioe.getMessage() + ")", Project.MSG_VERBOSE); | |||
| } finally { | |||
| FileUtils.close(stream); | |||
| } | |||
| } | |||
| throw new ClassNotFoundException(name); | |||
| @@ -28,7 +28,6 @@ import java.util.ArrayList; | |||
| import java.util.Collections; | |||
| import java.util.List; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import org.apache.tools.ant.util.LoaderUtils; | |||
| /** | |||
| @@ -146,16 +145,11 @@ public class ArgumentProcessorRegistry { | |||
| private ArgumentProcessor getProcessorByService(InputStream is) | |||
| throws IOException { | |||
| InputStreamReader isr = null; | |||
| try { | |||
| isr = new InputStreamReader(is, StandardCharsets.UTF_8); | |||
| BufferedReader rd = new BufferedReader(isr); | |||
| try (BufferedReader rd = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { | |||
| String processorClassName = rd.readLine(); | |||
| if (processorClassName != null && !processorClassName.isEmpty()) { | |||
| return getProcessor(processorClassName); | |||
| } | |||
| } finally { | |||
| FileUtils.close(isr); | |||
| } | |||
| return null; | |||
| } | |||
| @@ -40,7 +40,6 @@ import org.apache.tools.ant.launch.Launcher; | |||
| import org.apache.tools.ant.taskdefs.Definer; | |||
| import org.apache.tools.ant.taskdefs.Property; | |||
| import org.apache.tools.ant.taskdefs.Typedef; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| /** | |||
| * Component creation and configuration. | |||
| @@ -785,9 +784,7 @@ public class ComponentHelper { | |||
| String resource = type ? MagicNames.TYPEDEFS_PROPERTIES_RESOURCE | |||
| : MagicNames.TASKDEF_PROPERTIES_RESOURCE; | |||
| String errorString = type ? ERROR_NO_TYPE_LIST_LOAD : ERROR_NO_TASK_LIST_LOAD; | |||
| InputStream in = null; | |||
| try { | |||
| in = ComponentHelper.class.getResourceAsStream(resource); | |||
| try (InputStream in = ComponentHelper.class.getResourceAsStream(resource)) { | |||
| if (in == null) { | |||
| throw new BuildException(errorString); | |||
| } | |||
| @@ -796,8 +793,6 @@ public class ComponentHelper { | |||
| defaultDefinitions[idx] = p; | |||
| } catch (IOException e) { | |||
| throw new BuildException(errorString, e); | |||
| } finally { | |||
| FileUtils.close(in); | |||
| } | |||
| } | |||
| return defaultDefinitions[idx]; | |||
| @@ -637,15 +637,11 @@ public class Main implements AntMain { | |||
| private void loadPropertyFiles() { | |||
| for (final String filename : propertyFiles) { | |||
| final Properties props = new Properties(); | |||
| InputStream fis = null; | |||
| try { | |||
| fis = Files.newInputStream(Paths.get(filename)); | |||
| try (InputStream fis = Files.newInputStream(Paths.get(filename))) { | |||
| props.load(fis); | |||
| } catch (final IOException e) { | |||
| System.out.println("Could not load property file " | |||
| + filename + ": " + e.getMessage()); | |||
| } finally { | |||
| FileUtils.close(fis); | |||
| } | |||
| // ensure that -D properties take precedence | |||
| @@ -113,22 +113,20 @@ public class ProjectHelperImpl extends ProjectHelper { | |||
| + "default plugin"); | |||
| } | |||
| File bFile = (File) source; | |||
| InputStream inputStream = null; | |||
| InputSource inputSource = null; | |||
| this.project = project; | |||
| this.buildFile = new File(bFile.getAbsolutePath()); | |||
| buildFileParent = new File(this.buildFile.getParent()); | |||
| try { | |||
| try { | |||
| parser = JAXPUtils.getParser(); | |||
| } catch (BuildException e) { | |||
| parser = new XMLReaderAdapter(JAXPUtils.getXMLReader()); | |||
| } | |||
| parser = JAXPUtils.getParser(); | |||
| } catch (BuildException e) { | |||
| parser = new XMLReaderAdapter(JAXPUtils.getXMLReader()); | |||
| } | |||
| try (InputStream inputStream = Files.newInputStream(bFile.toPath())) { | |||
| String uri = FILE_UTILS.toURI(bFile.getAbsolutePath()); | |||
| inputStream = Files.newInputStream(bFile.toPath()); | |||
| inputSource = new InputSource(inputStream); | |||
| InputSource inputSource = new InputSource(inputStream); | |||
| inputSource.setSystemId(uri); | |||
| project.log("parsing buildfile " + bFile + " with URI = " + uri, Project.MSG_VERBOSE); | |||
| HandlerBase hb = new RootHandler(this); | |||
| @@ -138,8 +136,8 @@ public class ProjectHelperImpl extends ProjectHelper { | |||
| parser.setDTDHandler(hb); | |||
| parser.parse(inputSource); | |||
| } catch (SAXParseException exc) { | |||
| Location location = new Location(exc.getSystemId(), exc.getLineNumber(), exc | |||
| .getColumnNumber()); | |||
| Location location = new Location(exc.getSystemId(), exc.getLineNumber(), | |||
| exc.getColumnNumber()); | |||
| Throwable t = exc.getException(); | |||
| if (t instanceof BuildException) { | |||
| @@ -162,8 +160,6 @@ public class ProjectHelperImpl extends ProjectHelper { | |||
| throw new BuildException("Encoding of project file is invalid.", exc); | |||
| } catch (IOException exc) { | |||
| throw new BuildException("Error reading project file: " + exc.getMessage(), exc); | |||
| } finally { | |||
| FileUtils.close(inputStream); | |||
| } | |||
| } | |||
| @@ -18,6 +18,7 @@ | |||
| package org.apache.tools.ant.taskdefs; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.io.OutputStream; | |||
| import org.apache.tools.ant.BuildException; | |||
| @@ -76,26 +77,17 @@ public class EchoXML extends XMLFragment { | |||
| * Execute the task. | |||
| */ | |||
| public void execute() { | |||
| DOMElementWriter writer = | |||
| new DOMElementWriter(!append, namespacePolicy.getPolicy()); | |||
| OutputStream os = null; | |||
| try { | |||
| if (file != null) { | |||
| os = FileUtils.newOutputStream(file.toPath(), append); | |||
| } else { | |||
| os = new LogOutputStream(this, Project.MSG_INFO); | |||
| } | |||
| Node n = getFragment().getFirstChild(); | |||
| if (n == null) { | |||
| throw new BuildException(ERROR_NO_XML); | |||
| } | |||
| Node n = getFragment().getFirstChild(); | |||
| if (n == null) { | |||
| throw new BuildException(ERROR_NO_XML); | |||
| } | |||
| DOMElementWriter writer = new DOMElementWriter(!append, namespacePolicy.getPolicy()); | |||
| try (OutputStream os = (file == null) ? new LogOutputStream(this, Project.MSG_INFO) | |||
| : FileUtils.newOutputStream(file.toPath(), append)) { | |||
| writer.write((Element) n, os); | |||
| } catch (BuildException e) { | |||
| throw e; | |||
| } catch (Exception e) { | |||
| } catch (IOException e) { | |||
| throw new BuildException(e); | |||
| } finally { | |||
| FileUtils.close(os); | |||
| } | |||
| } | |||
| @@ -21,10 +21,10 @@ import java.io.BufferedWriter; | |||
| import java.io.File; | |||
| import java.io.FileWriter; | |||
| import java.io.IOException; | |||
| import java.util.Locale; | |||
| import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.Project; | |||
| import org.apache.tools.ant.taskdefs.condition.Os; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| /** | |||
| @@ -78,18 +78,12 @@ public class Jikes { | |||
| File tmpFile = null; | |||
| try { | |||
| String myos = System.getProperty("os.name"); | |||
| // Windows has a 32k limit on total arg size, so | |||
| // create a temporary file to store all the arguments | |||
| if (myos.toLowerCase(Locale.ENGLISH).contains("windows") | |||
| && args.length > MAX_FILES_ON_COMMAND_LINE) { | |||
| BufferedWriter out = null; | |||
| try { | |||
| tmpFile = FileUtils.getFileUtils().createTempFile("jikes", | |||
| "tmp", null, false, true); | |||
| out = new BufferedWriter(new FileWriter(tmpFile)); | |||
| if (Os.isFamily(Os.FAMILY_WINDOWS) && args.length > MAX_FILES_ON_COMMAND_LINE) { | |||
| tmpFile = FileUtils.getFileUtils().createTempFile("jikes", | |||
| "tmp", null, false, true); | |||
| try (BufferedWriter out = new BufferedWriter(new FileWriter(tmpFile))) { | |||
| for (String arg : args) { | |||
| out.write(arg); | |||
| out.newLine(); | |||
| @@ -98,10 +92,7 @@ public class Jikes { | |||
| commandArray = new String[] {command, | |||
| "@" + tmpFile.getAbsolutePath()}; | |||
| } catch (IOException e) { | |||
| throw new BuildException("Error creating temporary file", | |||
| e); | |||
| } finally { | |||
| FileUtils.close(out); | |||
| throw new BuildException("Error creating temporary file", e); | |||
| } | |||
| } else { | |||
| commandArray = new String[args.length + 1]; | |||
| @@ -123,10 +114,8 @@ public class Jikes { | |||
| throw new BuildException("Error running Jikes compiler", e); | |||
| } | |||
| } finally { | |||
| if (tmpFile != null) { | |||
| if (!tmpFile.delete()) { | |||
| tmpFile.deleteOnExit(); | |||
| } | |||
| if (tmpFile != null && !tmpFile.delete()) { | |||
| tmpFile.deleteOnExit(); | |||
| } | |||
| } | |||
| } | |||
| @@ -25,7 +25,7 @@ import java.io.InputStreamReader; | |||
| import java.io.PrintWriter; | |||
| import java.io.Reader; | |||
| import java.io.StringWriter; | |||
| import java.io.UnsupportedEncodingException; | |||
| import java.nio.charset.Charset; | |||
| import java.nio.charset.StandardCharsets; | |||
| import java.util.Collections; | |||
| import java.util.Enumeration; | |||
| @@ -37,7 +37,6 @@ import java.util.Objects; | |||
| import java.util.Vector; | |||
| import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import org.apache.tools.ant.util.StreamUtils; | |||
| /** | |||
| @@ -93,7 +92,7 @@ public class Manifest { | |||
| + "with \"" + ATTRIBUTE_FROM + "\" in \""; | |||
| /** Encoding to be used for JAR files. */ | |||
| public static final String JAR_ENCODING = "UTF-8"; | |||
| public static final Charset JAR_ENCODING = StandardCharsets.UTF_8; | |||
| private static final String ATTRIBUTE_MANIFEST_VERSION_LC = | |||
| ATTRIBUTE_MANIFEST_VERSION.toLowerCase(Locale.ENGLISH); | |||
| @@ -751,35 +750,25 @@ public class Manifest { | |||
| * default manifest | |||
| */ | |||
| public static Manifest getDefaultManifest() throws BuildException { | |||
| InputStreamReader insr = null; | |||
| String defManifest = "/org/apache/tools/ant/defaultManifest.mf"; | |||
| try (InputStream in = Manifest.class.getResourceAsStream(defManifest)) { | |||
| if (in == null) { | |||
| throw new BuildException("Could not find default manifest: %s", | |||
| defManifest); | |||
| } | |||
| try { | |||
| insr = new InputStreamReader(in, StandardCharsets.UTF_8); | |||
| Manifest defaultManifest = new Manifest(insr); | |||
| String version = System.getProperty("java.runtime.version"); | |||
| if (version == null) { | |||
| version = System.getProperty("java.vm.version"); | |||
| } | |||
| Attribute createdBy = new Attribute("Created-By", | |||
| version + " (" | |||
| + System.getProperty("java.vm.vendor") + ")"); | |||
| defaultManifest.getMainSection().storeAttribute(createdBy); | |||
| return defaultManifest; | |||
| } catch (UnsupportedEncodingException e) { | |||
| insr = new InputStreamReader(in); | |||
| return new Manifest(insr); | |||
| Manifest defaultManifest = new Manifest(new InputStreamReader(in, JAR_ENCODING)); | |||
| String version = System.getProperty("java.runtime.version"); | |||
| if (version == null) { | |||
| version = System.getProperty("java.vm.version"); | |||
| } | |||
| Attribute createdBy = new Attribute("Created-By", version | |||
| + " (" + System.getProperty("java.vm.vendor") + ")"); | |||
| defaultManifest.getMainSection().storeAttribute(createdBy); | |||
| return defaultManifest; | |||
| } catch (ManifestException e) { | |||
| throw new BuildException("Default manifest is invalid !!", e); | |||
| } catch (IOException e) { | |||
| throw new BuildException("Unable to read default manifest", e); | |||
| } finally { | |||
| FileUtils.close(insr); | |||
| } | |||
| } | |||
| @@ -648,33 +648,20 @@ public class Property extends Task { | |||
| protected void loadResource(String name) { | |||
| Properties props = new Properties(); | |||
| log("Resource Loading " + name, Project.MSG_VERBOSE); | |||
| ClassLoader cL = null; | |||
| boolean cleanup = false; | |||
| if (classpath != null) { | |||
| cleanup = true; | |||
| cL = getProject().createClassLoader(classpath); | |||
| } else { | |||
| cL = this.getClass().getClassLoader(); | |||
| } | |||
| InputStream is = null; | |||
| try { | |||
| if (cL == null) { | |||
| is = ClassLoader.getSystemResourceAsStream(name); | |||
| ClassLoader cL = (classpath == null) ? this.getClass().getClassLoader() | |||
| : getProject().createClassLoader(classpath); | |||
| try (InputStream is = (cL == null) ? ClassLoader.getSystemResourceAsStream(name) | |||
| : cL.getResourceAsStream(name)) { | |||
| if (is == null) { | |||
| log("Unable to find resource " + name, Project.MSG_WARN); | |||
| } else { | |||
| is = cL.getResourceAsStream(name); | |||
| } | |||
| if (is != null) { | |||
| loadProperties(props, is, name.endsWith(".xml")); | |||
| addProperties(props); | |||
| } else { | |||
| log("Unable to find resource " + name, Project.MSG_WARN); | |||
| } | |||
| } catch (IOException ex) { | |||
| throw new BuildException(ex, getLocation()); | |||
| } finally { | |||
| FileUtils.close(is); | |||
| if (cleanup && cL != null) { | |||
| if (classpath != null && cL != null) { | |||
| ((AntClassLoader) cL).cleanup(); | |||
| } | |||
| } | |||
| @@ -26,7 +26,6 @@ import org.apache.tools.ant.BuildException; | |||
| import org.apache.tools.ant.Project; | |||
| import org.apache.tools.ant.taskdefs.LogOutputStream; | |||
| import org.apache.tools.ant.types.Commandline; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import org.apache.tools.ant.util.JavaEnvUtils; | |||
| /** | |||
| @@ -50,8 +49,7 @@ public class Javac12 extends DefaultCompilerAdapter { | |||
| attributes.log("Using classic compiler", Project.MSG_VERBOSE); | |||
| Commandline cmd = setupJavacCommand(true); | |||
| OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN); | |||
| try { | |||
| try (OutputStream logstr = new LogOutputStream(attributes, Project.MSG_WARN)) { | |||
| // Create an instance of the compiler, redirecting output to | |||
| // the project log | |||
| Class<?> c = Class.forName(CLASSIC_COMPILER_CLASSNAME); | |||
| @@ -78,8 +76,6 @@ public class Javac12 extends DefaultCompilerAdapter { | |||
| throw new BuildException("Error starting classic compiler: ", | |||
| ex, location); | |||
| } | |||
| } finally { | |||
| FileUtils.close(logstr); | |||
| } | |||
| } | |||
| } | |||
| @@ -119,8 +119,7 @@ public class Message extends ProjectComponent { | |||
| : new BufferedWriter(new OutputStreamWriter(ps, charset)); | |||
| if (messageSource != null) { | |||
| // Read message from a file | |||
| try (Reader freader = getReader(messageSource); | |||
| BufferedReader in = new BufferedReader(freader)) { | |||
| try (BufferedReader in = new BufferedReader(getReader(messageSource))) { | |||
| String line; | |||
| while ((line = in.readLine()) != null) { | |||
| out.write(getProject().replaceProperties(line)); | |||
| @@ -187,27 +187,18 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware | |||
| createTransformer(); | |||
| } | |||
| InputStream fis = null; | |||
| OutputStream fos = null; | |||
| try { | |||
| fis = new BufferedInputStream(Files.newInputStream(infile.toPath())); | |||
| fos = new BufferedOutputStream(Files.newOutputStream(outfile.toPath())); | |||
| // autoclose all handles, otherwise the garbage collector will close them... | |||
| // and Windows may complain about not being able to delete files. | |||
| try (InputStream fis = new BufferedInputStream(Files.newInputStream(infile.toPath())); | |||
| OutputStream fos = new BufferedOutputStream(Files.newOutputStream(outfile.toPath()))) { | |||
| final StreamResult res = new StreamResult(fos); | |||
| // not sure what could be the need of this... | |||
| res.setSystemId(JAXPUtils.getSystemId(outfile)); | |||
| final Source src = getSource(fis, infile); | |||
| // set parameters on each transformation, maybe something has changed | |||
| //(e.g. value of file name parameter) | |||
| setTransformationParameters(); | |||
| transformer.transform(src, res); | |||
| } finally { | |||
| // make sure to close all handles, otherwise the garbage | |||
| // collector will close them...whenever possible and | |||
| // Windows may complain about not being able to delete files. | |||
| FileUtils.close(fis); | |||
| FileUtils.close(fos); | |||
| transformer.transform(getSource(fis, infile), res); | |||
| } | |||
| } | |||
| @@ -29,7 +29,6 @@ import java.util.Vector; | |||
| import java.util.zip.ZipEntry; | |||
| import java.util.zip.ZipFile; | |||
| import org.apache.tools.ant.util.FileUtils; | |||
| import org.apache.tools.ant.util.depend.AbstractAnalyzer; | |||
| /** | |||
| @@ -69,23 +68,13 @@ public class AntAnalyzer extends AbstractAnalyzer { | |||
| } | |||
| containers.add(container); | |||
| ZipFile zipFile = null; | |||
| InputStream inStream = null; | |||
| try { | |||
| if (container.getName().endsWith(".class")) { | |||
| inStream = Files.newInputStream(Paths.get(container.getPath())); | |||
| } else { | |||
| zipFile = new ZipFile(container.getPath()); | |||
| String entryName = classname.replace('.', '/') + ".class"; | |||
| ZipEntry entry = new ZipEntry(entryName); | |||
| inStream = zipFile.getInputStream(entry); | |||
| } | |||
| try (InputStream inStream = container.getName().endsWith(".class") | |||
| ? Files.newInputStream(Paths.get(container.getPath())) | |||
| : new ZipFile(container.getPath()).getInputStream(new ZipEntry( | |||
| classname.replace('.', '/') + ".class"))) { | |||
| ClassFile classFile = new ClassFile(); | |||
| classFile.read(inStream); | |||
| analyzedDeps.addAll(classFile.getClassRefs()); | |||
| } finally { | |||
| FileUtils.close(inStream); | |||
| FileUtils.close(zipFile); | |||
| } | |||
| } catch (IOException ioe) { | |||
| // ignore | |||
| @@ -1203,28 +1203,18 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR | |||
| private static void registerNonCrash() | |||
| throws IOException { | |||
| if (crashFile != null) { | |||
| FileWriter out = null; | |||
| try { | |||
| out = new FileWriter(crashFile); | |||
| try (FileWriter out = new FileWriter(crashFile)) { | |||
| out.write(Constants.TERMINATED_SUCCESSFULLY + "\n"); | |||
| out.flush(); | |||
| } finally { | |||
| FileUtils.close(out); | |||
| } | |||
| } | |||
| } | |||
| private static void registerTestCase(final String testCase) { | |||
| if (crashFile != null) { | |||
| try { | |||
| FileWriter out = null; | |||
| try { | |||
| out = new FileWriter(crashFile); | |||
| out.write(testCase + "\n"); | |||
| out.flush(); | |||
| } finally { | |||
| FileUtils.close(out); | |||
| } | |||
| try (FileWriter out = new FileWriter(crashFile)) { | |||
| out.write(testCase + "\n"); | |||
| out.flush(); | |||
| } catch (final IOException e) { | |||
| // ignored. | |||
| } | |||
| @@ -53,18 +53,14 @@ public class HashvalueAlgorithm implements Algorithm { | |||
| // Because the content is only read the file will not be damaged. I tested | |||
| // with JPG, ZIP and PDF as binary files. | |||
| public String getValue(File file) { | |||
| Reader r = null; | |||
| try { | |||
| if (!file.canRead()) { | |||
| return null; | |||
| } | |||
| r = new FileReader(file); | |||
| if (!file.canRead()) { | |||
| return null; | |||
| } | |||
| try (Reader r = new FileReader(file)) { | |||
| int hash = FileUtils.readFully(r).hashCode(); | |||
| return Integer.toString(hash); | |||
| } catch (Exception e) { | |||
| return null; | |||
| } finally { | |||
| FileUtils.close(r); | |||
| } | |||
| } | |||
| @@ -31,7 +31,6 @@ import java.net.MalformedURLException; | |||
| import java.net.URL; | |||
| import java.net.URLConnection; | |||
| import java.nio.channels.Channel; | |||
| import java.nio.file.FileSystem; | |||
| import java.nio.file.Files; | |||
| import java.nio.file.NoSuchFileException; | |||
| import java.nio.file.Path; | |||