Package com.complexible.common.nio

# Class ByteBuffers

java.lang.Object
com.complexible.common.nio.ByteBuffers

public class ByteBuffers extends Object

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final boolean`
  `ADDRESS_AVAILABLE`
  This flag indicates whether `getAddress(ByteBuffer)` utility is available.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ByteBuffers()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static ByteBuffer`
  `allocate(int capacity, ByteOrder order)`
  The replacement for `ByteBuffer.allocate(int)` that requires to specify the byte order.
  `static ByteBuffer`
  `allocateDirect(int capacity, ByteOrder order)`
  The replacement for `ByteBuffer.allocateDirect(int)` that requires to specify the byte order.
  `static ByteBuffer`
  `asReadOnlyBuffer(ByteBuffer source)`
  This method replaces `ByteBuffer.asReadOnlyBuffer()`, with the difference that it preserves the source buffer's order in the read-only view.
  `static ByteBuffer`
  `duplicate(ByteBuffer toDuplicate)`
  This method replaces `ByteBuffer.duplicate()`, with the difference that it preserves the source buffer's order in the duplicate.
  `static void`
  `freeDirectByteBuffer(ByteBuffer buffer)`
  Invoke the cleaner on the ByteBuffer Added in JDK 9
  `static long`
  `getAddress(ByteBuffer b)`
  Returns the actual native address the given direct ByteBuffer.
  `static ByteBuffer`
  `makeAddressAlignedSlice(ByteBuffer directByteBuffer, int capacity, int alignment)`
  Returns a slice of the given direct ByteBuffer, which starting native address is a multiple of the given alignment.
  `static MappedByteBuffer`
  `map(FileChannel channel, FileChannel.MapMode mode, long position, long size, ByteOrder order)`
  The replacement for `FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)` that requires to specify the byte order.
  `static ByteBuffer`
  `slice(ByteBuffer toSlice)`
  This method replaces `ByteBuffer.slice()`, with the difference that it preserves the source buffer's order in the slice.
  `static ByteBuffer`
  `wrap(byte[] array, int offset, int length, ByteOrder order)`
  The replacement for `ByteBuffer.wrap(byte[], int, int)` that requires to specify the byte order.
  `static ByteBuffer`
  `wrap(byte[] array, ByteOrder order)`
  The replacement for `ByteBuffer.wrap(byte[], int, int)` that requires to specify the byte order.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### ADDRESS_AVAILABLE

    public static final boolean ADDRESS_AVAILABLE
    This flag indicates whether `getAddress(ByteBuffer)` utility is available. If it's not (this flag value is false), the method throws `UnsupportedOperationException`.

- ## Constructor Details

  - ### ByteBuffers

    public ByteBuffers()

- ## Method Details

  - ### allocate

    public static ByteBuffer allocate(int capacity, ByteOrder order)
    The replacement for `ByteBuffer.allocate(int)` that requires to specify the byte order.

  - ### wrap

    public static ByteBuffer wrap(byte[] array, ByteOrder order)
    The replacement for `ByteBuffer.wrap(byte[], int, int)` that requires to specify the byte order.

  - ### wrap

    public static ByteBuffer wrap(byte[] array, int offset, int length, ByteOrder order)
    The replacement for `ByteBuffer.wrap(byte[], int, int)` that requires to specify the byte order.

  - ### allocateDirect

    public static ByteBuffer allocateDirect(int capacity, ByteOrder order)
    The replacement for `ByteBuffer.allocateDirect(int)` that requires to specify the byte order.

  - ### map

    public static MappedByteBuffer map(FileChannel channel, FileChannel.MapMode mode, long position, long size, ByteOrder order) throws IOException
    The replacement for `FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)` that requires to specify the byte order.
    Throws:
      `IOException`

  - ### slice

    public static ByteBuffer slice(ByteBuffer toSlice)
    This method replaces `ByteBuffer.slice()`, with the difference that it preserves the source buffer's order in the slice. `ByteBuffer.slice()` returns a buffer that is always BIG_ENDIAN, that is error-prone.

  - ### duplicate

    public static ByteBuffer duplicate(ByteBuffer toDuplicate)
    This method replaces `ByteBuffer.duplicate()`, with the difference that it preserves the source buffer's order in the duplicate. `ByteBuffer.duplicate()` returns a buffer that is always BIG_ENDIAN, that is error-prone.

  - ### asReadOnlyBuffer

    public static ByteBuffer asReadOnlyBuffer(ByteBuffer source)
    This method replaces `ByteBuffer.asReadOnlyBuffer()`, with the difference that it preserves the source buffer's order in the read-only view. `ByteBuffer.asReadOnlyBuffer()` returns a buffer that is always BIG_ENDIAN, that is error-prone.

  - ### getAddress

    public static long getAddress(ByteBuffer b)
    Returns the actual native address the given direct ByteBuffer.
    Returns:
      direct memory address
    Throws:
      `IllegalArgumentException` - if the given ByteBuffer is not direct
      `UnsupportedOperationException` - if `ADDRESS_AVAILABLE` == false

  - ### makeAddressAlignedSlice

    public static ByteBuffer makeAddressAlignedSlice(ByteBuffer directByteBuffer, int capacity, int alignment)
    Returns a slice of the given direct ByteBuffer, which starting native address is a multiple of the given alignment.
    Parameters:
      `directByteBuffer` - the buffer to make a slice from
      `capacity` - the capacity of the aligned slice. Must be a multiple of the given alignment
      `alignment` - the desired alignment of the starting native address for the returned slice
    Returns:
      an address-aligned slice
    Throws:
      `IllegalArgumentException` - if the given buffer is not direct, or if the capacity and the starting native address of the given buffer don't allow to create a slice with the specified alignment and capacity

  - ### freeDirectByteBuffer

    public static void freeDirectByteBuffer(ByteBuffer buffer)
    Invoke the cleaner on the ByteBuffer Added in JDK 9
    Parameters:
      `buffer` - direct buffer where we are going to invoke the cleaner on