Browse Source

* refactor <filesmatch> 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
master
Stefan Bodewig 23 years ago
parent
commit
fa9eca56a6
2 changed files with 23 additions and 52 deletions
  1. +7
    -51
      src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java
  2. +16
    -1
      src/main/org/apache/tools/ant/util/FileUtils.java

+ 7
- 51
src/main/org/apache/tools/ant/taskdefs/condition/FilesMatch.java View File

@@ -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);
}


+ 16
- 1
src/main/org/apache/tools/ant/util/FileUtils.java View File

@@ -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.
*
* <p>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.</p>
*
* @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 {


Loading…
Cancel
Save