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

LineNumberReader.read(char[] cbuf, int off, int len) does not compress "\r\n"

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      The LineNumberReader's read() and readLine() methods normalize the new-line characters to '\n' before returning the characters read. But the read(char[] cbuf, int off, int len) method does not normalize the new-line characters.

      For example, a file created on windows, with the following text:
      [[
      hello
      dear
      ]]
      ...has 9 visible characters and 2 invisible new-line characters viz. \r\n

      When such a file is read using LineNumberReader's read() or readLine(), the total number of characters read is 10 -- i.e. 9 visible chars + 1 new-line char \n

      But when the same file is read using LineNumberReader's read(char[] cbuf, int off, int len) method the total numer of characters read is 11 -- i.e. 9 visible + 2 new-line chars \r\n

      This behaviour seems to be a bug

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the following program to reproduce:
      [[
      import java.io.*;

      public class LineNumberReaderBug {

          public static void main(String[] args) throws Exception {

              LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(args[0])));

              char[] buffer = new char[2048];
              int charsRead = lnr.read(buffer);
              System.out.println("reading using LineNumberReader.read(char[] cbuf, int off, int len)...");
              System.out.println("Chars read="+charsRead);
              System.out.println("[["+new String(buffer, 0, charsRead)+"]]");

              lnr.close();


              lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(args[0])));

              StringBuffer sb = new StringBuffer();
              int ch = -1;

              while ((ch = lnr.read()) != -1) {
                  sb.append((char)ch);
              }

              System.out.println("\nreading using LineNumberReader.read()...");
              System.out.println("Chars read="+sb.length());
              System.out.println("[["+sb.toString()+"]]");

              lnr.close();

          }

      }
      ]]

      Use the following input file to the above program:
      [[
      hello
      dear
      ]]
      ...make sure the file is created on windows platform so that the new-line is \r\n.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected chars read to return 10, but it returned 11.

      C:\Temp>java -showversion -classpath %CLASSPATH%;. LineNumberReaderBug input.txt
      java version "1.5.0_06"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

      reading using LineNumberReader.read(char[] cbuf, int off, int len)...
      Chars read=11
      [[hello
      dear]]

      reading using LineNumberReader.read()...
      Chars read=10
      [[hello
      dear]]


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Use the following program to reproduce:
      [[
      import java.io.*;

      public class LineNumberReaderBug {

          public static void main(String[] args) throws Exception {

              LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(args[0])));

              char[] buffer = new char[2048];
              int charsRead = lnr.read(buffer);
              System.out.println("reading using LineNumberReader.read(char[] cbuf, int off, int len)...");
              System.out.println("Chars read="+charsRead);
              System.out.println("[["+new String(buffer, 0, charsRead)+"]]");

              lnr.close();


              lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(args[0])));

              StringBuffer sb = new StringBuffer();
              int ch = -1;

              while ((ch = lnr.read()) != -1) {
                  sb.append((char)ch);
              }

              System.out.println("\nreading using LineNumberReader.read()...");
              System.out.println("Chars read="+sb.length());
              System.out.println("[["+sb.toString()+"]]");

              lnr.close();

          }

      }
      ]]

      Use the following input file to the above program:
      [[
      hello
      dear
      ]]
      ...make sure the file is created on windows platform so that the new-line is \r\n.

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

            iris Iris Clark
            mmma Marvin Ma (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: