Skip to content

Instantly share code, notes, and snippets.

@cho45
Last active September 28, 2015 01:42
Show Gist options
  • Save cho45/bde61333d464919e42b5 to your computer and use it in GitHub Desktop.
Save cho45/bde61333d464919e42b5 to your computer and use it in GitHub Desktop.
Check TypedArray Endianness on Nashorn (JavaScript engine on JVM)
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);");
}
}
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