git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269960 13f79535-47bb-0310-9956-ffa450edef68master
@@ -389,55 +389,6 @@ public class FixCRLF extends MatchingTask { | |||||
} | } | ||||
/** | |||||
* 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 { | private void processFile(String file) throws BuildException { | ||||
File srcFile = new File(srcDir, file); | File srcFile = new File(srcDir, file); | ||||
File destD = destDir == null ? srcDir : destDir; | File destD = destDir == null ? srcDir : destDir; | ||||
@@ -602,7 +553,7 @@ public class FixCRLF extends MatchingTask { | |||||
if (destFile.exists()) { | if (destFile.exists()) { | ||||
// Compare the destination with the temp file | // Compare the destination with the temp file | ||||
log("destFile exists", Project.MSG_DEBUG); | log("destFile exists", Project.MSG_DEBUG); | ||||
if ( ! filesEqual(destFile, tmpFile)) { | |||||
if (!fileUtils.contentEquals(destFile, tmpFile)) { | |||||
log(destFile + " is being written", Project.MSG_DEBUG); | log(destFile + " is being written", Project.MSG_DEBUG); | ||||
if (!destFile.delete()) { | if (!destFile.delete()) { | ||||
throw new BuildException("Unable to delete " | throw new BuildException("Unable to delete " | ||||
@@ -638,6 +589,8 @@ public class FixCRLF extends MatchingTask { | |||||
tmpFile = null; | tmpFile = null; | ||||
} catch (IOException e) { | |||||
throw new BuildException(e); | |||||
} finally { | } finally { | ||||
try { | try { | ||||
if (lines != null) { | if (lines != null) { | ||||
@@ -54,14 +54,17 @@ | |||||
package org.apache.tools.ant.util; | 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.BufferedReader; | ||||
import java.io.FileReader; | |||||
import java.io.BufferedWriter; | import java.io.BufferedWriter; | ||||
import java.io.FileWriter; | |||||
import java.io.File; | |||||
import java.io.FileInputStream; | import java.io.FileInputStream; | ||||
import java.io.FileOutputStream; | 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.lang.reflect.Method; | ||||
import java.text.DecimalFormat; | import java.text.DecimalFormat; | ||||
import java.util.Random; | import java.util.Random; | ||||
@@ -504,5 +507,56 @@ public class FileUtils { | |||||
} | } | ||||
return result; | 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) {} | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
@@ -298,6 +298,24 @@ public class FileUtilsTest extends TestCase { | |||||
tmp3.getAbsolutePath()); | tmp3.getAbsolutePath()); | ||||
} | } | ||||
/** | |||||
* 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 | * adapt file separators to local conventions | ||||
*/ | */ | ||||