diff --git a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java index 0b16038f8..067a21b18 100644 --- a/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java +++ b/src/main/org/apache/tools/ant/taskdefs/FixCRLF.java @@ -232,136 +232,131 @@ public class FixCRLF extends MatchingTask { DirectoryScanner ds = super.getDirectoryScanner(srcDir); String[] files = ds.getIncludedFiles(); - try { - for (int i = 0; i < files.length; i++) { - File srcFile = new File(srcDir, files[i]); - - // read the contents of the file - int count = (int)srcFile.length(); - byte indata[] = new byte[count]; - try { - FileInputStream inStream = new FileInputStream(srcFile); - inStream.read(indata); - inStream.close(); - } catch (IOException e) { - throw new BuildException(e); - } + for (int i = 0; i < files.length; i++) { + File srcFile = new File(srcDir, files[i]); + + // read the contents of the file + int count = (int)srcFile.length(); + byte indata[] = new byte[count]; + try { + FileInputStream inStream = new FileInputStream(srcFile); + inStream.read(indata); + inStream.close(); + } catch (IOException e) { + throw new BuildException(e); + } - // count the number of cr, lf, and tab characters - int cr = 0; - int lf = 0; - int tab = 0; + // count the number of cr, lf, and tab characters + int cr = 0; + int lf = 0; + int tab = 0; - for (int k=0; k0) && (indata[count-1] == 0x1A)); - - // log stats (before fixes) - project.log(srcFile + ": size=" + count + " cr=" + cr + - " lf=" + lf + " tab=" + tab + " eof=" + eof, - "fixcrlf", project.MSG_VERBOSE); - - // determine the output buffer size (slightly pessimisticly) - int outsize = count; - if (addcr != 0) outsize-=cr; - if (addcr == +1) outsize+=lf; - if (addtab == -1) outsize+=tab*7; - if (ctrlz == +1) outsize+=1; - - // copy the data - byte outdata[] = new byte[outsize]; - int o = 0; // output offset - int line = o; // beginning of line - int col = 0; // desired column - - for (int k=0; k0) && (indata[count-1] == 0x1A)); + + // log stats (before fixes) + project.log(srcFile + ": size=" + count + " cr=" + cr + + " lf=" + lf + " tab=" + tab + " eof=" + eof, + "fixcrlf", project.MSG_VERBOSE); + + // determine the output buffer size (slightly pessimisticly) + int outsize = count; + if (addcr != 0) outsize-=cr; + if (addcr == +1) outsize+=lf; + if (addtab == -1) outsize+=tab*7; + if (ctrlz == +1) outsize+=1; + + // copy the data + byte outdata[] = new byte[outsize]; + int o = 0; // output offset + int line = o; // beginning of line + int col = 0; // desired column + + for (int k=0; k0 && o+10 && o+12 && outdata[o-1]==0x0A && outdata[o-2]==0x1A) o--; - if (o>1 && outdata[o-1]==0x1A) o--; - } + // add or remove an eof character as required + if (ctrlz == +1) { + if (outdata[o-1]!=0x1A) outdata[o++]=0x1A; + } else if (ctrlz == -1) { + if (o>2 && outdata[o-1]==0x0A && outdata[o-2]==0x1A) o--; + if (o>1 && outdata[o-1]==0x1A) o--; + } - // output the data - try { - File destFile = srcFile; - if (destDir != null) destFile = new File(destDir, files[i]); - FileOutputStream outStream = new FileOutputStream(destFile); - outStream.write(outdata,0,o); - outStream.close(); - } catch (IOException e) { - throw new BuildException(e); - } + // output the data + try { + File destFile = srcFile; + if (destDir != null) destFile = new File(destDir, files[i]); + FileOutputStream outStream = new FileOutputStream(destFile); + outStream.write(outdata,0,o); + outStream.close(); + } catch (IOException e) { + throw new BuildException(e); + } - } /* end for */ - } catch (Exception e) { - e.printStackTrace(); - throw new BuildException(e); - } + } /* end for */ } }