JDK-8230342 : LineNumberReader.getLineNumber() returns inconsistent results after EOF
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 11,13,14
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_10
  • CPU: x86_64
  • Submitted: 2019-08-27
  • Updated: 2019-12-11
  • Resolved: 2019-09-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 14
14 b15Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
LineNumberReader.getLineNumber() returns inconsistent results after reaching EOF depending on the methods which were called to read.

readLine() always returns a line number one higher than if EOF has been reached using the other methods.


---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;

public class EofLineNumberTest {
    public static void main(String[] args) throws IOException {
        String string = "first \n second";
        /*
         * getLineNumber() is described as "Get the current line number."
         * However, the result is inconsistent after EOF
         */
        System.out.println("Line number after EOF:");
        System.out.println("  read(): " + linesRead(string));
        System.out.println("  read(char[]): " + linesReadBuffer(string));
        System.out.println("  readLine(): " + linesReadLine(string));
    }
    
    private static int linesRead(String string) throws IOException {
        LineNumberReader reader = new LineNumberReader(new StringReader(string));
        while (reader.read() != -1) { }
        
        return reader.getLineNumber();
    }
    
    private static int linesReadBuffer(String string) throws IOException {
        LineNumberReader reader = new LineNumberReader(new StringReader(string));
        char[] buff = new char[512];
        while (reader.read(buff) != -1) { }
        
        return reader.getLineNumber();
    }
    
    private static int linesReadLine(String string) throws IOException {
        LineNumberReader reader = new LineNumberReader(new StringReader(string));
        while (reader.readLine() != null) { }
        
        return reader.getLineNumber();
    }
}

---------- END SOURCE ----------

FREQUENCY : always



Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/e64fec9f1773 User: bpb Date: 2019-09-11 19:32:42 +0000
11-09-2019

To reproduce the issue, run the attached test case: JDK 11.0.4 - Fail JDK 13-ea+32 - Fail JDK 14-ea+8 - Fail Output: Line number after EOF: read(): 1 read(char[]): 1 readLine(): 2
29-08-2019