Does anyone know if and how it is possible to pass a byte[] from java to javascript in jxbrowser?
(The goal is to open a binary file in the browser without a file chooser dialog popping up).
When I try to pass the byte[] natively:
Java:
JsFunction function = frame.executeJavaScript("openFileContent");
byte[] blob = new byte[3];
blob[0] = 10;
blob[1] = 20;
blob[2] = 30;
return function.invoke(instance, blob);
Javascript:
openFileContent = function(fileContent) {
console.log(fileContent);
console.log(fileContent[0]);
On the javascript side, I get:
[object [B]
undefined
So it doesn't look like it gets marshalled into anything usable.
My other approach was wrapping the blob into a POJO, like:
public static class JsBlob {
private byte[] buffer;
public JsBlob(byte[] buffer) {
this.buffer = buffer;
}
@JsAccessible
public byte get(int index) {
return buffer[index];
}
@JsAccessible
public byte[] getBuffer() {
return buffer;
}
@JsAccessible
public int getLength() {
return buffer.length;
}
}
Which -while working- was super slow because of all the inter process calls when getting the actual array contents, byte-by-byte.
My current fallback is transferring the base64 encoded blob in a String, which does get properly marshalled as string, then decoding it on the js side, but if possible, I'd like to avoid this unnecessary processing, if possible.