Discussion:
casting typed array to java byte[] is it possible?
Paulo Lopes
2018-10-11 18:21:31 UTC
Permalink
Hi,

I'm trying to handle a case where a Uint8Array is being passed to a
method, that has the signature:

String encode(byte[]);

Sadly nashorn fails with:

java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.objects.NativeUint8Array to [B

And trying to help the cast with:

javaObj.encode(Java.to(arr, 'byte[]'));

Does not help either. The documentation on typed arrays is quite scarce
so I cannot see how to handle this, does anyone have a clue?

Thanks!
Paulo
Hannes Wallnöfer
2018-10-12 08:10:27 UTC
Permalink
Hi Paulo,

Java.to() would be the way to go, but as you found out it does not support typed arrays.

What works is to convert the typed array to an ordinary JS array and convert to byte[] from there:

Java.to(Array.prototype.slice.call(arr), 'byte[]‘);

That’s obviously not very elegant nor efficient, but it’s the only workaround I can think of.

Hannes
Post by Paulo Lopes
Hi,
I'm trying to handle a case where a Uint8Array is being passed to a
String encode(byte[]);
java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.objects.NativeUint8Array to [B
javaObj.encode(Java.to(arr, 'byte[]'));
Does not help either. The documentation on typed arrays is quite scarce
so I cannot see how to handle this, does anyone have a clue?
Thanks!
Paulo
Paulo Lopes
2018-10-12 11:34:56 UTC
Permalink
Hi Hannes,

Thanks for the tip, knowing that the convention isn't possible as is, are there any alternatives to get the JVM buffer that backs a typed array?

I wanted to perform some work on a given buffer and then pass it to the JVM for further processing. Having to copy the buffer byte by byte will introduce a penalty that kind of defeats the whole purpose of using typed arrays to start with.


Paulo Lopes
Principal Software Engineer


  Original Message  
From: ***@oracle.com
Sent: October 12, 2018 10:10 AM
To: ***@redhat.com
Cc: nashorn-***@openjdk.java.net
Subject: Re: casting typed array to java byte[] is it possible?

Hi Paulo,

Java.to() would be the way to go, but as you found out it does not support typed arrays.

What works is to convert the typed array to an ordinary JS array and convert to byte[] from there:

  Java.to(Array.prototype.slice.call(arr), 'byte[]‘);

That’s obviously not very elegant nor efficient, but it’s the only workaround I can think of.

Hannes
Post by Paulo Lopes
Hi,
I'm trying to handle a case where a Uint8Array is being passed to a
String encode(byte[]);
java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.objects.NativeUint8Array to [B
javaObj.encode(Java.to(arr, 'byte[]'));
Does not help either. The documentation on typed arrays is quite scarce
so I cannot see how to handle this, does anyone have a clue?
T
Sundararajan Athijegannathan
2018-10-12 12:04:45 UTC
Permalink
Hi Paulo Lopes,

ArrayBuffer can be created using a nio ByteBuffer.

// you could create a ByteBuffer in Java & pass
var bb = java.nio.ByteBuffer.allocateDirect(100)

// create JS ArrayBuffer backed by Java ByteBuffer instance
var ab = new ArrayBuffer(bb)

var ia = new Int8Array(ab)
ia[0] = 10
print(bb.get(0))

Hope this helps,
-Sundar
Post by Paulo Lopes
Hi Hannes,
Thanks for the tip, knowing that the convention isn't possible as is, are there any alternatives to get the JVM buffer that backs a typed array?
I wanted to perform some work on a given buffer and then pass it to the JVM for further processing. Having to copy the buffer byte by byte will introduce a penalty that kind of defeats the whole purpose of using typed arrays to start with.
Paulo Lopes
Principal Software Engineer
Original Message
Sent: October 12, 2018 10:10 AM
Subject: Re: casting typed array to java byte[] is it possible?
Hi Paulo,
Java.to() would be the way to go, but as you found out it does not support typed arrays.
Java.to(Array.prototype.slice.call(arr), 'byte[]‘);
That’s obviously not very elegant nor efficient, but it’s the only workaround I can think of.
Hannes
Post by Paulo Lopes
Hi,
I'm trying to handle a case where a Uint8Array is being passed to a
String encode(byte[]);
java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.objects.NativeUint8Array to [B
javaObj.encode(Java.to(arr, 'byte[]'));
Does not help either. The documentation on typed arrays is quite scarce
so I cannot see how to handle this, does anyone have a clue?
Thanks!
Paulo
Paulo Lopes
2018-10-12 13:34:59 UTC
Permalink
Hi Sundar,

Yes indeed that helps and it really gets close to my needs, I notice
however, if I do that, then:

var ia = new Int8Array(ab)
ia.length

will never have a valid value, it will be zero regardless of the
underlying ArrayBuffer, since the property is read only I cannot assign
it myself (since I know the size at construction time) either.

Trying to specify the size:

var bb = java.nio.ByteBuffer.allocateDirect(12)
var ab = new ArrayBuffer(bb)
var ia = new Int8Array(ab, 0, 12)

it's all 8 bit ints so (bytes) fails with:

java.lang.RuntimeException: byteOffset + byteLength out of range,
byteOffset=0, elementLength=12, bytesPerElement=1

Is this expected? Or can I specify the length somewhere?

Also ab.byteLength prints 0 instead of 12...
Post by Sundararajan Athijegannathan
Hi Paulo Lopes,
ArrayBuffer can be created using a nio ByteBuffer.
// you could create a ByteBuffer in Java & pass
var bb = java.nio.ByteBuffer.allocateDirect(100)
// create JS ArrayBuffer backed by Java ByteBuffer instance
var ab = new ArrayBuffer(bb)
var ia = new Int8Array(ab)
ia[0] = 10
print(bb.get(0))
Hope this helps,
-Sundar
Post by Paulo Lopes
Hi Hannes,
Thanks for the tip, knowing that the convention isn't possible as
is, are there any alternatives to get the JVM buffer that backs a
typed array?
I wanted to perform some work on a given buffer and then pass it to
the JVM for further processing. Having to copy the buffer byte by
byte will introduce a penalty that kind of defeats the whole
purpose of using typed arrays to start with.
Paulo Lopes
Principal Software Engineer
Original Message
Sent: October 12, 2018 10:10 AM
Subject: Re: casting typed array to java byte[] is it possible?
Hi Paulo,
Java.to() would be the way to go, but as you found out it does not
support typed arrays.
What works is to convert the typed array to an ordinary JS array
Java.to(Array.prototype.slice.call(arr), 'byte[]‘);
That’s obviously not very elegant nor efficient, but it’s the only
workaround I can think of.
Hannes
Post by Paulo Lopes
Hi,
I'm trying to handle a case where a Uint8Array is being passed to a
String encode(byte[]);
java.lang.ClassCastException: Cannot cast
jdk.nashorn.internal.objects.NativeUint8Array to [B
javaObj.encode(Java.to(arr, 'byte[]'));
Does not help either. The documentation on typed arrays is quite scarce
so I cannot see how to handle this, does anyone have a clue?
Thanks!
Paulo
Paulo Lopes
2018-10-12 13:59:17 UTC
Permalink
Interesting enough, with Java >=9 the length is reported correctly, so
in the case of:

var bb = java.nio.ByteBuffer.allocateDirect(12)
var ab = new ArrayBuffer(bb)
var ia = new Int8Array(ab)

ia.length // prints 12
ab.byteLength // prints 12

This fails now on JDK8 and Graal.js (is Graal.js supposed to allow
this? (setting a ByteBuffer as the underlying buffer of a js
ArrayBuffer?)

I can imagine that for embedded applications this could make sense...
Sundararajan Athijegannathan
2018-10-15 04:40:47 UTC
Permalink
Hi,

Yes, I reproduced the issue with jdk8 (haven't tried graal.js though).
Please file bug(s).

-Sundar
Interesting enough, with Java>=9 the length is reported correctly, so
var bb = java.nio.ByteBuffer.allocateDirect(12)
var ab = new ArrayBuffer(bb)
var ia = new Int8Array(ab)
ia.length // prints 12
ab.byteLength // prints 12
This fails now on JDK8 and Graal.js (is Graal.js supposed to allow
this? (setting a ByteBuffer as the underlying buffer of a js
ArrayBuffer?)
I can imagine that for embedded applications this could make sense...
Loading...