changeset 59381:de66d41b9486

8235668: LineNumberReader#getLineNumber() returns wrong line number (one fewer) in Lucene test Reviewed-by: alanb, rriggs
author bpb
date Thu, 12 Dec 2019 13:43:07 -0800
parents 4b189ba2cb2f
children 9373d791b6bb
files src/java.base/share/classes/java/io/BufferedReader.java src/java.base/share/classes/java/io/LineNumberReader.java test/jdk/java/io/LineNumberReader/Read.java
diffstat 3 files changed, 8 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/io/BufferedReader.java	Thu Dec 12 12:45:03 2019 -0800
+++ b/src/java.base/share/classes/java/io/BufferedReader.java	Thu Dec 12 13:43:07 2019 -0800
@@ -302,8 +302,6 @@
      * (EOF).
      *
      * @param      ignoreLF  If true, the next '\n' will be skipped
-     * @param      term      Output: Whether a line terminator was encountered
-     *                       while reading the line; may be {@code null}.
      *
      * @return     A String containing the contents of the line, not including
      *             any line-termination characters, or null if the end of the
@@ -313,14 +311,13 @@
      *
      * @throws     IOException  If an I/O error occurs
      */
-    String readLine(boolean ignoreLF, boolean[] term) throws IOException {
+    String readLine(boolean ignoreLF) throws IOException {
         StringBuilder s = null;
         int startChar;
 
         synchronized (lock) {
             ensureOpen();
             boolean omitLF = ignoreLF || skipLF;
-            if (term != null) term[0] = false;
 
         bufferLoop:
             for (;;) {
@@ -347,7 +344,6 @@
                 for (i = nextChar; i < nChars; i++) {
                     c = cb[i];
                     if ((c == '\n') || (c == '\r')) {
-                        if (term != null) term[0] = true;
                         eol = true;
                         break charLoop;
                     }
@@ -393,7 +389,7 @@
      * @see java.nio.file.Files#readAllLines
      */
     public String readLine() throws IOException {
-        return readLine(false, null);
+        return readLine(false);
     }
 
     /**
--- a/src/java.base/share/classes/java/io/LineNumberReader.java	Thu Dec 12 12:45:03 2019 -0800
+++ b/src/java.base/share/classes/java/io/LineNumberReader.java	Thu Dec 12 13:43:07 2019 -0800
@@ -25,6 +25,7 @@
 
 package java.io;
 
+
 /**
  * A buffered character-input stream that keeps track of line numbers.  This
  * class defines methods {@link #setLineNumber(int)} and {@link
@@ -199,10 +200,9 @@
      */
     public String readLine() throws IOException {
         synchronized (lock) {
-            boolean[] term = new boolean[1];
-            String l = super.readLine(skipLF, term);
+            String l = super.readLine(skipLF);
             skipLF = false;
-            if (l != null && term[0])
+            if (l != null)
                 lineNumber++;
             return l;
         }
--- a/test/jdk/java/io/LineNumberReader/Read.java	Thu Dec 12 12:45:03 2019 -0800
+++ b/test/jdk/java/io/LineNumberReader/Read.java	Thu Dec 12 13:43:07 2019 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,24 +22,16 @@
  */
 
 /* @test
-   @bug 4074875 4063511 8230342
+   @bug 4074875 4063511
    @summary Make sure LineNumberReader.read(char, int , int) will increase
             the linenumber correctly.
    */
 
-import java.io.IOException;
-import java.io.LineNumberReader;
-import java.io.StringReader;
-import java.util.function.Consumer;
+import java.io.*;
 
 public class Read {
 
     public static void main(String[] args) throws Exception {
-        testReadChars();
-        testEofs();
-    }
-
-    private static void testReadChars() throws Exception {
         String s = "aaaa\nbbb\n";
         char[] buf = new char[5];
         int n = 0;
@@ -57,49 +49,4 @@
             throw new Exception("Failed test: Expected line number: 2, got "
                                 + line);
     }
-
-    private static void testEofs() throws Exception {
-        String string = "first \n second";
-
-        Consumer<LineNumberReader> c = (LineNumberReader r) -> {
-            try {
-                while (r.read() != -1)
-                    continue;
-            } catch (IOException e) {
-                throw new RuntimeException(e);
-            }
-        };
-        testEof(c, string, 1);
-
-        c = (LineNumberReader r) -> {
-            try {
-                char[] buf = new char[128];
-                while (r.read(buf) != -1)
-                    continue;
-            } catch (IOException e) {
-                throw new RuntimeException(e);
-            }
-        };
-        testEof(c, string, 1);
-
-        c = (LineNumberReader r) -> {
-            try {
-                while (r.readLine() != null)
-                    continue;
-            } catch (IOException e) {
-                throw new RuntimeException(e);
-            }
-        };
-        testEof(c, string, 1);
-    }
-
-    private static void testEof(Consumer<LineNumberReader> c, String s, int n)
-        throws Exception {
-        LineNumberReader r = new LineNumberReader(new StringReader(s));
-        c.accept(r);
-        int line;
-        if ((line = r.getLineNumber()) != n)
-            throw new Exception("Failed test: Expected line number: " + n  +
-                " , got " + line);
-    }
 }