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

LineNumberReader.getLineNumber() returns inconsistent results after EOF

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b15
    • CPU:
      x86_64
    • OS:
      windows_10

      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


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              bpb Brian Burkhalter
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: