diff options
Diffstat (limited to 'java_base/org/jau/io/Buffers.java')
-rw-r--r-- | java_base/org/jau/io/Buffers.java | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/java_base/org/jau/io/Buffers.java b/java_base/org/jau/io/Buffers.java index 524a715..a930958 100644 --- a/java_base/org/jau/io/Buffers.java +++ b/java_base/org/jau/io/Buffers.java @@ -87,8 +87,8 @@ public class Buffers { /** * Helper routine to get the Buffer byte offset by taking into - * account the Buffer position and the underlying type. This is - * the total offset for Direct Buffers. + * account the Buffer position and the underlying type. + * This is the total offset for Direct Buffers. * * Return value is of type `long` only to cover the `int` multiple of the position and element type size.<br/> * For ByteBuffer, the return value can be safely cast to `int`. @@ -119,6 +119,39 @@ public class Buffers { } /** + * Helper routine to get the Buffer byte limit by taking into + * account the Buffer limit and the underlying type. + * This is the total limit for Direct Buffers. + * + * Return value is of type `long` only to cover the `int` multiple of the position and element type size.<br/> + * For ByteBuffer, the return value can be safely cast to `int`. + */ + public static long getDirectBufferByteLimit(final Object buf) { + if (buf == null) { + return 0; + } + if (buf instanceof Buffer) { + final long limit = ((Buffer) buf).limit(); + if (buf instanceof ByteBuffer) { + return limit; + } else if (buf instanceof FloatBuffer) { + return limit * SIZEOF_FLOAT; + } else if (buf instanceof IntBuffer) { + return limit * SIZEOF_INT; + } else if (buf instanceof ShortBuffer) { + return limit * SIZEOF_SHORT; + } else if (buf instanceof DoubleBuffer) { + return limit * SIZEOF_DOUBLE; + } else if (buf instanceof LongBuffer) { + return limit * SIZEOF_LONG; + } else if (buf instanceof CharBuffer) { + return limit * SIZEOF_CHAR; + } + } + throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName()); + } + + /** * Access to NIO {@link sun.misc.Cleaner}, allowing caller to deterministically clean a given {@link sun.nio.ch.DirectBuffer}. */ public static class Cleaner { |