From 6231c77d24ce79d308b50e46097851fa8dc63e93 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Tue, 3 Apr 2001 11:26:26 +0000 Subject: [PATCH] will only overwrite files if their content is different from the original. Submitted by: Attila Szegedi git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268905 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 7 ++- .../apache/tools/ant/taskdefs/FixCRLF.java | 57 ++++++++++++++++--- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/build.xml b/build.xml index 310e3929c..09e9ff735 100644 --- a/build.xml +++ b/build.xml @@ -630,13 +630,18 @@ - + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java index 465e738c6..36d880f54 100644 --- a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java +++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java @@ -238,9 +238,9 @@ public class FixCRLF extends MatchingTask { // log options used log("options:" + - " cr=" + (addcr==-1 ? "add" : addcr==0 ? "asis" : "remove") + - " tab=" + (addtab==-1 ? "add" : addtab==0 ? "asis" : "remove") + - " eof=" + (ctrlz==-1 ? "add" : ctrlz==0 ? "asis" : "remove") + + " cr=" + (addcr==1 ? "add" : addcr==0 ? "asis" : "remove") + + " tab=" + (addtab==1 ? "add" : addtab==0 ? "asis" : "remove") + + " eof=" + (ctrlz==1 ? "add" : ctrlz==0 ? "asis" : "remove") + " tablength=" + tablength, Project.MSG_VERBOSE); @@ -363,11 +363,54 @@ public class FixCRLF extends MatchingTask { // output the data try { + // Determine whether it should be written, + // that is if it is different than the potentially already existing file + boolean write = false; + byte[] existingdata = indata; File destFile = srcFile; - if (destDir != null) destFile = new File(destDir, files[i]); - FileOutputStream outStream = new FileOutputStream(destFile); - outStream.write(outdata,0,o); - outStream.close(); + if (destDir != null) { + destFile = new File(destDir, files[i]); + if(destFile.isFile()) { + int len = (int)destFile.length(); + if(len != o) { + write = true; + } else { + existingdata = new byte[len]; + try { + FileInputStream in = new FileInputStream(destFile); + in.read(existingdata); + in.close(); + } catch (IOException e) { + throw new BuildException(e); + } + } + } else { + write = true; + } + } + + if(!write) { + if(existingdata.length != o) { + write = true; + } else { + for(int j = 0; j < o; ++j) { + if(existingdata[j] != outdata[j]) { + write = true; + break; + } + } + } + } + + if(write) { + log(destFile + " is being written", Project.MSG_VERBOSE); + FileOutputStream outStream = new FileOutputStream(destFile); + outStream.write(outdata,0,o); + outStream.close(); + } else { + log(destFile + " is not written, as the contents are identical", + Project.MSG_VERBOSE); + } } catch (IOException e) { throw new BuildException(e); }