Package com.complexible.common.nio
Class ByteBuffers
- java.lang.Object
-
- com.complexible.common.nio.ByteBuffers
-
public class ByteBuffers extends java.lang.Object
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
ADDRESS_AVAILABLE
This flag indicates whethergetAddress(ByteBuffer)
utility is available.
-
Constructor Summary
Constructors Constructor Description ByteBuffers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.nio.ByteBuffer
allocate(int capacity, java.nio.ByteOrder order)
The replacement forByteBuffer.allocate(int)
that requires to specify the byte order.static java.nio.ByteBuffer
allocateDirect(int capacity, java.nio.ByteOrder order)
The replacement forByteBuffer.allocateDirect(int)
that requires to specify the byte order.static java.nio.ByteBuffer
asReadOnlyBuffer(java.nio.ByteBuffer source)
This method replacesByteBuffer.asReadOnlyBuffer()
, with the difference that it preserves the source buffer's order in the read-only view.static java.nio.ByteBuffer
duplicate(java.nio.ByteBuffer toDuplicate)
This method replacesByteBuffer.duplicate()
, with the difference that it preserves the source buffer's order in the duplicate.static long
getAddress(java.nio.ByteBuffer b)
Returns the actual native address the given direct ByteBuffer.static java.nio.ByteBuffer
makeAddressAlignedSlice(java.nio.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 java.nio.MappedByteBuffer
map(java.nio.channels.FileChannel channel, java.nio.channels.FileChannel.MapMode mode, long position, long size, java.nio.ByteOrder order)
The replacement forFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
that requires to specify the byte order.static java.nio.ByteBuffer
slice(java.nio.ByteBuffer toSlice)
This method replacesByteBuffer.slice()
, with the difference that it preserves the source buffer's order in the slice.static java.nio.ByteBuffer
wrap(byte[] array, int offset, int length, java.nio.ByteOrder order)
The replacement forByteBuffer.wrap(byte[], int, int)
that requires to specify the byte order.static java.nio.ByteBuffer
wrap(byte[] array, java.nio.ByteOrder order)
The replacement forByteBuffer.wrap(byte[], int, int)
that requires to specify the byte order.
-
-
-
Field Detail
-
ADDRESS_AVAILABLE
public static final boolean ADDRESS_AVAILABLE
This flag indicates whethergetAddress(ByteBuffer)
utility is available. If it's not (this flag value is false), the method throwsUnsupportedOperationException
.
-
-
Method Detail
-
allocate
public static java.nio.ByteBuffer allocate(int capacity, java.nio.ByteOrder order)
The replacement forByteBuffer.allocate(int)
that requires to specify the byte order.
-
wrap
public static java.nio.ByteBuffer wrap(byte[] array, java.nio.ByteOrder order)
The replacement forByteBuffer.wrap(byte[], int, int)
that requires to specify the byte order.
-
wrap
public static java.nio.ByteBuffer wrap(byte[] array, int offset, int length, java.nio.ByteOrder order)
The replacement forByteBuffer.wrap(byte[], int, int)
that requires to specify the byte order.
-
allocateDirect
public static java.nio.ByteBuffer allocateDirect(int capacity, java.nio.ByteOrder order)
The replacement forByteBuffer.allocateDirect(int)
that requires to specify the byte order.
-
map
public static java.nio.MappedByteBuffer map(java.nio.channels.FileChannel channel, java.nio.channels.FileChannel.MapMode mode, long position, long size, java.nio.ByteOrder order) throws java.io.IOException
The replacement forFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
that requires to specify the byte order.- Throws:
java.io.IOException
-
slice
public static java.nio.ByteBuffer slice(java.nio.ByteBuffer toSlice)
This method replacesByteBuffer.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 java.nio.ByteBuffer duplicate(java.nio.ByteBuffer toDuplicate)
This method replacesByteBuffer.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 java.nio.ByteBuffer asReadOnlyBuffer(java.nio.ByteBuffer source)
This method replacesByteBuffer.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(java.nio.ByteBuffer b)
Returns the actual native address the given direct ByteBuffer.- Returns:
- direct memory address
- Throws:
java.lang.IllegalArgumentException
- if the given ByteBuffer is not directjava.lang.UnsupportedOperationException
- ifADDRESS_AVAILABLE
== false
-
makeAddressAlignedSlice
public static java.nio.ByteBuffer makeAddressAlignedSlice(java.nio.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 fromcapacity
- the capacity of the aligned slice. Must be a multiple of the given alignmentalignment
- the desired alignment of the starting native address for the returned slice- Returns:
- an address-aligned slice
- Throws:
java.lang.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
-
-