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

java.io: line.separator property used only for output, not for input

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      solaris_2.5.1

      Description

      Using a line separator other than \n seems to cause inconsistent handling
      of line termination between DataInputStream.readLine and PrintStream.println.

      For example, the following code prints a string to an output stream
      and then reads it back with readLine(). The String which is read back
      equals the original string if the default line separator (\n)
      is used. However, if an alternate line separator is used, the
      original string and the string read back from the output stream
      differ.

      // Println - test of println method
      //

      import java.io.*;

      class Println
      {
         public static void main(String args[]) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            DataInputStream dis;
            String s;
            String ol = new String("Hello There !");

            try {
      System.out.println(ol);
      ps.println(ol);
            
      dis = new DataInputStream(
      new ByteArrayInputStream(baos.toByteArray()));
      s = dis.readLine();
      if (s == null)
      System.out.println("First readLine failed");
      else if (!s.equals(ol))
      System.out.println("\"" + s + "\" is not equal to " +
      "\"" + ol + "\"");
      else {
      s = dis.readLine();
      if (s != null) {
      char sb[] = s.toCharArray();
      System.out.print("Expected null but got string of length "
      + sb.length + ":");
      for (int i = 0; i < sb.length; i++) {
      if (i > 0)
      System.out.print(", ");
      System.out.print("\'" + ((int) sb[i]) + "\'");
      }
      System.out.println("");
      }
      else
      System.out.println("All is well !");
      }
            } catch (IOException e) {
      System.out.println(e);
            }
         }
      }


      If the test is run on Solaris with the following command line:

      java -Dline.separator=+ Println

      You get the following:

      "Hello There !+" is not equal to "Hello There !"+

        Attachments

          Activity

            People

            • Assignee:
              mr Mark Reinhold
              Reporter:
              duke J. Duke (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: