Compartilhar via


MethodHandles.ByteBufferViewVarHandle(Class, ByteOrder) Method

Definition

Produces a VarHandle giving access to elements of a ByteBuffer viewed as if it were an array of elements of a different primitive component type to that of byte, such as int[] or long[].

[Android.Runtime.Register("byteBufferViewVarHandle", "(Ljava/lang/Class;Ljava/nio/ByteOrder;)Ljava/lang/invoke/VarHandle;", "", ApiSince=33)]
public static Java.Lang.Invoke.VarHandle? ByteBufferViewVarHandle (Java.Lang.Class? viewArrayClass, Java.Nio.ByteOrder? byteOrder);
[<Android.Runtime.Register("byteBufferViewVarHandle", "(Ljava/lang/Class;Ljava/nio/ByteOrder;)Ljava/lang/invoke/VarHandle;", "", ApiSince=33)>]
static member ByteBufferViewVarHandle : Java.Lang.Class * Java.Nio.ByteOrder -> Java.Lang.Invoke.VarHandle

Parameters

viewArrayClass
Class

the view array class, with a component type of type T

byteOrder
ByteOrder

the endianness of the view array elements, as stored in the underlying ByteBuffer (Note this overrides the endianness of a ByteBuffer)

Returns

a VarHandle giving access to elements of a ByteBuffer viewed as if elements corresponding to the components type of the view array class

Attributes

Remarks

Produces a VarHandle giving access to elements of a ByteBuffer viewed as if it were an array of elements of a different primitive component type to that of byte, such as int[] or long[]. The VarHandle's variable type is the component type of viewArrayClass and the list of coordinate types is (ByteBuffer, int), where the int coordinate type corresponds to an argument that is an index into a byte[] array. The returned VarHandle accesses bytes at an index in a ByteBuffer, composing bytes to or from a value of the component type of viewArrayClass according to the given endianness.

The supported component types (variables types) are short, char, int, long, float and double.

Access will result in a ReadOnlyBufferException for anything other than the read access modes if the ByteBuffer is read-only.

Access of bytes at a given index will result in an IndexOutOfBoundsException if the index is less than 0 or greater than the ByteBuffer limit minus the size (in bytes) of T.

Access of bytes at an index may be aligned or misaligned for T, with respect to the underlying memory address, A say, associated with the ByteBuffer and index. If access is misaligned then access for anything other than the get and set access modes will result in an IllegalStateException. In such cases atomic access is only guaranteed with respect to the largest power of two that divides the GCD of A and the size (in bytes) of T. If access is aligned then following access modes are supported and are guaranteed to support atomic access: <ul> <li>read write access modes for all T, with the exception of access modes get and set for long and double on 32-bit platforms. <li>atomic update access modes for int, long, float or double. (Future major platform releases of the JDK may support additional types for certain currently unsupported access modes.) <li>numeric atomic update access modes for int and long. (Future major platform releases of the JDK may support additional numeric types for certain currently unsupported access modes.) <li>bitwise atomic update access modes for int and long. (Future major platform releases of the JDK may support additional numeric types for certain currently unsupported access modes.) </ul>

Misaligned access, and therefore atomicity guarantees, may be determined for a ByteBuffer, bb (direct or otherwise), an index, T and it's corresponding boxed type, T_BOX, as follows:

{@code
            int sizeOfT = T_BOX.BYTES;  // size in bytes of T
            ByteBuffer bb = ...
            int misalignedAtIndex = bb.alignmentOffset(index, sizeOfT);
            boolean isMisaligned = misalignedAtIndex != 0;
            }

If the variable type is float or double then atomic update access modes compare values using their bitwise representation (see Float#floatToRawIntBits and Double#doubleToRawLongBits, respectively).

Added in 9.

Java documentation for java.lang.invoke.MethodHandles.byteBufferViewVarHandle(java.lang.Class<?>, java.nio.ByteOrder).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to