From fa9eca56a6da096bf979f5509971017d87e4eb23 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 29 Jan 2002 17:12:20 +0000 Subject: [PATCH] * refactor condition to take advantage of FileUtils. * improve FileUtils.contentEquals by adding two additional shortcut tests. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271004 13f79535-47bb-0310-9956-ffa450edef68 --- .../ant/taskdefs/condition/FilesMatch.java | 58 +++---------------- .../org/apache/tools/ant/util/FileUtils.java | 17 +++++- 2 files changed, 23 insertions(+), 52 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java b/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java index a6d251ead..245205252 100644 --- a/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java +++ b/src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java @@ -54,9 +54,8 @@ package org.apache.tools.ant.taskdefs.condition; import org.apache.tools.ant.BuildException; -import java.io.BufferedInputStream; +import org.apache.tools.ant.util.FileUtils; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; /** @@ -64,6 +63,7 @@ import java.io.IOException; * are not looked at at all. * * @author Steve Loughran + * @version $Revision$ * @created 12 January 2002 * @since Ant 1.5 */ @@ -75,6 +75,10 @@ public class FilesMatch implements Condition { */ private File file1, file2; + /** + * Helper that provides the file comparison method. + */ + private FileUtils fu = FileUtils.newFileUtils(); /** * Sets the File1 attribute @@ -95,43 +99,6 @@ public class FilesMatch implements Condition { this.file2 = file2; } - /** - * simple but sub-optimal comparision algorithm. - * written for working rather than fast. Better would - * be a block read into buffers followed by long comparisions - * apart from the final 1-7 bytes. - */ - - public boolean simpleFileCompare(File f1,File f2) throws IOException { - BufferedInputStream in1=null; - BufferedInputStream in2=null; - boolean matches=true; - try { - in1=new BufferedInputStream(new FileInputStream(f1)); - in2=new BufferedInputStream(new FileInputStream(f2)); - int c1,c2; - do { - c1=in1.read(); - c2=in2.read(); - if(c1!=c2) { - matches=false; - } - } while(matches && (c1!=-1)); - } finally { - try { - if(in1!=null) { - in1.close(); - } - } catch(IOException e) { } - try { - if(in2!=null) { - in2.close(); - } - } catch(IOException e) { } - } - return matches; - } - /** * comparision method of the interface * @@ -152,21 +119,10 @@ public class FilesMatch implements Condition { throw new BuildException("file " + file2 + " not found"); } - //shortcut tests - //#1 : same filename => true - if(file1.equals(file2)) { - return true; - } - - //#2 : different size =>false - if(file1.length()!=file2.length()) { - return false; - } - //#now match the files boolean matches=false; try { - matches=simpleFileCompare(file1,file2); + matches=fu.contentEquals(file1, file2); } catch(IOException ioe) { throw new BuildException("when comparing files", ioe); } diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 68f94d4f9..3b70a37a5 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -1,7 +1,7 @@ /* * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights + * Copyright (c) 2001-2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without @@ -514,6 +514,11 @@ public class FileUtils { /** * Compares the contents of two files. * + *

simple but sub-optimal comparision algorithm. written for + * working rather than fast. Better would be a block read into + * buffers followed by long comparisions apart from the final 1-7 + * bytes.

+ * * @since 1.9 */ public boolean contentEquals(File f1, File f2) throws IOException { @@ -531,6 +536,16 @@ public class FileUtils { return false; } + if (f1.equals(f2)) { + // same filename => true + return true; + } + + if (f1.length() != f2.length()) { + // different size =>false + return false; + } + InputStream in1 = null; InputStream in2 = null; try {