Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8241020

LineNumberReader.getLineNumber() behavior is inconsistent with respect to EOF

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 16
    • core-libs
    • None
    • behavioral
    • medium
    • This might cause a problem for code which does not expect the line number to be incremented by an EOS, i.e., which expects incrementing only for `\r`, `\n`, or `\r\n`.
    • Java API
    • SE

      Summary

      Modify LineNumberReader to consider end-of-stream to be a line terminator.

      Problem

      LineNumberReader presently does not consider end-of-stream to be a line terminator, i.e, one of \r, \n, or \r\n. Thus for example for a stream such as

      line 1\n
      line 2\n
      line 3

      which ends without a line terminator, only two lines would be reported.

      Solution

      Modify LineNumberReader to consider end-of-stream to be a line terminator.

      Specification

      Modify the specification of LineNumberReader as follows.

      Class specification

      By default, line numbering begins at 0. This number increments at every line terminator as the data is read, and at the end of the stream if the last character in the stream is not a line terminator. This number can be changed with a call to setLineNumber(int). Note however, that setLineNumber(int) does not actually change the current position in the stream; it only changes the value that will be returned by getLineNumber().

      A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed, or any of the previous terminators followed by end of stream, or end of stream not preceded by another terminator.

      read() specification

      Read a single character. Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

      read(char[],int,int) specification

      Read characters into a portion of an array. Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

      readLine() specification

      Read a line of text. Line terminators are compressed into single newline ('\n') characters. The current line number is incremented whenever a line terminator is read, or when the end of the stream is reached and the last character in the stream is not a line terminator.

        1. webrev-8235792.zip
          156 kB
        2. webrev-8235792.zip
          157 kB
        3. webrev-8235792.zip
          158 kB

            bpb Brian Burkhalter
            bpb Brian Burkhalter
            Alan Bateman, Roger Riggs
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: