diff --git a/src/main/org/apache/tools/ant/taskdefs/Replace.java b/src/main/org/apache/tools/ant/taskdefs/Replace.java index 79c5f8794..4e9264040 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Replace.java +++ b/src/main/org/apache/tools/ant/taskdefs/Replace.java @@ -57,6 +57,7 @@ package org.apache.tools.ant.taskdefs; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; +import org.apache.tools.ant.util.FileUtils; import java.io.File; import java.io.FileInputStream; @@ -101,6 +102,8 @@ public class Replace extends MatchingTask { /** The encoding used to read and write files - if null, uses default */ private String encoding = null; + private FileUtils fileUtils = FileUtils.newFileUtils(); + //Inner class public class NestedString { @@ -302,20 +305,19 @@ public class Replace extends MatchingTask { throw new BuildException("Replace: source file " + src.getPath() + " doesn't exist", location); } - File temp = new File(src.getPath() + ".temp"); - - if (temp.exists()) { - throw new BuildException("Replace: temporary file " + temp.getPath() + " already exists", location); - } + File temp = fileUtils.createTempFile("rep", ".tmp", + fileUtils.getParentFile(src)); + Reader reader = null; + Writer writer = null; try { - Reader fileReader = encoding == null ? new FileReader(src) - : new InputStreamReader(new FileInputStream(src), encoding); - Writer fileWriter = encoding == null ? new FileWriter(temp) - : new OutputStreamWriter(new FileOutputStream(temp), encoding); + reader = encoding == null ? new FileReader(src) + : new InputStreamReader(new FileInputStream(src), encoding); + writer = encoding == null ? new FileWriter(temp) + : new OutputStreamWriter(new FileOutputStream(temp), encoding); - BufferedReader br = new BufferedReader(fileReader); - BufferedWriter bw = new BufferedWriter(fileWriter); + BufferedReader br = new BufferedReader(reader); + BufferedWriter bw = new BufferedWriter(writer); // read the entire file into a StringBuffer // size of work buffer may be bigger than needed @@ -365,7 +367,9 @@ public class Replace extends MatchingTask { // cleanup bw.close(); + writer = null; br.close(); + reader = null; // If there were changes, move the new one to the old one; // otherwise, delete the new one @@ -373,13 +377,27 @@ public class Replace extends MatchingTask { ++fileCount; src.delete(); temp.renameTo(src); - } else { - temp.delete(); + temp = null; } } catch (IOException ioe) { throw new BuildException("IOException in " + src + " - " + ioe.getClass().getName() + ":" + ioe.getMessage(), ioe, location); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) {} + } + if (writer != null) { + try { + writer.close(); + } catch (IOException e) {} + } + if (temp != null) { + temp.delete(); + } } + } private String processReplacefilters(String buffer, String filename) { diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 735d2e654..f282ba50a 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -211,7 +211,7 @@ public class FileUtils { // ensure that parent dir of dest file exists! // not using getParentFile method to stay 1.1 compat - File parent = new File(destFile.getParent()); + File parent = getParentFile(destFile); if (!parent.exists()) { parent.mkdirs(); } @@ -347,14 +347,13 @@ public class FileUtils { while (tok.hasMoreTokens()) { String part = tok.nextToken(); if (part.equals("..")) { - String parentFile = helpFile.getParent(); - if (parentFile == null) { + helpFile = getParentFile(helpFile); + if (helpFile == null) { String msg = "The file or path you specified (" + filename + ") is invalid relative to " + file.getPath(); throw new BuildException(msg); } - helpFile = new File(parentFile); } else if (part.equals(".")) { // Do nothing here } else { @@ -558,5 +557,20 @@ public class FileUtils { } } } + + /** + * Emulation of File.getParentFile for JDK 1.1 + * + * @since 1.10 + */ + public File getParentFile(File f) { + if (f != null) { + String p = f.getParent(); + if (p != null) { + return new File(p); + } + } + return null; + } }