You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2005/10/15 01:19:24 UTC

svn commit: r321245 - in /jakarta/commons/proper/io/trunk: RELEASE-NOTES.txt src/java/org/apache/commons/io/FileUtils.java src/test/org/apache/commons/io/FileUtilsTestCase.java

Author: scolebourne
Date: Fri Oct 14 16:19:18 2005
New Revision: 321245

URL: http://svn.apache.org/viewcvs?rev=321245&view=rev
Log:
FileUtils.contentEquals(File,File)
Performance improved by adding length and file location checking
from discussion at The Server Side
http://www.theserverside.com/news/thread.tss?thread_id=37035

Modified:
    jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt?rev=321245&r1=321244&r2=321245&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/io/trunk/RELEASE-NOTES.txt Fri Oct 14 16:19:18 2005
@@ -34,6 +34,8 @@
 
 Enhancements from 1.1
 ---------------------
+- FileUtils.contentEquals(File,File)
+  Performance improved by adding length and file location checking
 
 
 Feedback

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=321245&r1=321244&r2=321245&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Fri Oct 14 16:19:18 2005
@@ -268,6 +268,11 @@
     /**
      * <p>Compare the contents of two files to determine if they are equal or
      * not.</p>
+     *
+     * <p>This method checks to see if the two files are different lengths
+     * or if they point to the same file, before resorting to byte-by-byte
+     * comparison of the contents.</p>
+     *
      * <p>Code origin: Avalon</p>
      *
      * @param file1 the first file
@@ -291,6 +296,16 @@
         if (file1.isDirectory() || file2.isDirectory()) {
             // don't want to compare directory contents
             throw new IOException("Can't compare directories, only files");
+        }
+
+        if (file1.length() != file2.length()) {
+            // lengths differ, cannot be equal
+            return false;
+        }
+
+        if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
+            // same file
+            return true;
         }
 
         InputStream input1 = null;

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=321245&r1=321244&r2=321245&view=diff
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Fri Oct 14 16:19:18 2005
@@ -20,8 +20,6 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
 import java.net.URL;
 import java.util.Arrays;
 import java.util.GregorianCalendar;
@@ -31,9 +29,7 @@
 import junit.framework.TestSuite;
 import junit.textui.TestRunner;
 
-import org.apache.commons.io.output.ByteArrayOutputStream;
 import org.apache.commons.io.testtools.FileBasedTestCase;
-import org.apache.commons.io.testtools.YellOnFlushAndCloseOutputStream;
 
 /**
  * This is used to test FileUtils for correctness.
@@ -226,7 +222,12 @@
     public void testContentEquals() throws Exception {
         // Non-existent files
         File file = new File(getTestDirectory(), getName());
+        File file2 = new File(getTestDirectory(), getName() + "2");
+        // both don't  exist
         assertTrue(FileUtils.contentEquals(file, file));
+        assertTrue(FileUtils.contentEquals(file, file2));
+        assertTrue(FileUtils.contentEquals(file2, file2));
+        assertTrue(FileUtils.contentEquals(file2, file));
 
         // Directories
         try {
@@ -244,6 +245,13 @@
             getClass().getResource("/java/lang/Object.class"),
             objFile1);
 
+        File objFile1b =
+            new File(getTestDirectory(), getName() + ".object2");
+        objFile1.deleteOnExit();
+        FileUtils.copyURLToFile(
+            getClass().getResource("/java/lang/Object.class"),
+            objFile1b);
+
         File objFile2 =
             new File(getTestDirectory(), getName() + ".collection");
         objFile2.deleteOnExit();
@@ -251,13 +259,19 @@
             getClass().getResource("/java/util/Collection.class"),
             objFile2);
 
-        assertTrue(
-            "Files should not be equal.",
-            !FileUtils.contentEquals(objFile1, objFile2));
+        assertEquals(false, FileUtils.contentEquals(objFile1, objFile2));
+        assertEquals(false, FileUtils.contentEquals(objFile1b, objFile2));
+        assertEquals(true, FileUtils.contentEquals(objFile1, objFile1b));
+
+        assertEquals(true, FileUtils.contentEquals(objFile1, objFile1));
+        assertEquals(true, FileUtils.contentEquals(objFile1b, objFile1b));
+        assertEquals(true, FileUtils.contentEquals(objFile2, objFile2));
 
         // Equal files
         file.createNewFile();
-        assertTrue(FileUtils.contentEquals(file, file));
+        file2.createNewFile();
+        assertEquals(true, FileUtils.contentEquals(file, file));
+        assertEquals(true, FileUtils.contentEquals(file, file2));
     }
 
     // copyURLToFile



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org