jaroslav@1260: /** jaroslav@1260: * Back 2 Browser Bytecode Translator jaroslav@1260: * Copyright (C) 2012 Jaroslav Tulach jaroslav@1260: * jaroslav@1260: * This program is free software: you can redistribute it and/or modify jaroslav@1260: * it under the terms of the GNU General Public License as published by jaroslav@1260: * the Free Software Foundation, version 2 of the License. jaroslav@1260: * jaroslav@1260: * This program is distributed in the hope that it will be useful, jaroslav@1260: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1260: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1260: * GNU General Public License for more details. jaroslav@1260: * jaroslav@1260: * You should have received a copy of the GNU General Public License jaroslav@1260: * along with this program. Look for COPYING file in the top folder. jaroslav@1260: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1260: */ jaroslav@1260: package java.lang; jaroslav@1260: jaroslav@1416: import java.io.BufferedOutputStream; jaroslav@1322: import java.io.ByteArrayInputStream; jaroslav@1416: import java.io.IOException; jaroslav@1322: import java.io.InputStream; jaroslav@1416: import java.io.OutputStream; jaroslav@1322: import java.io.PrintStream; jaroslav@1410: import java.util.Properties; jaroslav@1279: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@1279: jaroslav@1260: /** Poor man's re-implementation of most important System methods. jaroslav@1260: * jaroslav@1260: * @author Jaroslav Tulach jaroslav@1260: */ jaroslav@1260: public class System { jaroslav@1260: private System() { jaroslav@1260: } jaroslav@1260: jaroslav@1260: public static void arraycopy(Object value, int srcBegin, Object dst, int dstBegin, int count) { jaroslav@1260: org.apidesign.bck2brwsr.emul.lang.System.arraycopy(value, srcBegin, dst, dstBegin, count); jaroslav@1260: } jaroslav@1260: jaroslav@1260: public static long currentTimeMillis() { jaroslav@1260: return org.apidesign.bck2brwsr.emul.lang.System.currentTimeMillis(); jaroslav@1260: } jaroslav@1260: toni@1289: public static long nanoTime() { toni@1289: return org.apidesign.bck2brwsr.emul.lang.System.nanoTime(); toni@1290: toni@1289: } toni@1289: jaroslav@1260: public static int identityHashCode(Object obj) { jaroslav@1260: return obj.defaultHashCode(); jaroslav@1260: } jaroslav@1260: jaroslav@1260: public static String getProperty(String name) { jaroslav@1328: if ("os.name".equals(name)) { jaroslav@1328: return userAgent(); jaroslav@1328: } jaroslav@1260: return null; jaroslav@1260: } jaroslav@1260: jaroslav@1362: @JavaScriptBody(args = {}, body = "return (typeof navigator !== 'undefined') ? navigator.userAgent : 'unknown';") jaroslav@1328: private static native String userAgent(); jaroslav@1328: jaroslav@1260: public static String getProperty(String key, String def) { jaroslav@1260: return def; jaroslav@1260: } jaroslav@1260: jaroslav@1410: public static Properties getProperties() { jaroslav@1410: throw new SecurityException(); jaroslav@1410: } jaroslav@1410: jaroslav@1410: public static void setProperties(Properties p) { jaroslav@1410: throw new SecurityException(); jaroslav@1410: } jaroslav@1410: jaroslav@1260: /** jaroslav@1260: * Returns the system-dependent line separator string. It always jaroslav@1260: * returns the same value - the initial value of the {@linkplain jaroslav@1260: * #getProperty(String) system property} {@code line.separator}. jaroslav@1260: * jaroslav@1260: *

On UNIX systems, it returns {@code "\n"}; on Microsoft jaroslav@1260: * Windows systems it returns {@code "\r\n"}. jaroslav@1260: */ jaroslav@1260: public static String lineSeparator() { jaroslav@1260: return "\n"; jaroslav@1260: } jaroslav@1279: jaroslav@1279: @JavaScriptBody(args = { "exitCode" }, body = "window.close();") jaroslav@1279: public static void exit(int exitCode) { jaroslav@1279: } jaroslav@1322: jaroslav@1322: public final static InputStream in; jaroslav@1322: jaroslav@1322: public final static PrintStream out; jaroslav@1322: jaroslav@1322: public final static PrintStream err; jaroslav@1322: jaroslav@1416: public static void setOut(PrintStream out) { jaroslav@1416: throw new SecurityException(); jaroslav@1416: } jaroslav@1416: jaroslav@1416: public static void setIn(InputStream in) { jaroslav@1416: throw new SecurityException(); jaroslav@1416: } jaroslav@1416: jaroslav@1416: public static void setErr(PrintStream err) { jaroslav@1416: throw new SecurityException(); jaroslav@1416: } jaroslav@1416: jaroslav@1322: static { jaroslav@1322: in = new ByteArrayInputStream(new byte[0]); jaroslav@1416: out = new PrintStream(new BufferedOutputStream(new SystemStream("log"))); jaroslav@1416: err = new PrintStream(new BufferedOutputStream(new SystemStream("warn"))); jaroslav@1322: } jaroslav@1416: jaroslav@1416: private static final class SystemStream extends OutputStream { jaroslav@1416: private final String method; jaroslav@1416: jaroslav@1416: public SystemStream(String method) { jaroslav@1416: this.method = method; jaroslav@1416: } jaroslav@1416: jaroslav@1416: @Override jaroslav@1416: public void write(byte b[], int off, int len) throws IOException { jaroslav@1416: write(method, new String(b, off, len, "UTF-8")); jaroslav@1416: } jaroslav@1416: jaroslav@1416: @JavaScriptBody(args = { "method", "b" }, body = "if (typeof console !== 'undefined') console[method](b.toString());") jaroslav@1416: private static native void write(String method, String b); jaroslav@1416: jaroslav@1416: @Override jaroslav@1416: public void write(int b) throws IOException { jaroslav@1416: write(new byte[] { (byte)b }); jaroslav@1416: } jaroslav@1416: } // end of SystemStream jaroslav@1260: }