Skip to content

Instantly share code, notes, and snippets.

@shannah
Created April 8, 2015 16:57
Show Gist options
  • Save shannah/b72c8ae40ce3d72fa954 to your computer and use it in GitHub Desktop.
Save shannah/b72c8ae40ce3d72fa954 to your computer and use it in GitHub Desktop.
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