RFR: 8349503: Consolidate multi-byte access into ByteArray
Per Minborg
pminborg at openjdk.org
Thu Feb 6 14:33:12 UTC 2025
On Wed, 5 Feb 2025 23:41:19 GMT, Chen Liang <liach at openjdk.org> wrote:
> `MethodHandles.byteArrayViewVarHandle` exposes checked multi-byte access to byte arrays via VarHandle. This larger access speeds up many operations, yet it cannot be used in early bootstrap, and as a result, people tend to use `Unsafe` which can threaten memory safety of the Java Platform.
>
> To promote the safe use of multi-byte access, I propose to move the checked implementations from VarHandle to ByteArray to allow earlier use and reduce maintenance costs. In addition, ByteArrayLittleEndian is consolidated, and now the access methods are distinguished by BO (byte order) / BE (big endian) / LE (little endian) suffixes to indicate their access features.
src/java.base/share/classes/jdk/internal/util/ByteArray.java line 131:
> 129: // BE methods
> 130:
> 131: public static char getCharBE(byte[] array, int offset) {
I think it is worth making the effort to document these methods as they are used across the JDK. We could just take the docs from the old class and modify it slightly.
src/java.desktop/share/classes/javax/imageio/stream/ImageInputStreamImpl.java line 245:
> 243: throw new EOFException();
> 244: }
> 245: return (byteOrder == ByteOrder.BIG_ENDIAN)
This could just be `ByteArray.getShortBO(byteBuff, 0, byteOrder == ByteOrder.BIG_ENDIAN)`. Same for the others.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944825706
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944828604
More information about the core-libs-dev
mailing list