rt/emul/compact/src/main/java/java/lang/System.java
changeset 1416 a0a31f8f7dc1
parent 1410 7a021349afec
child 1439 6c04b26f9a9a
child 1483 cecf27be3802
     1.1 --- a/rt/emul/compact/src/main/java/java/lang/System.java	Thu Nov 07 09:44:51 2013 +0100
     1.2 +++ b/rt/emul/compact/src/main/java/java/lang/System.java	Tue Dec 10 17:14:15 2013 +0100
     1.3 @@ -17,9 +17,11 @@
     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.ByteArrayOutputStream;
    1.10 +import java.io.IOException;
    1.11  import java.io.InputStream;
    1.12 +import java.io.OutputStream;
    1.13  import java.io.PrintStream;
    1.14  import java.util.Properties;
    1.15  import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.16 @@ -88,9 +90,42 @@
    1.17  
    1.18      public final static PrintStream err;
    1.19      
    1.20 +    public static void setOut(PrintStream out) {
    1.21 +        throw new SecurityException();
    1.22 +    }
    1.23 +
    1.24 +    public static void setIn(InputStream in) {
    1.25 +        throw new SecurityException();
    1.26 +    }
    1.27 +
    1.28 +    public static void setErr(PrintStream err) {
    1.29 +        throw new SecurityException();
    1.30 +    }
    1.31 +    
    1.32      static {
    1.33          in = new ByteArrayInputStream(new byte[0]);
    1.34 -        out = err = new PrintStream(new ByteArrayOutputStream());
    1.35 +        out = new PrintStream(new BufferedOutputStream(new SystemStream("log")));
    1.36 +        err = new PrintStream(new BufferedOutputStream(new SystemStream("warn")));
    1.37      }
    1.38 -    
    1.39 +
    1.40 +    private static final class SystemStream extends OutputStream {
    1.41 +        private final String method;
    1.42 +
    1.43 +        public SystemStream(String method) {
    1.44 +            this.method = method;
    1.45 +        }
    1.46 +
    1.47 +        @Override
    1.48 +        public void write(byte b[], int off, int len) throws IOException {
    1.49 +            write(method, new String(b, off, len, "UTF-8"));
    1.50 +        }
    1.51 +
    1.52 +        @JavaScriptBody(args = { "method", "b" }, body = "if (typeof console !== 'undefined') console[method](b.toString());")
    1.53 +        private static native void write(String method, String b);
    1.54 +
    1.55 +        @Override
    1.56 +        public void write(int b) throws IOException {
    1.57 +            write(new byte[] { (byte)b });
    1.58 +        }
    1.59 +    } // end of SystemStream
    1.60  }