@Test public void shouldDetectNonPowerOfTwoAlignment() { assertThrows(IllegalArgumentException.class, () -> BufferUtil.allocateDirectAligned(1, 3)); }
public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); }
BufferUtil { public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); } }
BufferUtil { public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); } private  BufferUtil(); }
BufferUtil { public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); } private  BufferUtil(); static void boundsCheck(final byte[] buffer, final long index, final int length); static void boundsCheck(final ByteBuffer buffer, final long index, final int length); static long address(final ByteBuffer buffer); static byte[] array(final ByteBuffer buffer); static int arrayOffset(final ByteBuffer buffer); static ByteBuffer allocateDirectAligned(final int capacity, final int alignment); static void free(final DirectBuffer buffer); static void free(final ByteBuffer buffer); }
BufferUtil { public static ByteBuffer allocateDirectAligned(final int capacity, final int alignment) { if (!isPowerOfTwo(alignment)) { throw new IllegalArgumentException("Must be a power of 2: alignment=" + alignment); } final ByteBuffer buffer = ByteBuffer.allocateDirect(capacity + alignment); final long address = address(buffer); final int remainder = (int)(address & (alignment - 1)); final int offset = alignment - remainder; buffer.limit(capacity + offset); buffer.position(offset); return buffer.slice(); } private  BufferUtil(); static void boundsCheck(final byte[] buffer, final long index, final int length); static void boundsCheck(final ByteBuffer buffer, final long index, final int length); static long address(final ByteBuffer buffer); static byte[] array(final ByteBuffer buffer); static int arrayOffset(final ByteBuffer buffer); static ByteBuffer allocateDirectAligned(final int capacity, final int alignment); static void free(final DirectBuffer buffer); static void free(final ByteBuffer buffer); static final byte[] NULL_BYTES; static final ByteOrder NATIVE_BYTE_ORDER; static final long ARRAY_BASE_OFFSET; static final long BYTE_BUFFER_HB_FIELD_OFFSET; static final long BYTE_BUFFER_OFFSET_FIELD_OFFSET; static final long BYTE_BUFFER_ADDRESS_FIELD_OFFSET; }