ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/main/org/apache/tools/ant/util FileUtils.java
Date Mon, 19 Nov 2001 14:15:44 GMT
bodewig     01/11/19 06:15:44

  Modified:    src/main/org/apache/tools/ant/taskdefs Replace.java
               src/main/org/apache/tools/ant/util FileUtils.java
  Log:
  Make <replace> more robust by:
  
  (1) making sure that files will be closed
  (2) deleting temporary files
  
  even if something goes wrong.
  
  Revision  Changes    Path
  1.19      +31 -13    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java
  
  Index: Replace.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Replace.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Replace.java	2001/10/28 21:26:29	1.18
  +++ Replace.java	2001/11/19 14:15:43	1.19
  @@ -57,6 +57,7 @@
   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 @@
       /** 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 {
   
  @@ -301,21 +304,20 @@
           if (!src.exists()) {
               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 @@
   
               // 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 @@
                   ++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) {
  
  
  
  1.10      +19 -5     jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- FileUtils.java	2001/11/19 13:58:52	1.9
  +++ FileUtils.java	2001/11/19 14:15:43	1.10
  @@ -85,7 +85,7 @@
    * @author <a href="mailto:conor@apache.org">Conor MacNeill</a>
    * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> 
    *
  - * @version $Revision: 1.9 $
  + * @version $Revision: 1.10 $
    */
    
   public class FileUtils {
  @@ -211,7 +211,7 @@
   
               // 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 @@
           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 {
  @@ -557,6 +556,21 @@
                   } catch (IOException e) {}
               }
           }
  +    }
  +
  +    /**
  +     * 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;
       }
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message