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

Please add LINE_SEPARATOR constant into System or some other class

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P5
    • Resolution: Duplicate
    • Affects Version/s: 6
    • Fix Version/s: None
    • Component/s: core-libs
    • Subcomponent:
    • CPU:
      x86
    • OS:
      windows_xp

      Description

      A DESCRIPTION OF THE REQUEST :
      There is well-known system property: System.getProperty("line.separator"). It is often necessary while creating different text files, logging, printing debug messages by System.out.println, etc. The simplest example:

      StringBuilder sb = new StringBuilder();
      for (int i=0; i<100000; i++) {
         ...some complex calculations....
         sb.append("Some debug information: ..." + System.getProperty("line.separator"));
         // It is too slow to call here logging/printing methods
      }
      myLogger.fine(sb.toString()); // or System.out.println(sb);

      I think it would be good to add the corresponding constant to some standard Java class, as String or System. Why do you offer File.pathSeparator constant but do not offer any constant for line separator string?

      Of course, we can use the expression "System.getProperty("line.separator")". But here are, at least, two problems.

      1) This expression is not syntactically protected against a mistake while typing. If I'll write "line.separato", the compiler will not warn and this bug can be not detected for a long time, if this string is used for debug messages only.

      2) According Java documentation, System.getProperty method can throw SecurityException. It is theoretically possible that this exception will be thrown while the call "System.getProperty("line.separator")": your documentation allows this situation. To avoid this problem, I'm forced to create the code alike the following one in my libraries:

          public static final String LINE_SEPARATOR = getLineSeparator();
          private static String getLineSeparator() {
              java.io.ByteArrayOutputStream byteArrayOutputStream = new java.io.ByteArrayOutputStream(8);
              java.io.PrintStream printStream = new java.io.PrintStream(byteArrayOutputStream);
              printStream.println();
              printStream.flush();
              printStream.close();
              byte[] bytes = byteArrayOutputStream.toByteArray();
              char[] chars = new char[bytes.length];
              for (int k = 0; k < bytes.length; k++)
                  chars[k] = (char)(bytes[k] & 0xFF);
              return String.valueOf(chars);
          }

      I include such a code for almost all my libraries that require non-trivial debug messaging.

      I offer to include the simple and safe constant LINE_SEPARATOR into System, String or another standard public class.


      JUSTIFICATION :
        Too complex code allowing to safely create a multi-line text with standard system line separator between lines.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I would like to be able to write:
          String s = "My first line" + System.LINE_SEPARATOR + "My second write";
          System.out.println(s);
      Another possible solution is to add the method, for example into String class, that replaces all \n characters with standard system line separator. For example:
          String s = "My first line\nMy second write";
          System.out.println(s.correctLineSeparators());
      It may be better solution because such method can preserve all existing system-oriented line separators (as \r\n in Windows) but correct only single \n characters.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ndcosta Nelson Dcosta
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Imported:
                  Indexed: