8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF

Roger Riggs Roger.Riggs at oracle.com
Mon Sep 9 14:35:22 UTC 2019

Hi Brian,

I would lean toward updating the spec to reflect the current implementation.

A simple program that uses readline and prints the line number and line 
would show a duplicate line number.

try (LineNumberReader r =
              new LineNumberReader(Files.newBufferedReader(Path.of(args[0])))) {
     String s;
     while ((s = r.readLine()) !=null) {
         System.out.printf("%3d: %s%n", r.getLineNumber(), s);

   1: 123
   1: ABC

Other questions and comments:

- Is the use of AtomicBoolean due to concurrency concerns?
   If not, a new boolean[1] would be less overhead

- BufferedReader also keeps track of CR/LF pairs and I've thought it 
would simpler
   not to duplicate the line number counting logic in LineNumberReader.
   If BufferedReader kept track of the line number, it could expose a 
package private field
   to LineNumberReader to make it visible to the get/SetLineNumber 
methods of LineNumberReader.

Regards, Roger

On 9/5/19 11:11 AM, Brian Burkhalter wrote:
> https://bugs.openjdk.java.net/browse/JDK-8230342
> http://cr.openjdk.java.net/~bpb/8230342/webrev.00/
> When the last line is read by readLine() the line number is incremented due to the EOF but not when read() or read(char[]) is used. The specification states it is incremented on line terminators only. The alternative solution would be to change the specification to state that the line number is also incremented on EOF even if there is no line terminator at the end of the stream. That would entail a different implementation change.
> Thanks,
> Brian

More information about the core-libs-dev mailing list