commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1127854 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/input/Tailer.java test/java/org/apache/commons/io/input/TailerTest.java
Date Thu, 26 May 2011 10:03:42 GMT
Author: sebb
Date: Thu May 26 10:03:42 2011
New Revision: 1127854

URL: http://svn.apache.org/viewvc?rev=1127854&view=rev
Log:
IO-274 - Tailer returning partial lines when reaching EOF before EOL
Added version of readLine() that returns null if EOF seen before EOL
Re-enabled test case
Had to add sleep call to allow test file to be cleared up properly

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=1127854&r1=1127853&r2=1127854&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Thu May 26 10:03:42 2011
@@ -41,6 +41,9 @@ The <action> type attribute can be add,u
   <body>
     <release version="1.4" date="Not yet released">
 
+      <action dev="sebb" type="fix" issue="IO-274" due-to="Frank Grimes">
+        Tailer returning partial lines when reaching EOF before EOL
+      </action>
       <action dev="sebb" type="fix" issue="IO-266" due-to="Igor Smereka">
         FileUtils.copyFile() throws IOException when copying large files to a shared directory
       </action>

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=1127854&r1=1127853&r2=1127854&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 May
26 10:03:42 2011
@@ -335,12 +335,42 @@ public class Tailer implements Runnable 
      * @throws java.io.IOException if an I/O error occurs.
      */
     private long readLines(RandomAccessFile reader) throws IOException {
-        String line = reader.readLine();
+        long pos = reader.getFilePointer();
+        String line = readLine(reader);
         while (line != null) {
+            pos = reader.getFilePointer();
             listener.handle(line);
-            line = reader.readLine();
+            line = readLine(reader);
         }
-        return reader.getFilePointer();
+        reader.seek(pos); // Ensure we can re-read if necessary
+        return pos;
     }
 
+    /**
+     * Version of readline() that returns null on EOF rather than a partial line.
+     * @param reader the input file
+     * @return the line, or null if EOF reached before '\n' is seen.
+     * @throws IOException if an error occurs.
+     */
+    private String readLine(RandomAccessFile reader) throws IOException {
+        StringBuffer sb  = new StringBuffer();
+        int ch;
+        boolean seenCR = false;
+        while((ch=reader.read()) != -1) {
+            switch(ch) {
+                case '\n':
+                    return sb.toString();
+                case '\r':
+                    seenCR = true;
+                    break;
+                default:
+                    if (seenCR) {
+                        sb.append('\r');
+                        seenCR = false;
+                    }
+                    sb.append((char)ch); // add character, not its ascii value
+            }
+        }
+        return null;
+    }
 }

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=1127854&r1=1127853&r2=1127854&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
May 26 10:03:42 2011
@@ -52,7 +52,7 @@ public class TailerTest extends FileBase
         FileUtils.deleteDirectory(getTestDirectory());
     }
     
-    public void FIXME_testTailerEof() throws Exception {
+    public void testTailerEof() throws Exception {
         // Create & start the Tailer
         long delay = 50;
         final File file = new File(getTestDirectory(), "tailer2-test.txt");
@@ -81,6 +81,7 @@ public class TailerTest extends FileBase
             listener.clear();
         } finally {
             tailer.stop();
+            Thread.sleep(delay * 2);
             IOUtils.closeQuietly(writer);
         }
     }



Mime
View raw message