Author: sebb Date: Thu Jun 7 23:23:48 2012 New Revision: 1347829 URL: http://svn.apache.org/viewvc?rev=1347829&view=rev Log: IO-334 - Tailer#readLines - incorrect CR handling
Modified: commons/proper/io/trunk/src/changes/changes.xml commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java Modified: commons/proper/io/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1347829&r1=1347828&r2=1347829&view=diff ============================================================================== --- commons/proper/io/trunk/src/changes/changes.xml (original) +++ commons/proper/io/trunk/src/changes/changes.xml Thu Jun 7 23:23:48 2012 @@ -47,6 +47,9 @@ The <action> type attribute can be add,u <body> <!-- The release date is the date RC is cut --> <release version="2.4" date="2012-TDB-TDB" description=""> + <action issue="IO-335" dev="sebb" type="fix"> + Tailer#readLines - incorrect CR handling. + </action> <action issue="IO-334" dev="sebb" type="fix"> FileUtils.toURLs throws NPE for null parameter. Documented the behaviour. Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java?rev=1347829&r1=1347828&r2=1347829&view=diff ============================================================================== --- commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java (original) +++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/input/Tailer.java Thu Jun 7 23:23:48 2012 @@ -385,17 +385,23 @@ public class Tailer implements Runnable byte ch = inbuf[i]; switch (ch) { case '\n': + seenCR = false; // swallow CR before LF listener.handle(sb.toString()); - sb = new StringBuilder(); + sb.setLength(0); rePos = pos + i + 1; break; case '\r': + if (seenCR) { + sb.append('\r'); + } seenCR = true; break; default: if (seenCR) { - sb.append('\r'); - seenCR = false; + seenCR = false; // swallow final CR + listener.handle(sb.toString()); + sb.setLength(0); + rePos = pos + i + 1; } sb.append((char) ch); // add character, not its ascii value } Modified: commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java?rev=1347829&r1=1347828&r2=1347829&view=diff ============================================================================== --- commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java (original) +++ commons/proper/io/trunk/src/test/java/org/apache/commons/io/input/TailerTest.java Thu Jun 7 23:23:48 2012 @@ -290,6 +290,35 @@ public class TailerTest extends FileBase assertEquals("fileRotated should be not be called", 0 , listener.rotated); } + public void testIO335() throws Exception { // test CR behaviour + // Create & start the Tailer + long delayMillis = 50; + final File file = new File(getTestDirectory(), "tailer-testio334.txt"); + createFile(file, 0); + final TestTailerListener listener = new TestTailerListener(); + tailer = new Tailer(file, listener, delayMillis, false); + final Thread thread = new Thread(tailer); + thread.start(); + + // Write some lines to the file + writeString(file, "CRLF\r\nLF\nCR\rCRCR\r\rtrail"); + // 1 2 3 4 + final long testDelayMillis = delayMillis * 10; + Thread.sleep(testDelayMillis); + List<String> lines = listener.getLines(); + assertEquals("line count", 4, lines.size()); + assertEquals("line 1", "CRLF", lines.get(0)); + assertEquals("line 2", "LF", lines.get(1)); + assertEquals("line 3", "CR", lines.get(2)); + assertEquals("line 4", "CRCR\r", lines.get(3)); + + // Stop + tailer.stop(); + tailer=null; + thread.interrupt(); + Thread.sleep(testDelayMillis); + } + /** * Test {@link TailerListener} implementation. */