harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jessewil...@apache.org
Subject svn commit: r880995 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/BufferedReader.java main/java/java/io/LineNumberReader.java test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java
Date Mon, 16 Nov 2009 22:08:47 GMT
Author: jessewilson
Date: Mon Nov 16 22:08:47 2009
New Revision: 880995

URL: http://svn.apache.org/viewvc?rev=880995&view=rev
Log:
Fixing 2 bugs in LineNumberReader.

We were broken when the caller mixed read() and readLine() calls.
We were broken when the caller called readLine() after the end of the stream.

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/LineNumberReader.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java?rev=880995&r1=880994&r2=880995&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedReader.java
Mon Nov 16 22:08:47 2009
@@ -337,6 +337,17 @@
     }
 
     /**
+     * Peeks at the next input character, refilling the buffer if necessary. If
+     * this character is a newline character ("\n"), it is discarded.
+     */
+    final void chompNewline() throws IOException {
+        if ((pos != end || fillBuf() != -1)
+                && buf[pos] == '\n') {
+            pos++;
+        }
+    }
+
+    /**
      * Returns the next line of text available from this reader. A line is
      * represented by zero or more characters followed by {@code '\n'},
      * {@code '\r'}, {@code "\r\n"} or the end of the reader. The string does

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/LineNumberReader.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/LineNumberReader.java?rev=880995&r1=880994&r2=880995&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/LineNumberReader.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/LineNumberReader.java
Mon Nov 16 22:08:47 2009
@@ -139,8 +139,7 @@
      * <p>
      * The line number count is incremented if a line terminator is encountered.
      * Recognized line terminator sequences are {@code '\r'}, {@code '\n'} and
-     * {@code "\r\n"}. Line terminator sequences are always translated into
-     * {@code '\n'}.
+     * {@code "\r\n"}.
      *
      * @param buffer
      *            the array in which to store the characters read.
@@ -193,8 +192,15 @@
     @Override
     public String readLine() throws IOException {
         synchronized (lock) {
-            lineNumber++;
-            return super.readLine();
+            if (lastWasCR) {
+                chompNewline();
+                lastWasCR = false;
+            }
+            String result = super.readLine();
+            if (result != null) {
+                lineNumber++;
+            }
+            return result;
         }
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java?rev=880995&r1=880994&r2=880995&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/LineNumberReaderTest.java
Mon Nov 16 22:08:47 2009
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
+import java.io.Reader;
 import java.io.StringReader;
 
 import junit.framework.TestCase;
@@ -143,6 +144,54 @@
         assertEquals("Failed to reset reader", "0", line);
     }
 
+    public void testReadLineSourceThrows() throws IOException {
+        lnr = new LineNumberReader(new Reader() {
+            private StringReader delegate = new StringReader("hello\nworld");
+            private int calls = 0;
+            @Override public void close() throws IOException {}
+            @Override public int read(char[] buf, int offset, int len) throws IOException
{
+                if (calls++ < 2) {
+                    throw new IOException();
+                } else {
+                    return delegate.read(buf, offset, len);
+                }
+            }
+        });
+
+        assertEquals(0, lnr.getLineNumber());
+        try {
+            lnr.readLine();
+            fail();
+        } catch (IOException expected) {
+        }
+
+        assertEquals(0, lnr.getLineNumber());
+        try {
+            lnr.readLine();
+            fail();
+        } catch (IOException expected) {
+        }
+
+        assertEquals(0, lnr.getLineNumber());
+        assertEquals("hello", lnr.readLine());
+        assertEquals(1, lnr.getLineNumber());
+        assertEquals("world", lnr.readLine());
+        assertEquals(2, lnr.getLineNumber());
+    }
+
+    public void testGetLineNumberAfterEnd() throws IOException {
+        lnr = new LineNumberReader(new StringReader("hello\nworld"));
+        assertEquals(0, lnr.getLineNumber());
+        assertEquals("hello", lnr.readLine());
+        assertEquals(1, lnr.getLineNumber());
+        assertEquals("world", lnr.readLine());
+        assertEquals(2, lnr.getLineNumber());
+        assertEquals(null, lnr.readLine());
+        assertEquals(2, lnr.getLineNumber());
+        assertEquals(null, lnr.readLine());
+        assertEquals(2, lnr.getLineNumber());
+    }
+
     /**
      * @tests java.io.LineNumberReader#setLineNumber(int)
      */



Mime
View raw message