Java primitive arrays with non-native ByteOrder?
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Tue Aug 24 10:06:44 UTC 2021
On 14/08/2021 05:38, leerho wrote:
> All,
>
> Can anyone think of cases where it would be desirable to load a Java
> primitive array with non-native byte order values? Assume that all reading
> and writing to such an array by the user would always use the non-native
> byte order.
As for the use case, the main use case we're after with byte order is
serialization. A lot of message protocols use, for better or worse,
BigEndian as the default representation. The ByteBuffer API also uses
that as its default polarity.
So, if you store an array of doubles in a mapped file, with big endian
order, and later you want to read it, you need "swappy" primitives.
>
> The integral types should always be valid, but doubles and floats could
> become illegal IEEE 754 bit layouts if something in the JVM were to examine
> them. Assume that the user would not use any of the built-in static
> methods like isNaN(double), etc.
>
> Would this cause any problems in the JVM?
What you say is true - swapping doubles and floats could lead to
troubles, but this is already possible in the ByteBuffer API (as noted
above) so I don't think this problem is specific to this API?
Maurizio
>
> Has anyone done this?
>
> Cheers,
>
> Lee.
More information about the panama-dev
mailing list