Last active
September 28, 2015 01:42
-
-
Save cho45/bde61333d464919e42b5 to your computer and use it in GitHub Desktop.
Check TypedArray Endianness on Nashorn (JavaScript engine on JVM)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.lowreal; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
public class Main { | |
public static void main(String[] args) throws ScriptException { | |
final ScriptEngineManager scriptEngineManager = new ScriptEngineManager(); | |
final ScriptEngine js = scriptEngineManager.getEngineByName("js"); | |
System.out.println(js); | |
js.eval("var BYTE_ORDER = (function () {\n" + | |
"\tvar buf = new ArrayBuffer(2);\n" + | |
"\tvar i8 = new Uint8Array(buf);\n" + | |
"\ti8[0] = 0xfe;\n" + | |
"\ti8[1] = 0xff;\n" + | |
"\tvar i16 = new Uint16Array(buf);\n" + | |
"\treturn (i16[0] === 0xfeff) ? 'BIG_ENDIAN' : 'LITTLE_ENDIAN';\n" + | |
"})();"); | |
js.eval("print(BYTE_ORDER);"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jdk.nashorn.api.scripting.NashornScriptEngine@587c290d | |
LITTLE_ENDIAN | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment