From 4db248f63f4583aff79dc9d43cb64d84d4e33eb9 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Mon, 19 Nov 2001 13:58:52 +0000 Subject: [PATCH] add method that compares file contents to FileUtils, use it in FixCrLf. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269960 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/FixCRLF.java | 53 +--------------- .../org/apache/tools/ant/util/FileUtils.java | 62 +++++++++++++++++-- .../apache/tools/ant/util/FileUtilsTest.java | 18 ++++++ 3 files changed, 79 insertions(+), 54 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java index 13cd65e03..288824d72 100644 --- a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java +++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java @@ -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 { File srcFile = new File(srcDir, file); File destD = destDir == null ? srcDir : destDir; @@ -602,7 +553,7 @@ public class FixCRLF extends MatchingTask { 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 @@ public class FixCRLF extends MatchingTask { tmpFile = null; + } catch (IOException e) { + throw new BuildException(e); } finally { try { if (lines != null) { diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 560f008bb..735d2e654 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -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; @@ -504,5 +507,56 @@ public class FileUtils { } 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) {} + } + } + } } diff --git a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java index 8227c973a..6fc815c66 100644 --- a/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/testcases/org/apache/tools/ant/util/FileUtilsTest.java @@ -298,6 +298,24 @@ public class FileUtilsTest extends TestCase { 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 */