Package com.complexible.common.nio
Class ByteBuffers
java.lang.Object
com.complexible.common.nio.ByteBuffers
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanThis flag indicates whethergetAddress(ByteBuffer)utility is available. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ByteBufferThe replacement forByteBuffer.allocate(int)that requires to specify the byte order.static ByteBufferallocateDirect(int capacity, ByteOrder order) The replacement forByteBuffer.allocateDirect(int)that requires to specify the byte order.static ByteBufferasReadOnlyBuffer(ByteBuffer source) This method replacesByteBuffer.asReadOnlyBuffer(), with the difference that it preserves the source buffer's order in the read-only view.static ByteBufferduplicate(ByteBuffer toDuplicate) This method replacesByteBuffer.duplicate(), with the difference that it preserves the source buffer's order in the duplicate.static voidfreeDirectByteBuffer(ByteBuffer buffer) Invoke the cleaner on the ByteBuffer Added in JDK 9static longReturns the actual native address the given direct ByteBuffer.static ByteBuffermakeAddressAlignedSlice(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 MappedByteBuffermap(FileChannel channel, FileChannel.MapMode mode, long position, long size, ByteOrder order) The replacement forFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)that requires to specify the byte order.static ByteBufferslice(ByteBuffer toSlice) This method replacesByteBuffer.slice(), with the difference that it preserves the source buffer's order in the slice.static ByteBufferThe replacement forByteBuffer.wrap(byte[], int, int)that requires to specify the byte order.static ByteBufferThe replacement forByteBuffer.wrap(byte[], int, int)that requires to specify the byte order.
-
Field Details
-
ADDRESS_AVAILABLE
public static final boolean ADDRESS_AVAILABLEThis flag indicates whethergetAddress(ByteBuffer)utility is available. If it's not (this flag value is false), the method throwsUnsupportedOperationException.
-
-
Constructor Details
-
ByteBuffers
public ByteBuffers()
-
-
Method Details
-
allocate
The replacement forByteBuffer.allocate(int)that requires to specify the byte order. -
wrap
The replacement forByteBuffer.wrap(byte[], int, int)that requires to specify the byte order. -
wrap
The replacement forByteBuffer.wrap(byte[], int, int)that requires to specify the byte order. -
allocateDirect
The replacement forByteBuffer.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 forFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)that requires to specify the byte order.- Throws:
IOException
-
slice
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
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
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
Returns the actual native address the given direct ByteBuffer.- Returns:
- direct memory address
- Throws:
IllegalArgumentException- if the given ByteBuffer is not directUnsupportedOperationException- ifADDRESS_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 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:
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
Invoke the cleaner on the ByteBuffer Added in JDK 9- Parameters:
buffer- direct buffer where we are going to invoke the cleaner on
-