Browse Source

embrace AutoCloseable

master
Stefan Bodewig 9 years ago
parent
commit
4af291c580
22 changed files with 70 additions and 226 deletions
  1. +2
    -11
      src/main/org/apache/tools/ant/AntClassLoader.java
  2. +3
    -21
      src/main/org/apache/tools/ant/taskdefs/Jar.java
  3. +1
    -4
      src/main/org/apache/tools/ant/taskdefs/Javac.java
  4. +1
    -4
      src/main/org/apache/tools/ant/taskdefs/Pack.java
  5. +1
    -6
      src/main/org/apache/tools/ant/taskdefs/Property.java
  6. +4
    -11
      src/main/org/apache/tools/ant/taskdefs/Replace.java
  7. +2
    -13
      src/main/org/apache/tools/ant/taskdefs/SQLExec.java
  8. +1
    -4
      src/main/org/apache/tools/ant/taskdefs/Zip.java
  9. +2
    -6
      src/main/org/apache/tools/ant/taskdefs/email/Message.java
  10. +2
    -5
      src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java
  11. +2
    -7
      src/main/org/apache/tools/ant/taskdefs/email/UUMailer.java
  12. +4
    -15
      src/main/org/apache/tools/ant/taskdefs/optional/PropertyFile.java
  13. +8
    -13
      src/main/org/apache/tools/ant/taskdefs/optional/ReplaceRegExp.java
  14. +2
    -8
      src/main/org/apache/tools/ant/taskdefs/optional/TraXLiaison.java
  15. +1
    -3
      src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java
  16. +3
    -22
      src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java
  17. +3
    -21
      src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java
  18. +1
    -4
      src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java
  19. +2
    -5
      src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java
  20. +1
    -7
      src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java
  21. +23
    -35
      src/main/org/apache/tools/ant/util/FileUtils.java
  22. +1
    -1
      src/main/org/apache/tools/zip/ZipFile.java

+ 2
- 11
src/main/org/apache/tools/ant/AntClassLoader.java View File

@@ -490,19 +490,13 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
+ pathComponent.lastModified() + "-" + pathComponent.length();
String classpath = pathMap.get(absPathPlusTimeAndLength);
if (classpath == null) {
JarFile jarFile = null;
try {
jarFile = new JarFile(pathComponent);
try (JarFile jarFile = new JarFile(pathComponent)) {
final Manifest manifest = jarFile.getManifest();
if (manifest == null) {
return;
}
classpath = manifest.getMainAttributes()
.getValue(Attributes.Name.CLASS_PATH);
} finally {
if (jarFile != null) {
jarFile.close();
}
}
if (classpath == null) {
classpath = "";
@@ -1602,8 +1596,7 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
}

private static boolean readFully(final File f, final byte[] b) throws IOException {
final FileInputStream fis = new FileInputStream(f);
try {
try (FileInputStream fis = new FileInputStream(f)) {
final int len = b.length;
int count = 0, x = 0;
while (count != len) {
@@ -1614,8 +1607,6 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener {
count += x;
}
return count == len;
} finally {
fis.close();
}
}



+ 3
- 21
src/main/org/apache/tools/ant/taskdefs/Jar.java View File

@@ -349,13 +349,7 @@ public class Jar extends Zip {
}
return null;
} finally {
if (zf != null) {
try {
zf.close();
} catch (IOException e) {
// TODO - log an error? throw an exception?
}
}
FileUtils.close(zf);
}
}

@@ -388,13 +382,7 @@ public class Jar extends Zip {
}
return false;
} finally {
if (zf != null) {
try {
zf.close();
} catch (IOException e) {
// TODO - log an error? throw an exception?
}
}
FileUtils.close(zf);
}
}

@@ -1144,9 +1132,7 @@ public class Jar extends Zip {
protected static void grabFilesAndDirs(String file, List<String> dirs,
List<String> files)
throws IOException {
org.apache.tools.zip.ZipFile zf = null;
try {
zf = new org.apache.tools.zip.ZipFile(file, "utf-8");
try (org.apache.tools.zip.ZipFile zf = new org.apache.tools.zip.ZipFile(file, "utf-8")) {
Enumeration<org.apache.tools.zip.ZipEntry> entries = zf.getEntries();
HashSet<String> dirSet = new HashSet<String>();
while (entries.hasMoreElements()) {
@@ -1166,10 +1152,6 @@ public class Jar extends Zip {
}
}
dirs.addAll(dirSet);
} finally {
if (zf != null) {
zf.close();
}
}
}



+ 1
- 4
src/main/org/apache/tools/ant/taskdefs/Javac.java View File

@@ -1420,8 +1420,7 @@ public class Javac extends MatchingTask {
continue;
}
log("Creating empty " + pkgInfoClass);
final OutputStream os = new FileOutputStream(pkgInfoClass);
try {
try (OutputStream os = new FileOutputStream(pkgInfoClass)) {
os.write(PACKAGE_INFO_CLASS_HEADER);
final byte[] name = pkg.getBytes("UTF-8");
final int length = name.length + /* "/package-info" */ 13;
@@ -1429,8 +1428,6 @@ public class Javac extends MatchingTask {
os.write((byte) length % 256);
os.write(name);
os.write(PACKAGE_INFO_CLASS_FOOTER);
} finally {
os.close();
}
}
}


+ 1
- 4
src/main/org/apache/tools/ant/taskdefs/Pack.java View File

@@ -177,11 +177,8 @@ public abstract class Pack extends Task {
*/
protected void zipResource(Resource resource, OutputStream zOut)
throws IOException {
InputStream rIn = resource.getInputStream();
try {
try (InputStream rIn = resource.getInputStream()) {
zipFile(rIn, zOut);
} finally {
rIn.close();
}
}



+ 1
- 6
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -533,13 +533,8 @@ public class Property extends Task {
Properties props = new Properties();
log("Loading " + url, Project.MSG_VERBOSE);
try {
InputStream is = url.openStream();
try {
try (InputStream is = url.openStream()) {
loadProperties(props, is, url.getFile().endsWith(".xml"));
} finally {
if (is != null) {
is.close();
}
}
addProperties(props);
} catch (IOException ex) {


+ 4
- 11
src/main/org/apache/tools/ant/taskdefs/Replace.java View File

@@ -354,7 +354,7 @@ public class Replace extends MatchingTask {
* a StringBuffer. Compatible with the Replacefilter.
* @since 1.7
*/
private class FileInput /* JDK 5: implements Closeable */ {
private class FileInput implements AutoCloseable {
private StringBuffer outputBuffer;
private final InputStream is;
private Reader reader;
@@ -418,7 +418,7 @@ public class Replace extends MatchingTask {
* Replacefilter.
* @since 1.7
*/
private class FileOutput /* JDK 5: implements Closeable */ {
private class FileOutput implements AutoCloseable {
private StringBuffer inputBuffer;
private final OutputStream os;
private Writer writer;
@@ -667,10 +667,8 @@ public class Replace extends MatchingTask {
File temp = FILE_UTILS.createTempFile("rep", ".tmp",
src.getParentFile(), false, true);
try {
FileInput in = new FileInput(src);
try {
FileOutput out = new FileOutput(temp);
try {
try (FileInput in = new FileInput(src);
FileOutput out = new FileOutput(temp)) {
out.setInputBuffer(buildFilterChain(in.getOutputBuffer()));

while (in.readChunk()) {
@@ -682,11 +680,6 @@ public class Replace extends MatchingTask {
flushFilterChain();

out.flush();
} finally {
out.close();
}
} finally {
in.close();
}
boolean changes = (replaceCount != repCountStart);
if (changes) {


+ 2
- 13
src/main/org/apache/tools/ant/taskdefs/SQLExec.java View File

@@ -834,13 +834,7 @@ public class SQLExec extends JDBCTask {
throw e;
}
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
//ignore
}
}
FileUtils.close(resultSet);
}
}

@@ -854,13 +848,8 @@ public class SQLExec extends JDBCTask {
*/
@Deprecated
protected void printResults(PrintStream out) throws SQLException {
ResultSet rs = getStatement().getResultSet();
try {
try (ResultSet rs = getStatement().getResultSet()) {
printResults(rs, out);
} finally {
if (rs != null) {
rs.close();
}
}
}



+ 1
- 4
src/main/org/apache/tools/ant/taskdefs/Zip.java View File

@@ -1911,14 +1911,11 @@ public class Zip extends MatchingTask {
getLocation());
}

final FileInputStream fIn = new FileInputStream(file);
try {
try (FileInputStream fIn = new FileInputStream(file)) {
// ZIPs store time with a granularity of 2 seconds, round up
zipFile(fIn, zOut, vPath,
file.lastModified() + (roundUp ? ROUNDUP_MILLIS : 0),
null, mode);
} finally {
fIn.close();
}
}



+ 2
- 6
src/main/org/apache/tools/ant/taskdefs/email/Message.java View File

@@ -126,17 +126,13 @@ public class Message extends ProjectComponent {
: new BufferedWriter(new OutputStreamWriter(ps));
if (messageSource != null) {
// Read message from a file
Reader freader = getReader(messageSource);

try {
BufferedReader in = new BufferedReader(freader);
try (Reader freader = getReader(messageSource);
BufferedReader in = new BufferedReader(freader)) {
String line = null;
while ((line = in.readLine()) != null) {
out.write(getProject().replaceProperties(line));
out.newLine();
}
} finally {
freader.close();
}
} else {
out.write(getProject().replaceProperties(buffer.substring(0)));


+ 2
- 5
src/main/org/apache/tools/ant/taskdefs/email/PlainMailer.java View File

@@ -147,16 +147,13 @@ class PlainMailer extends Mailer {
int length;
final int maxBuf = 1024;
byte[] buf = new byte[maxBuf];
FileInputStream finstr = new FileInputStream(file);

try {
BufferedInputStream in = new BufferedInputStream(finstr, buf.length);
try (FileInputStream finstr = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(finstr, buf.length)) {

while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
} finally {
finstr.close();
}
}



+ 2
- 7
src/main/org/apache/tools/ant/taskdefs/email/UUMailer.java View File

@@ -40,16 +40,11 @@ class UUMailer extends PlainMailer {
+ "readable.");
}

FileInputStream finstr = new FileInputStream(file);

try {
BufferedInputStream in = new BufferedInputStream(finstr);
try (FileInputStream finstr = new FileInputStream(file);
BufferedInputStream in = new BufferedInputStream(finstr)) {
UUEncoder encoder = new UUEncoder(file.getName());

encoder.encode(in, out);

} finally {
finstr.close();
}
}
}


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

@@ -192,27 +192,16 @@ public class PropertyFile extends Task {
if (propertyfile.exists()) {
log("Updating property file: "
+ propertyfile.getAbsolutePath());
FileInputStream fis = null;
try {
fis = new FileInputStream(propertyfile);
BufferedInputStream bis = new BufferedInputStream(fis);
try (FileInputStream fis = new FileInputStream(propertyfile);
BufferedInputStream bis = new BufferedInputStream(fis)) {
properties.load(bis);
} finally {
if (fis != null) {
fis.close();
}
}
} else {
log("Creating new property file: "
+ propertyfile.getAbsolutePath());
FileOutputStream out = null;
try {
out = new FileOutputStream(propertyfile.getAbsolutePath());
try (FileOutputStream out =
new FileOutputStream(propertyfile.getAbsolutePath())) {
out.flush();
} finally {
if (out != null) {
out.close();
}
}
}
} catch (IOException ioe) {


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

@@ -356,13 +356,13 @@ public class ReplaceRegExp extends Task {
try {
boolean changes = false;

InputStream is = new FileInputStream(f);
try {
Reader r = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
OutputStream os = new FileOutputStream(temp);
try (InputStream is = new FileInputStream(f);
OutputStream os = new FileOutputStream(temp)) {
Reader r = null;
Writer w = null;
try {
Writer w = encoding != null ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os);
r = encoding != null ? new InputStreamReader(is, encoding) : new InputStreamReader(is);
w = encoding != null ? new OutputStreamWriter(os, encoding) : new OutputStreamWriter(os);
log("Replacing pattern '" + regex.getPattern(getProject())
+ "' with '" + subs.getExpression(getProject())
+ "' in '" + f.getPath() + "'" + (byline ? " by line" : "")
@@ -426,15 +426,10 @@ public class ReplaceRegExp extends Task {
} else {
changes = multilineReplace(r, w, options);
}

r.close();
w.close();

} finally {
os.close();
FileUtils.close(r);
FileUtils.close(w);
}
} finally {
is.close();
}
if (changes) {
log("File has changed; saving the updated file", Project.MSG_VERBOSE);


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

@@ -293,17 +293,11 @@ public class TraXLiaison implements XSLTLiaison4, ErrorListener, XSLTLoggerAware
// and avoid keeping the handle until the object is garbaged.
// (always keep control), otherwise you won't be able to delete
// the file quickly on windows.
InputStream xslStream = null;
try {
xslStream
= new BufferedInputStream(stylesheet.getInputStream());
try (InputStream xslStream =
new BufferedInputStream(stylesheet.getInputStream())) {
templatesModTime = stylesheet.getLastModified();
final Source src = getSource(xslStream, stylesheet);
templates = getFactory().newTemplates(src);
} finally {
if (xslStream != null) {
xslStream.close();
}
}
}



+ 1
- 3
src/main/org/apache/tools/ant/taskdefs/optional/depend/AntAnalyzer.java View File

@@ -96,9 +96,7 @@ public class AntAnalyzer extends AbstractAnalyzer {
}
} finally {
FileUtils.close(inStream);
if (zipFile != null) {
zipFile.close();
}
FileUtils.close(zipFile);
}
} catch (IOException ioe) {
// ignore


+ 3
- 22
src/main/org/apache/tools/ant/taskdefs/optional/ejb/WeblogicDeploymentTool.java View File

@@ -866,30 +866,11 @@ public class WeblogicDeploymentTool extends GenericDeploymentTool {

throw new BuildException(msg, ioe);
} finally {
// need to close files and perhaps rename output
if (genericJar != null) {
try {
genericJar.close();
} catch (IOException closeException) {
// empty
}
}

if (wlJar != null) {
try {
wlJar.close();
} catch (IOException closeException) {
// empty
}
}
FileUtils.close(genericJar);
FileUtils.close(wlJar);
FileUtils.close(newJarStream);

if (newJarStream != null) {
try {
newJarStream.close();
} catch (IOException closeException) {
// empty
}

try {
FILE_UTILS.rename(newWLJarFile, weblogicJarFile);
} catch (IOException renameException) {


+ 3
- 21
src/main/org/apache/tools/ant/taskdefs/optional/ejb/WebsphereDeploymentTool.java View File

@@ -832,29 +832,11 @@ public class WebsphereDeploymentTool extends GenericDeploymentTool {
throw new BuildException(msg, ioe);
} finally {
// need to close files and perhaps rename output
if (genericJar != null) {
try {
genericJar.close();
} catch (IOException closeException) {
// Ignore
}
}

if (wasJar != null) {
try {
wasJar.close();
} catch (IOException closeException) {
// Ignore
}
}
FileUtils.close(genericJar);
FileUtils.close(wasJar);
FileUtils.close(newJarStream);

if (newJarStream != null) {
try {
newJarStream.close();
} catch (IOException closeException) {
// Ignore
}

try {
FILE_UTILS.rename(newwasJarFile, websphereJarFile);
} catch (IOException renameException) {


+ 1
- 4
src/main/org/apache/tools/ant/taskdefs/optional/junit/AggregateTransformer.java View File

@@ -168,12 +168,9 @@ public class AggregateTransformer {
protected void setXmlfile(File xmlfile) throws BuildException {
try {
DocumentBuilder builder = privateDBFactory.newDocumentBuilder();
InputStream in = new FileInputStream(xmlfile);
try {
try (InputStream in = new FileInputStream(xmlfile)) {
Document doc = builder.parse(in);
setXmlDocument(doc);
} finally {
in.close();
}
} catch (Exception e) {
throw new BuildException("Error while parsing document: " + xmlfile, e);


+ 2
- 5
src/main/org/apache/tools/ant/taskdefs/optional/junit/XMLResultAggregator.java View File

@@ -213,9 +213,8 @@ public class XMLResultAggregator extends Task implements XMLConstants {
* @throws IOException thrown if there is an error while writing the content.
*/
protected void writeDOMTree(Document doc, File file) throws IOException {
OutputStream os = new FileOutputStream(file);
try {
PrintWriter wri = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(os), "UTF8"));
try (OutputStream os = new FileOutputStream(file);
PrintWriter wri = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(os), "UTF8"))) {
wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
(new DOMElementWriter()).write(doc.getDocumentElement(), wri, 0, " ");
wri.flush();
@@ -223,8 +222,6 @@ public class XMLResultAggregator extends Task implements XMLConstants {
if (wri.checkError()) {
throw new IOException("Error while writing DOM content");
}
} finally {
os.close();
}
}



+ 1
- 7
src/main/org/apache/tools/ant/taskdefs/optional/ssh/SSHExec.java View File

@@ -495,9 +495,7 @@ public class SSHExec extends SSHBase {
*/
private void writeToFile(final String from, final boolean append, final File to)
throws IOException {
FileWriter out = null;
try {
out = new FileWriter(to.getAbsolutePath(), append);
try (FileWriter out = new FileWriter(to.getAbsolutePath(), append)) {
final StringReader in = new StringReader(from);
final char[] buffer = new char[BUFFER_SIZE];
int bytesRead;
@@ -509,10 +507,6 @@ public class SSHExec extends SSHBase {
out.write(buffer, 0, bytesRead);
}
out.flush();
} finally {
if (out != null) {
out.close();
}
}
}



+ 23
- 35
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -1445,13 +1445,7 @@ public class FileUtils {
* @param device output writer, can be null.
*/
public static void close(Writer device) {
if (null != device) {
try {
device.close();
} catch (IOException e) {
//ignore
}
}
close((AutoCloseable) device);
}

/**
@@ -1461,13 +1455,7 @@ public class FileUtils {
* @param device Reader, can be null.
*/
public static void close(Reader device) {
if (null != device) {
try {
device.close();
} catch (IOException e) {
//ignore
}
}
close((AutoCloseable) device);
}

/**
@@ -1477,13 +1465,7 @@ public class FileUtils {
* @param device stream, can be null.
*/
public static void close(OutputStream device) {
if (null != device) {
try {
device.close();
} catch (IOException e) {
//ignore
}
}
close((AutoCloseable) device);
}

/**
@@ -1493,13 +1475,7 @@ public class FileUtils {
* @param device stream, can be null.
*/
public static void close(InputStream device) {
if (null != device) {
try {
device.close();
} catch (IOException e) {
//ignore
}
}
close((AutoCloseable) device);
}

/**
@@ -1510,13 +1486,7 @@ public class FileUtils {
* @since Ant 1.8.0
*/
public static void close(Channel device) {
if (null != device) {
try {
device.close();
} catch (IOException e) {
//ignore
}
}
close((AutoCloseable) device);
}

/**
@@ -1543,6 +1513,24 @@ public class FileUtils {
}
}

/**
* Close an {@link AutoCloseable} without throwing any exception
* if something went wrong. Do not attempt to close it if the
* argument is null.
*
* @param ac AutoCloseable, can be null.
* @since Ant 1.10.0
*/
public static void close(AutoCloseable ac) {
if (null != ac) {
try {
ac.close();
} catch (Exception e) {
//ignore
}
}
}

/**
* Delete the file with {@link File#delete()} if the argument is not null.
* Do nothing on a null argument.


+ 1
- 1
src/main/org/apache/tools/zip/ZipFile.java View File

@@ -72,7 +72,7 @@ import java.util.zip.ZipException;
* </ul>
*
*/
public class ZipFile {
public class ZipFile implements AutoCloseable {
private static final int HASH_SIZE = 509;
static final int NIBLET_MASK = 0x0f;
static final int BYTE_SHIFT = 8;


Loading…
Cancel
Save