You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2011/11/11 03:14:00 UTC
svn commit: r1200693 - in /commons/proper/io/trunk/src: changes/
main/java/org/apache/commons/io/ test/java/org/apache/commons/io/
test/resources/ test/resources/org/ test/resources/org/apache/
test/resources/org/apache/commons/ test/resources/org/apac...
Author: sebb
Date: Fri Nov 11 02:13:59 2011
New Revision: 1200693
URL: http://svn.apache.org/viewvc?rev=1200693&view=rev
Log:
IO-275 FileUtils.contentEquals/IOUtils.contentEquals - Add option to ignore "line endings"
Added:
commons/proper/io/trunk/src/test/resources/
commons/proper/io/trunk/src/test/resources/org/
commons/proper/io/trunk/src/test/resources/org/apache/
commons/proper/io/trunk/src/test/resources/org/apache/commons/
commons/proper/io/trunk/src/test/resources/org/apache/commons/io/
commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat (with props)
commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat (with props)
commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat (with props)
Modified:
commons/proper/io/trunk/src/changes/changes.xml
commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1200693&r1=1200692&r2=1200693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Fri Nov 11 02:13:59 2011
@@ -39,6 +39,12 @@ The <action> type attribute can be add,u
</properties>
<body>
+ <release version="2.1.1" date="TBA">
+ <action dev="sebb" type="add" issue="IO-275" due-to="CJ Aspromgos">
+ FileUtils.contentEquals and IOUtils.contentEquals - Add option to ignore "line endings"
+ Added contentEqualsIgnoreEOL methods to both classes
+ </action>
+ </release>
<!-- The 2.1 release date is the date RC is cut -->
<release version="2.1" date="2011-Sep-28">
<action dev="ggregory" type="add" issue="IO-285" due-to="ggregory">
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1200693&r1=1200692&r2=1200693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Fri Nov 11 02:13:59 2011
@@ -23,7 +23,9 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
+import java.io.Reader;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
@@ -622,6 +624,63 @@ public class FileUtils {
//-----------------------------------------------------------------------
/**
+ * Compares the contents of two files to determine if they are equal or not.
+ * <p>
+ * This method checks to see if the two files point to the same file,
+ * before resorting to line-by-line comparison of the contents.
+ * <p>
+ *
+ * @param file1 the first file
+ * @param file2 the second file
+ * @param charsetName the character encoding to be used.
+ * May be null, in which case the platform default is used
+ * @return true if the content of the files are equal or neither exists,
+ * false otherwise
+ * @throws IOException in case of an I/O error
+ * @since 2.1.1
+ * @see IOUtils#contentEqualsIgnoreEOL(Reader, Reader)
+ */
+ public static boolean contentEqualsIgnoreEOL(File file1, File file2, String charsetName) throws IOException {
+ boolean file1Exists = file1.exists();
+ if (file1Exists != file2.exists()) {
+ return false;
+ }
+
+ if (!file1Exists) {
+ // two not existing files are equal
+ return true;
+ }
+
+ if (file1.isDirectory() || file2.isDirectory()) {
+ // don't want to compare directory contents
+ throw new IOException("Can't compare directories, only files");
+ }
+
+ if (file1.getCanonicalFile().equals(file2.getCanonicalFile())) {
+ // same file
+ return true;
+ }
+
+ Reader input1 = null;
+ Reader input2 = null;
+ try {
+ if (charsetName == null) {
+ input1 = new InputStreamReader(new FileInputStream(file1));
+ input2 = new InputStreamReader(new FileInputStream(file2));
+ } else {
+ input1 = new InputStreamReader(new FileInputStream(file1), charsetName);
+ input2 = new InputStreamReader(new FileInputStream(file2), charsetName);
+ }
+ return IOUtils.contentEqualsIgnoreEOL(input1, input2);
+
+ } finally {
+ IOUtils.closeQuietly(input1);
+ IOUtils.closeQuietly(input2);
+ }
+ }
+
+ //-----------------------------------------------------------------------
+ /**
* Convert from a <code>URL</code> to a <code>File</code>.
* <p>
* From version 1.1 this method will decode the URL.
Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java?rev=1200693&r1=1200692&r2=1200693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/IOUtils.java Fri Nov 11 02:13:59 2011
@@ -1629,6 +1629,44 @@ public class IOUtils {
}
/**
+ * Compare the contents of two Readers to determine if they are equal or
+ * not, ignoring EOL characters.
+ * <p>
+ * This method buffers the input internally using
+ * <code>BufferedReader</code> if they are not already buffered.
+ *
+ * @param input1 the first reader
+ * @param input2 the second reader
+ * @return true if the content of the readers are equal (ignoring EOL differences), false otherwise
+ * @throws NullPointerException if either input is null
+ * @throws IOException if an I/O error occurs
+ * @since Commons IO 2.1.1
+ */
+ public static boolean contentEqualsIgnoreEOL(Reader input1, Reader input2)
+ throws IOException {
+ BufferedReader br1;
+ if (input1 instanceof BufferedReader) {
+ br1 = (BufferedReader) input1;
+ } else {
+ br1 = new BufferedReader(input1);
+ }
+ BufferedReader br2;
+ if (input2 instanceof BufferedReader) {
+ br2 = (BufferedReader) input2;
+ } else {
+ br2 = new BufferedReader(input2);
+ }
+
+ String line1 = br1.readLine();
+ String line2 = br2.readLine();
+ while (line1 != null && line2 != null && line1.equals(line2)) {
+ line1 = br1.readLine();
+ line2 = br2.readLine();
+ }
+ return line1 == null ? (line2 == null ? true : false) : line1.equals(line2);
+ }
+
+ /**
* Skip bytes from an input byte stream.
* This implementation guarantees that it will read as many bytes
* as possible before giving up; this may not always be the case for
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java?rev=1200693&r1=1200692&r2=1200693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/FileUtilsTestCase.java Fri Nov 11 02:13:59 2011
@@ -539,6 +539,75 @@ public class FileUtilsTestCase extends F
assertEquals(true, FileUtils.contentEquals(file, file2));
}
+ public void testContentEqualsIgnoreEOL() throws Exception {
+ // Non-existent files
+ File file1 = new File(getTestDirectory(), getName());
+ File file2 = new File(getTestDirectory(), getName() + "2");
+ // both don't exist
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file1, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file2, file2, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file2, file1, null));
+
+ // Directories
+ try {
+ FileUtils.contentEqualsIgnoreEOL(getTestDirectory(), getTestDirectory(), null);
+ fail("Comparing directories should fail with an IOException");
+ } catch (IOException ioe) {
+ //expected
+ }
+
+ // Different files
+ File tfile1 = new File(getTestDirectory(), getName() + ".txt1");
+ tfile1.deleteOnExit();
+ FileUtils.write(tfile1,"123\r");
+
+ File tfile2 = new File(getTestDirectory(), getName() + ".txt2");
+ tfile1.deleteOnExit();
+ FileUtils.write(tfile2,"123\n");
+
+ File tfile3 = new File(getTestDirectory(), getName() + ".collection");
+ tfile3.deleteOnExit();
+ FileUtils.write(tfile3,"123\r\n2");
+
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile1, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile2, tfile2, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile3, tfile3, null));
+
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile2, null));
+ assertFalse(FileUtils.contentEqualsIgnoreEOL(tfile1, tfile3, null));
+ assertFalse(FileUtils.contentEqualsIgnoreEOL(tfile2, tfile3, null));
+
+ URL urlCR = getClass().getResource("FileUtilsTestDataCR.dat");
+ assertNotNull(urlCR);
+ File cr = new File(urlCR.getPath());
+ assertTrue(cr.exists());
+
+ URL urlCRLF = getClass().getResource("FileUtilsTestDataCRLF.dat");
+ assertNotNull(urlCRLF);
+ File crlf = new File(urlCRLF.getPath());
+ assertTrue(crlf.exists());
+
+ URL urlLF = getClass().getResource("FileUtilsTestDataLF.dat");
+ assertNotNull(urlLF);
+ File lf = new File(urlLF.getPath());
+ assertTrue(lf.exists());
+
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, cr, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(crlf, crlf, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(lf, lf, null));
+
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, crlf, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(cr, lf, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(crlf, lf, null));
+
+ // Equal files
+ file1.createNewFile();
+ file2.createNewFile();
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file1, null));
+ assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
+ }
+
// copyURLToFile
public void testCopyURLToFile() throws Exception {
Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java?rev=1200693&r1=1200692&r2=1200693&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/IOUtilsTestCase.java Fri Nov 11 02:13:59 2011
@@ -663,4 +663,28 @@ public class IOUtilsTestCase extends Fil
testURLToString("US-ASCII");
}
+ public void testcontentEqualsIgnoreEOL() throws Exception {
+ Reader r1;
+ Reader r2;
+
+ r1 = new CharArrayReader("".toCharArray());
+ r2 = new CharArrayReader("".toCharArray());
+ assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
+
+ r1 = new CharArrayReader("1".toCharArray());
+ r2 = new CharArrayReader("1".toCharArray());
+ assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
+
+ r1 = new CharArrayReader("1".toCharArray());
+ r2 = new CharArrayReader("2".toCharArray());
+ assertFalse(IOUtils.contentEqualsIgnoreEOL(r1, r2));
+
+ r1 = new CharArrayReader("123\rabc".toCharArray());
+ r2 = new CharArrayReader("123\nabc".toCharArray());
+ assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
+
+ r1 = new CharArrayReader("321".toCharArray());
+ r2 = new CharArrayReader("321\r\n".toCharArray());
+ assertTrue(IOUtils.contentEqualsIgnoreEOL(r1, r2));
+ }
}
Added: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat?rev=1200693&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat (added)
+++ commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat Fri Nov 11 02:13:59 2011
@@ -0,0 +1 @@
+1
2
3
\ No newline at end of file
Propchange: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCR.dat
------------------------------------------------------------------------------
svn:eol-style = CR
Added: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat?rev=1200693&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat (added)
+++ commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat Fri Nov 11 02:13:59 2011
@@ -0,0 +1,3 @@
+1
+2
+3
Propchange: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataCRLF.dat
------------------------------------------------------------------------------
svn:eol-style = CRLF
Added: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat?rev=1200693&view=auto
==============================================================================
--- commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat (added)
+++ commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat Fri Nov 11 02:13:59 2011
@@ -0,0 +1,3 @@
+1
+2
+3
Propchange: commons/proper/io/trunk/src/test/resources/org/apache/commons/io/FileUtilsTestDataLF.dat
------------------------------------------------------------------------------
svn:eol-style = LF