26import java.nio.Buffer;
27import java.nio.ByteBuffer;
28import java.nio.ByteOrder;
29import java.nio.CharBuffer;
30import java.nio.DoubleBuffer;
31import java.nio.FloatBuffer;
32import java.nio.IntBuffer;
33import java.nio.LongBuffer;
34import java.nio.ShortBuffer;
36import org.jau.lang.UnsafeUtil;
37import org.jau.sys.Debug;
38import org.jau.sys.PlatformProps;
44 static final boolean DEBUG =
Debug.
debug(
"NioUtil");
62 return ByteBuffer.allocateDirect( size ).order( ByteOrder.nativeOrder() );
71 return buf.order(ByteOrder.nativeOrder());
78 public static boolean isDirect(
final Object buf) {
82 if (buf instanceof Buffer) {
83 return ((Buffer) buf).isDirect();
85 throw new IllegalArgumentException(
"Unexpected buffer type " + buf.getClass().getName());
100 if (buf instanceof Buffer) {
101 final long pos = ((Buffer) buf).position();
102 if (buf instanceof ByteBuffer) {
104 }
else if (buf instanceof FloatBuffer) {
106 }
else if (buf instanceof IntBuffer) {
108 }
else if (buf instanceof ShortBuffer) {
110 }
else if (buf instanceof DoubleBuffer) {
112 }
else if (buf instanceof LongBuffer) {
114 }
else if (buf instanceof CharBuffer) {
118 throw new IllegalArgumentException(
"Disallowed array backing store type in buffer " + buf.getClass().getName());
133 if (buf instanceof Buffer) {
134 final long limit = ((Buffer) buf).limit();
135 if (buf instanceof ByteBuffer) {
137 }
else if (buf instanceof FloatBuffer) {
139 }
else if (buf instanceof IntBuffer) {
141 }
else if (buf instanceof ShortBuffer) {
143 }
else if (buf instanceof DoubleBuffer) {
145 }
else if (buf instanceof LongBuffer) {
147 }
else if (buf instanceof CharBuffer) {
151 throw new IllegalArgumentException(
"Disallowed array backing store type in buffer " + buf.getClass().getName());
158 private static final boolean hasCleaner;
160 private static volatile boolean cleanerError;
163 cleanerError = !hasCleaner;
165 System.err.println(
"Buffers.Cleaner.init: hasCleaner: "+hasCleaner+
", cleanerError "+cleanerError);
174 public static boolean clean(
final ByteBuffer bb) {
175 if( !hasCleaner && ( cleanerError || !bb.isDirect() ) ) {
Access to NIO sun.misc.Cleaner, allowing caller to deterministically clean a given sun....
static boolean clean(final ByteBuffer bb)
If b is an direct NIO buffer, i.e sun.nio.ch.DirectBuffer, calls it's sun.misc.Cleaner instance clean...
Utility methods allowing easy java.nio.Buffer manipulations.
static final int SIZEOF_CHAR
static final int SIZEOF_FLOAT
static final int SIZEOF_INT
static final int SIZEOF_BYTE
static ByteBuffer nativeOrder(final ByteBuffer buf)
Helper routine to set a ByteBuffer to the native byte order, if that operation is supported by the un...
static ByteBuffer newDirectByteBuffer(final int size)
Allocates a new direct ByteBuffer with the specified number of elements.
static final int SIZEOF_SHORT
static boolean isDirect(final Object buf)
Helper routine to tell whether a buffer is direct or not.
static final int SIZEOF_LONG
static final int SIZEOF_DOUBLE
static long getDirectBufferByteOffset(final Object buf)
Helper routine to get the Buffer byte offset by taking into account the Buffer position and the under...
static long getDirectBufferByteLimit(final Object buf)
Helper routine to get the Buffer byte limit by taking into account the Buffer limit and the underlyin...
Utility methods allowing easy access to certain sun.misc.Unsafe functionality.
static boolean hasInvokeCleaner()
Returns true if sun.misc.Unsafe.invokeCleaner(java.nio.ByteBuffer) is available and has not caused an...
static boolean invokeCleaner(final ByteBuffer bb)
Access to sun.misc.Unsafe.invokeCleaner(java.nio.ByteBuffer).
Helper routines for logging and debugging.
static final boolean debug(final String subcomponent)