6900043: Add method to return line.separator property
authormartin
Mon, 07 Dec 2009 16:44:40 -0800
changeset 4355 12d58d6de82f
parent 4354 3a70dde80b3b
child 4356 1f9c2400b8c5
6900043: Add method to return line.separator property Summary: Add System.lineSeparator(), returning getProperty("line.separator") Reviewed-by: darcy
jdk/src/share/classes/java/lang/System.java
jdk/src/share/classes/java/util/Formatter.java
--- a/jdk/src/share/classes/java/lang/System.java	Mon Dec 07 15:32:26 2009 -0800
+++ b/jdk/src/share/classes/java/lang/System.java	Mon Dec 07 16:44:40 2009 -0800
@@ -620,6 +620,20 @@
     }
 
     /**
+     * Returns the system-dependent line separator string.  It always
+     * returns the same value - the initial value of the {@linkplain
+     * #getProperty(String) system property} {@code line.separator}.
+     *
+     * <p>On UNIX systems, it returns {@code "\n"}; on Microsoft
+     * Windows systems it returns {@code "\r\n"}.
+     */
+    public static String lineSeparator() {
+        return lineSeparator;
+    }
+
+    private static String lineSeparator;
+
+    /**
      * Sets the system properties to the <code>Properties</code>
      * argument.
      * <p>
@@ -1104,6 +1118,7 @@
     private static void initializeSystemClass() {
         props = new Properties();
         initProperties(props);
+        lineSeparator = props.getProperty("line.separator");
         sun.misc.Version.init();
 
         // Workaround until DownloadManager initialization is revisited.
@@ -1192,7 +1207,7 @@
     }
 
     /* returns the class of the caller. */
-    static Class getCallerClass() {
+    static Class<?> getCallerClass() {
         // NOTE use of more generic Reflection.getCallerClass()
         return Reflection.getCallerClass(3);
     }
--- a/jdk/src/share/classes/java/util/Formatter.java	Mon Dec 07 15:32:26 2009 -0800
+++ b/jdk/src/share/classes/java/util/Formatter.java	Mon Dec 07 16:44:40 2009 -0800
@@ -2552,9 +2552,6 @@
         private boolean dt = false;
         private char c;
 
-        // cache the line separator
-        private String ls;
-
         private int index(String s) {
             if (s != null) {
                 try {
@@ -2702,9 +2699,7 @@
                 printHashCode(arg);
                 break;
             case Conversion.LINE_SEPARATOR:
-                if (ls == null)
-                    ls = System.getProperty("line.separator");
-                a.append(ls);
+                a.append(System.lineSeparator());
                 break;
             case Conversion.PERCENT_SIGN:
                 a.append('%');