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 Tue, 29 Jan 2002 17:12:21 GMT
bodewig     02/01/29 09:12:20

  Modified:    src/main/org/apache/tools/ant/taskdefs/condition
                        FilesMatch.java
               src/main/org/apache/tools/ant/util FileUtils.java
  Log:
  * refactor <filesmatch> condition to take advantage of FileUtils.
  
  * improve FileUtils.contentEquals by adding two additional shortcut
    tests.
  
  Revision  Changes    Path
  1.2       +7 -51     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java
  
  Index: FilesMatch.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- FilesMatch.java	14 Jan 2002 00:01:50 -0000	1.1
  +++ FilesMatch.java	29 Jan 2002 17:12:20 -0000	1.2
  @@ -54,9 +54,8 @@
   package org.apache.tools.ant.taskdefs.condition;
   
   import org.apache.tools.ant.BuildException;
  -import java.io.BufferedInputStream;
  +import org.apache.tools.ant.util.FileUtils;
   import java.io.File;
  -import java.io.FileInputStream;
   import java.io.IOException;
   
   /**
  @@ -64,6 +63,7 @@
    * are not looked at at all.
    *
    * @author Steve Loughran
  + * @version $Revision: 1.2 $
    * @created 12 January 2002
    * @since Ant 1.5
    */
  @@ -75,6 +75,10 @@
        */
       private File file1, file2;
   
  +    /**
  +     * Helper that provides the file comparison method.
  +     */
  +    private FileUtils fu = FileUtils.newFileUtils();
   
       /**
        * Sets the File1 attribute 
  @@ -96,43 +100,6 @@
       }
   
       /**
  -     * simple but sub-optimal comparision algorithm. 
  -     * written for working rather than fast. Better would 
  -     * be a block read into buffers followed by long comparisions
  -     * apart from the final 1-7 bytes. 
  -     */
  -     
  -    public boolean simpleFileCompare(File f1,File f2) throws IOException {
  -        BufferedInputStream in1=null;
  -        BufferedInputStream in2=null;
  -        boolean matches=true;
  -        try {
  -            in1=new BufferedInputStream(new FileInputStream(f1));
  -            in2=new BufferedInputStream(new FileInputStream(f2));
  -            int c1,c2;
  -            do {
  -                c1=in1.read();
  -                c2=in2.read();
  -                if(c1!=c2) {
  -                    matches=false;
  -                } 
  -            } while(matches && (c1!=-1));
  -        } finally {
  -            try {
  -                if(in1!=null) {
  -                    in1.close();
  -                }
  -            } catch(IOException e) { }
  -            try {
  -                if(in2!=null) {
  -                    in2.close();
  -                }
  -            } catch(IOException e) { }
  -        }
  -        return matches; 
  -    }
  -
  -    /**
        * comparision method of the interface
        *
        * @return true if the files are equal
  @@ -152,21 +119,10 @@
               throw new BuildException("file " + file2 + " not found");
           }
           
  -        //shortcut tests
  -        //#1 : same filename => true
  -        if(file1.equals(file2)) {
  -            return true;
  -        }
  -        
  -        //#2 : different size =>false
  -        if(file1.length()!=file2.length()) {
  -            return false;
  -        }
  -        
           //#now match the files
           boolean matches=false;
           try {
  -            matches=simpleFileCompare(file1,file2);
  +            matches=fu.contentEquals(file1, file2);
           } catch(IOException ioe) {
               throw new BuildException("when comparing files", ioe);
           }
  
  
  
  1.13      +17 -2     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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- FileUtils.java	20 Jan 2002 20:45:57 -0000	1.12
  +++ FileUtils.java	29 Jan 2002 17:12:20 -0000	1.13
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -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.12 $
  + * @version $Revision: 1.13 $
    */
    
   public class FileUtils {
  @@ -514,6 +514,11 @@
       /**
        * Compares the contents of two files.
        *
  +     * <p>simple but sub-optimal comparision algorithm.  written for
  +     * working rather than fast. Better would be a block read into
  +     * buffers followed by long comparisions apart from the final 1-7
  +     * bytes.</p>
  +     *
        * @since 1.9
        */
       public boolean contentEquals(File f1, File f2) throws IOException {
  @@ -528,6 +533,16 @@
           
           if (f1.isDirectory() || f2.isDirectory()) {
               // don't want to compare directory contents for now
  +            return false;
  +        }
  +        
  +        if (f1.equals(f2)) {
  +            // same filename => true
  +            return true;
  +        }
  +        
  +        if (f1.length() != f2.length()) {
  +            // different size =>false
               return false;
           }
           
  
  
  

--
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