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/testcases/org/apache/tools/ant/util FileUtilsTest.java
Date Mon, 19 Nov 2001 13:58:52 GMT
bodewig     01/11/19 05:58:52

  Modified:    src/main/org/apache/tools/ant/taskdefs FixCRLF.java
               src/main/org/apache/tools/ant/util FileUtils.java
               src/testcases/org/apache/tools/ant/util FileUtilsTest.java
  Log:
  add method that compares file contents to FileUtils, use it in FixCrLf.
  
  Revision  Changes    Path
  1.24      +4 -51     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  
  Index: FixCRLF.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- FixCRLF.java	2001/11/13 14:49:48	1.23
  +++ FixCRLF.java	2001/11/19 13:58:52	1.24
  @@ -115,7 +115,7 @@
    *
    * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
    * @author <a href="mailto:pbwest@powerup.com.au">Peter B. West</a>
  - * @version $Revision: 1.23 $ $Name:  $
  + * @version $Revision: 1.24 $ $Name:  $
    */
   
   public class FixCRLF extends MatchingTask {
  @@ -389,55 +389,6 @@
       }
   
   
  -    /**
  -     * Checks for the inequality of two files
  -     */
  -    private boolean filesEqual(File file1, File file2) {
  -        BufferedReader reader1 = null;
  -        BufferedReader reader2 = null;
  -        char buf1[] = new char[INBUFLEN];
  -        char buf2[] = new char[INBUFLEN];
  -        int buflen;
  -
  -        if (file1.length() != file2.length()) {
  -            return false;
  -        }
  -        
  -        try {
  -             reader1 = new BufferedReader
  -                     (getReader(file1), INBUFLEN);
  -             reader2 = new BufferedReader
  -                     (getReader(file2), INBUFLEN);
  -             while ((buflen = reader1.read(buf1, 0, INBUFLEN)) != -1 ) {
  -                 reader2.read(buf2, 0, INBUFLEN);
  -                 // Compare the contents of the buffers
  -                 // There must be an easier way to do this, but I don''t
  -                 // know what it is
  -                 for (int i = 0; i < buflen; i++) {
  -                     if (buf1[i] != buf2[i]) {
  -                         return false;
  -                     } // end of if (buf1[i] != buf2[i])
  -                 }
  -             }
  -             return true;   // equal
  -        } catch (IOException e) {
  -            throw new BuildException("IOException in filesEqual: " +
  -                                      file1 + " : " + file2);
  -        } finally {
  -            if (reader1 != null) {
  -                try {
  -                    reader1.close();
  -                } catch (IOException e) {}
  -            }
  -            if (reader2 != null) {
  -                try {
  -                    reader2.close();
  -                } catch (IOException e) {}
  -            }
  -        }
  -    }
  -
  -
       private void processFile(String file) throws BuildException {
           File srcFile = new File(srcDir, file);
           File destD = destDir == null ? srcDir : destDir;
  @@ -602,7 +553,7 @@
               if (destFile.exists()) {
                   // Compare the destination with the temp file
                   log("destFile exists", Project.MSG_DEBUG);
  -                if ( ! filesEqual(destFile, tmpFile)) {
  +                if (!fileUtils.contentEquals(destFile, tmpFile)) {
                       log(destFile + " is being written", Project.MSG_DEBUG);
                       if (!destFile.delete()) {
                           throw new BuildException("Unable to delete "
  @@ -638,6 +589,8 @@
   
               tmpFile = null;
   
  +        } catch (IOException e) {
  +            throw new BuildException(e);
           } finally {
               try {
                   if (lines != null) {
  
  
  
  1.9       +59 -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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- FileUtils.java	2001/11/02 16:36:49	1.8
  +++ FileUtils.java	2001/11/19 13:58:52	1.9
  @@ -54,14 +54,17 @@
   
   package org.apache.tools.ant.util;
   
  -import java.io.IOException;
  -import java.io.File;
  +import java.io.BufferedInputStream;
   import java.io.BufferedReader;
  -import java.io.FileReader;
   import java.io.BufferedWriter;
  -import java.io.FileWriter;
  +import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileOutputStream;
  +import java.io.FileReader;
  +import java.io.FileWriter;
  +import java.io.IOException;
  +import java.io.InputStream;
  +import java.io.InputStream;
   import java.lang.reflect.Method;
   import java.text.DecimalFormat;
   import java.util.Random;
  @@ -82,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.8 $
  + * @version $Revision: 1.9 $
    */
    
   public class FileUtils {
  @@ -503,6 +506,57 @@
               } while (result.exists());
           }
           return result;
  +    }
  +
  +    /**
  +     * Compares the contents of two files.
  +     *
  +     * @since 1.9
  +     */
  +    public boolean contentEquals(File f1, File f2) throws IOException {
  +        if (f1.exists() != f2.exists()) {
  +            return false;
  +        }
  +
  +        if (!f1.exists()) {
  +            // two not existing files are equal
  +            return true;
  +        }
  +        
  +        if (f1.isDirectory() || f2.isDirectory()) {
  +            // don't want to compare directory contents for now
  +            return false;
  +        }
  +        
  +        InputStream in1 = null;
  +        InputStream in2 = null;
  +        try {
  +            in1 = new BufferedInputStream(new FileInputStream(f1));
  +            in2 = new BufferedInputStream(new FileInputStream(f2));
  +
  +            int expectedByte = in1.read();
  +            while (expectedByte != -1) {
  +                if (expectedByte != in2.read()) {
  +                    return false;
  +                }
  +                expectedByte = in1.read();
  +            }
  +            if (in2.read() != -1) {
  +                return false;
  +            }
  +            return true;
  +        } finally {
  +            if (in1 != null) {
  +                try {
  +                    in1.close();
  +                } catch (IOException e) {}
  +            }
  +            if (in2 != null) {
  +                try {
  +                    in2.close();
  +                } catch (IOException e) {}
  +            }
  +        }
       }
   }
   
  
  
  
  1.6       +18 -0     jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java
  
  Index: FileUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- FileUtilsTest.java	2001/11/02 16:36:49	1.5
  +++ FileUtilsTest.java	2001/11/19 13:58:52	1.6
  @@ -299,6 +299,24 @@
       }
   
       /**
  +     * Test contentEquals
  +     */
  +    public void testContentEquals() throws IOException {
  +        assertTrue("Non existing files", fu.contentEquals(new File("foo"), 
  +                                                          new File("bar")));
  +        assertTrue("One exists, the other one doesn\'t", 
  +                   !fu.contentEquals(new File("foo"), new File("build.xml")));
  +        assertTrue("Don\'t compare directories",
  +                   !fu.contentEquals(new File("src"), new File("src")));
  +        assertTrue("File equals itself",
  +                   fu.contentEquals(new File("build.xml"), 
  +                                    new File("build.xml")));
  +        assertTrue("Files are different",
  +                   !fu.contentEquals(new File("build.xml"), 
  +                                     new File("docs.xml")));
  +    }
  +
  +    /**
        * adapt file separators to local conventions
        */
       private String localize(String path) {
  
  
  

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