1.1 --- a/rt/emul/compact/src/main/java/java/lang/System.java Wed Sep 18 11:25:14 2013 +0200
1.2 +++ b/rt/emul/compact/src/main/java/java/lang/System.java Tue Feb 11 13:31:42 2014 +0100
1.3 @@ -17,6 +17,13 @@
1.4 */
1.5 package java.lang;
1.6
1.7 +import java.io.BufferedOutputStream;
1.8 +import java.io.ByteArrayInputStream;
1.9 +import java.io.IOException;
1.10 +import java.io.InputStream;
1.11 +import java.io.OutputStream;
1.12 +import java.io.PrintStream;
1.13 +import java.util.Properties;
1.14 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.15
1.16 /** Poor man's re-implementation of most important System methods.
1.17 @@ -45,13 +52,27 @@
1.18 }
1.19
1.20 public static String getProperty(String name) {
1.21 + if ("os.name".equals(name)) {
1.22 + return userAgent();
1.23 + }
1.24 return null;
1.25 }
1.26
1.27 + @JavaScriptBody(args = {}, body = "return (typeof navigator !== 'undefined') ? navigator.userAgent : 'unknown';")
1.28 + private static native String userAgent();
1.29 +
1.30 public static String getProperty(String key, String def) {
1.31 return def;
1.32 }
1.33
1.34 + public static Properties getProperties() {
1.35 + throw new SecurityException();
1.36 + }
1.37 +
1.38 + public static void setProperties(Properties p) {
1.39 + throw new SecurityException();
1.40 + }
1.41 +
1.42 /**
1.43 * Returns the system-dependent line separator string. It always
1.44 * returns the same value - the initial value of the {@linkplain
1.45 @@ -67,4 +88,49 @@
1.46 @JavaScriptBody(args = { "exitCode" }, body = "window.close();")
1.47 public static void exit(int exitCode) {
1.48 }
1.49 +
1.50 + public final static InputStream in;
1.51 +
1.52 + public final static PrintStream out;
1.53 +
1.54 + public final static PrintStream err;
1.55 +
1.56 + public static void setOut(PrintStream out) {
1.57 + throw new SecurityException();
1.58 + }
1.59 +
1.60 + public static void setIn(InputStream in) {
1.61 + throw new SecurityException();
1.62 + }
1.63 +
1.64 + public static void setErr(PrintStream err) {
1.65 + throw new SecurityException();
1.66 + }
1.67 +
1.68 + static {
1.69 + in = new ByteArrayInputStream(new byte[0]);
1.70 + out = new PrintStream(new BufferedOutputStream(new SystemStream("log")));
1.71 + err = new PrintStream(new BufferedOutputStream(new SystemStream("warn")));
1.72 + }
1.73 +
1.74 + private static final class SystemStream extends OutputStream {
1.75 + private final String method;
1.76 +
1.77 + public SystemStream(String method) {
1.78 + this.method = method;
1.79 + }
1.80 +
1.81 + @Override
1.82 + public void write(byte b[], int off, int len) throws IOException {
1.83 + write(method, new String(b, off, len, "UTF-8"));
1.84 + }
1.85 +
1.86 + @JavaScriptBody(args = { "method", "b" }, body = "if (typeof console !== 'undefined') console[method](b.toString());")
1.87 + private static native void write(String method, String b);
1.88 +
1.89 + @Override
1.90 + public void write(int b) throws IOException {
1.91 + write(new byte[] { (byte)b });
1.92 + }
1.93 + } // end of SystemStream
1.94 }