Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public Uint8Array getArrayBuffer(String url){
Window window = (Window)JS.getGlobal();
final XMLHttpRequest req = window.createXMLHttpRequest();
final Object lock = new Object();
final boolean[] complete = new boolean[1];
req.open("get", url, true);
req.setOnReadyStateChange(new ReadyStateChangeHandler() {
@Override
public void stateChanged() {
if ( req.getReadyState() == XMLHttpRequest.DONE ){
new Thread() {
@Override
public void run() {
complete[0]=true;
synchronized(lock){
lock.notifyAll();
}
}
}.start();
}
}
});
req.setResponseType("arraybuffer");
req.send();
while (!complete[0]){
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
if (req.getResponse() == null ){
System.out.println(req.getAllResponseHeaders());
System.out.println(req.getStatusText());
System.out.println("Failed to load resource "+url);
System.out.println("Status code was "+req.getStatus());
return null;
}
return window.createUint8Array(
(ArrayBuffer)req.getResponse()), req.getResponseType());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment