Browse Source

Make <fixcrlf> work for long lines as well.

PR: 4186, 4646, 4709


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269907 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
286537d1f8
6 changed files with 47 additions and 31 deletions
  1. +2
    -0
      WHATSNEW
  2. +8
    -0
      src/etc/testcases/taskdefs/fixcrlf/build.xml
  3. +2
    -0
      src/etc/testcases/taskdefs/fixcrlf/expected/longlines.lf
  4. +2
    -0
      src/etc/testcases/taskdefs/fixcrlf/input/longlines.crlf
  5. +27
    -31
      src/main/org/apache/tools/ant/taskdefs/FixCRLF.java
  6. +6
    -0
      src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java

+ 2
- 0
WHATSNEW View File

@@ -32,6 +32,8 @@ Fixed bugs:
of targets (it can now) and nested "unknown" elements have always of targets (it can now) and nested "unknown" elements have always
been considered to be tasks (changed as well). been considered to be tasks (changed as well).


* <fixcrlf> would fail for files that contained lines longer than 8kB.

Other changes: Other changes:
-------------- --------------




+ 8
- 0
src/etc/testcases/taskdefs/fixcrlf/build.xml View File

@@ -106,4 +106,12 @@
/> />
</target> </target>


<target name="testLongLines" depends="init">
<fixcrlf srcdir="input" destdir="result"
includes="longlines.crlf"
javafiles="false"
cr="remove"
/>
</target>

</project> </project>

+ 2
- 0
src/etc/testcases/taskdefs/fixcrlf/expected/longlines.lf
File diff suppressed because it is too large
View File


+ 2
- 0
src/etc/testcases/taskdefs/fixcrlf/input/longlines.crlf
File diff suppressed because it is too large
View File


+ 27
- 31
src/main/org/apache/tools/ant/taskdefs/FixCRLF.java View File

@@ -858,7 +858,8 @@ public class FixCRLF extends MatchingTask {
private StringBuffer eofStr = new StringBuffer(); private StringBuffer eofStr = new StringBuffer();


private BufferedReader reader; private BufferedReader reader;
private String line;
private StringBuffer line = new StringBuffer();
private boolean reachedEof = false;


public OneLiner(File srcFile) public OneLiner(File srcFile)
throws BuildException throws BuildException
@@ -874,33 +875,25 @@ public class FixCRLF extends MatchingTask {


protected void nextLine() protected void nextLine()
throws BuildException { throws BuildException {
int ch;
int ch = -1;
int eolcount = 0; int eolcount = 0;


eolStr.setLength(0); eolStr.setLength(0);
line.setLength(0);


try { try {
int linelen;

reader.mark(INBUFLEN);
line = reader.readLine();
if (line == null) {
// Eof has been reached
linelen = 0;
}
else {
linelen = line.length();
ch = reader.read();
while (ch != -1 && ch != '\r' && ch != '\n') {
line.append((char) ch);
ch = reader.read();
} }
if (ch == -1 && line.length() == 0) {
// Eof has been reached
reachedEof = true;
return;
}
// Find the EOL character(s)

reader.reset();

// an IOException will be thrown
reader.skip((long)linelen);
reader.mark(INBUFLEN);
ch = reader.read();
switch ((char) ch) { switch ((char) ch) {
case '\r': case '\r':
// Check for \r, \r\n and \r\r\n // Check for \r, \r\n and \r\r\n
@@ -928,20 +921,23 @@ public class FixCRLF extends MatchingTask {
} // end of switch ((char) ch) } // end of switch ((char) ch)


// Reset the position of the file reader
reader.reset();
reader.skip((long)eolcount);

// if at eolcount == 0 and trailing characters of string // if at eolcount == 0 and trailing characters of string
// are CTRL-Zs, set eofStr // are CTRL-Zs, set eofStr
if (line != null && eolcount == 0) {
int i = linelen;
while (--i >= 0 && line.charAt(i) == CTRLZ) {}
if (i < linelen - 1) {
if (eolcount == 0) {
int i = line.length();
while (--i >= 0 && line.charAt(i) == CTRLZ) {
// keep searching for the first ^Z
}
if (i < line.length() - 1) {
// Trailing characters are ^Zs // Trailing characters are ^Zs
// Construct new line and eofStr // Construct new line and eofStr
eofStr.append(line.substring(i + 1)); eofStr.append(line.substring(i + 1));
line = i < 0 ? null : line.substring(0, i + 1);
if (i < 0) {
line.setLength(0);
reachedEof = true;
} else {
line.setLength(i + 1);
}
} }
} // end of if (eolcount == 0) } // end of if (eolcount == 0)
@@ -965,7 +961,7 @@ public class FixCRLF extends MatchingTask {


public boolean hasMoreElements() public boolean hasMoreElements()
{ {
return line != null;
return !reachedEof;
} }


public Object nextElement() public Object nextElement()
@@ -975,7 +971,7 @@ public class FixCRLF extends MatchingTask {
throw new NoSuchElementException("OneLiner"); throw new NoSuchElementException("OneLiner");
} }
BufferLine tmpLine = BufferLine tmpLine =
new BufferLine(line, eolStr.toString());
new BufferLine(line.toString(), eolStr.toString());
nextLine(); nextLine();
return tmpLine; return tmpLine;
} }


+ 6
- 0
src/testcases/org/apache/tools/ant/taskdefs/FixCrLfTest.java View File

@@ -165,6 +165,12 @@ public class FixCrLfTest extends TaskdefsTest {
new File("src/etc/testcases/taskdefs/fixcrlf/result/input.crlf.utf16")); new File("src/etc/testcases/taskdefs/fixcrlf/result/input.crlf.utf16"));
} }
public void testLongLines() throws IOException {
executeTarget("testLongLines");
assertEqualContent(new File("src/etc/testcases/taskdefs/fixcrlf/expected/longlines.lf"),
new File("src/etc/testcases/taskdefs/fixcrlf/result/longlines.crlf"));
}
public void assertEqualContent(File expect, File result) public void assertEqualContent(File expect, File result)
throws AssertionFailedError, IOException { throws AssertionFailedError, IOException {
if (!result.exists()) { if (!result.exists()) {


Loading…
Cancel
Save