Adding constant for line.separator and friends
Martin Buchholz
martinrb at google.com
Tue Nov 10 00:34:30 UTC 2009
(In response to Joe, who once asked me for little things to add
to the core libraries)
Lots of classes need to use System.getProperty("line.separator").
Many don't do it right because you need to use
a doPrivileged block whenever you read a system property.
Yet it is no secret - you can divine the line separator
even if you have no trust with the security manager.
Here's a strawman proposal:
http://cr.openjdk.java.net/~martin/webrevs/openjdk7/line.separator/
diff --git a/src/share/classes/java/lang/System.java
b/src/share/classes/java/lang/System.java
--- a/src/share/classes/java/lang/System.java
+++ b/src/share/classes/java/lang/System.java
@@ -620,6 +620,32 @@
}
/**
+ * Defines some standard system properties as constant strings.
+ */
+ public static class StandardProperties {
+ public final static String FILE_SEPARATOR;
+ public final static String PATH_SEPARATOR;
+ public final static String LINE_SEPARATOR;
+
+ static {
+ String[] props =
+ AccessController.doPrivileged
+ (new PrivilegedAction<String[]>() {
+ public String[] run() {
+ return new String[] {
+ getProperty("file.separator"),
+ getProperty("path.separator"),
+ getProperty("line.separator")
+ };
+ }
+ });
+ FILE_SEPARATOR = props[0];
+ PATH_SEPARATOR = props[1];
+ LINE_SEPARATOR = props[2];
+ }
+ }
+
+ /**
* Sets the system properties to the <code>Properties</code>
* argument.
* <p>
More information about the core-libs-dev
mailing list