diff options
author | Sven Gothel <[email protected]> | 2013-04-29 19:33:55 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2013-04-29 19:33:55 +0200 |
commit | 6dd403b5b460e58a4b1cb3ed3e522355f50901b7 (patch) | |
tree | 08bd40c7293bd213c6f5d61aa8b928cc45420ca8 | |
parent | 35e932c32dad33693caa249a8139708412e8d798 (diff) |
Buffers: getRemainingBytes(Object) -> remainingBytes(Object); sizeOfBufferElem(Buffer) -> sizeOfBufferElem(Object) to include NativeBuffer<?>
Misc:
- Add remainingElem(Object buffer).
- Removed 'sizeOfBufferType(Class<?> bufferType)', since we don't use such calling convention w/ class type
Note: remainingBytes(..) exist to allow using only one branch traversal to return the remaining size in bytes
instead of 2, remaining(obj) and sizeOfBufferElem(obj).
Note: The methods can take NativeBuffer<?> as an argument.
-rw-r--r-- | src/java/com/jogamp/common/nio/Buffers.java | 120 |
1 files changed, 60 insertions, 60 deletions
diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index 2be7fd7..382084a 100644 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -332,7 +332,7 @@ public class Buffers { * Returns the size of a single element of the given buffer in bytes * or <code>0</code> if the given buffer is <code>null</code>. */ - public static int sizeOfBufferElem(Buffer buffer) { + public static int sizeOfBufferElem(Object buffer) { if (buffer == null) { return 0; } @@ -350,33 +350,73 @@ public class Buffers { return SIZEOF_LONG; } else if (buffer instanceof CharBuffer) { return SIZEOF_CHAR; + } else if (buffer instanceof NativeBuffer) { + return ((NativeBuffer<?>) buffer).elementSize(); } throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName()); } /** - * Returns the size of a single element of given buffer type in bytes. + * Returns the number of remaining elements of the given anonymous <code>buffer</code>. + * + * @param buffer Anonymous <i>Buffer</i> of type {@link NativeBuffer} or a derivation of {@link Buffer}. + * @return If <code>buffer</code> is null, returns <code>0<code>, otherwise the remaining size in elements. + * @throws IllegalArgumentException if <code>buffer</code> is of invalid type. */ - public static int sizeOfBufferType(Class<?> bufferType) { - if (ByteBuffer.class.isInstance(bufferType)) { - return SIZEOF_BYTE; - } else if (IntBuffer.class.isInstance(bufferType)) { - return SIZEOF_INT; - } else if (ShortBuffer.class.isInstance(bufferType)) { - return SIZEOF_SHORT; - } else if (FloatBuffer.class.isInstance(bufferType)) { - return SIZEOF_FLOAT; - } else if (DoubleBuffer.class.isInstance(bufferType)) { - return SIZEOF_DOUBLE; - } else if (LongBuffer.class.isInstance(bufferType)) { - return SIZEOF_LONG; - } else if (CharBuffer.class.isInstance(bufferType)) { - return SIZEOF_CHAR; + public static int remainingElem(Object buffer) throws IllegalArgumentException { + if (buffer == null) { + return 0; + } + if (buffer instanceof Buffer) { + return ((Buffer) buffer).remaining(); + } else if (buffer instanceof NativeBuffer) { + return ((NativeBuffer<?>) buffer).remaining(); + } else { + throw new IllegalArgumentException("Unsupported anonymous buffer type: "+buffer.getClass().getCanonicalName()); } - throw new RuntimeException("Unexpected buffer type " + bufferType.getName()); } /** + * Returns the number of remaining bytes of the given anonymous <code>buffer</code>. + * + * @param buffer Anonymous <i>Buffer</i> of type {@link NativeBuffer} or a derivation of {@link Buffer}. + * @return If <code>buffer</code> is null, returns <code>0<code>, otherwise the remaining size in bytes. + * @throws IllegalArgumentException if <code>buffer</code> is of invalid type. + */ + public static int remainingBytes(Object buffer) throws IllegalArgumentException { + if (buffer == null) { + return 0; + } + final int bytesRemaining; + if (buffer instanceof Buffer) { + int elementsRemaining = ((Buffer) buffer).remaining(); + if (buffer instanceof ByteBuffer) { + bytesRemaining = elementsRemaining; + } else if (buffer instanceof FloatBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_FLOAT; + } else if (buffer instanceof IntBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_INT; + } else if (buffer instanceof ShortBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_SHORT; + } else if (buffer instanceof DoubleBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_DOUBLE; + } else if (buffer instanceof LongBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_LONG; + } else if (buffer instanceof CharBuffer) { + bytesRemaining = elementsRemaining * SIZEOF_CHAR; + } else { + throw new InternalError("Unsupported Buffer type: "+buffer.getClass().getCanonicalName()); + } + } else if (buffer instanceof NativeBuffer) { + final NativeBuffer<?> nb = (NativeBuffer<?>) buffer; + bytesRemaining = nb.remaining() * nb.elementSize(); + } else { + throw new IllegalArgumentException("Unsupported anonymous buffer type: "+buffer.getClass().getCanonicalName()); + } + return bytesRemaining; + } + + /** * Helper routine to tell whether a buffer is direct or not. Null * pointers <b>are</b> considered direct. */ @@ -998,59 +1038,19 @@ public class Buffers { * @param buffer buffer to test for minimum * @param minBytesRemaining minimum bytes remaining * @throws IllegalArgumentException if <code>buffer</code> is of invalid type. - * @throws IndexOutOfBoundsException if {@link #getRemainingBytes(Object)} is < <code>minBytesRemaining<code>. + * @throws IndexOutOfBoundsException if {@link #remainingBytes(Object)} is < <code>minBytesRemaining<code>. */ public static void rangeCheckBytes(Object buffer, int minBytesRemaining) throws IllegalArgumentException, IndexOutOfBoundsException { if (buffer == null) { return; } - final int bytesRemaining = getRemainingBytes(buffer); + final int bytesRemaining = remainingBytes(buffer); if (bytesRemaining < minBytesRemaining) { throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining); } } /** - * Returns the number of remaining bytes of the given anonymous <code>buffer</code>. - * - * @param buffer Anonymous <i>Buffer</i> of type {@link NativeBuffer} or a derivation of {@link Buffer}. - * @return If <code>buffer</code> is null, returns <code>0<code>, otherwise the remaining size in bytes. - * @throws IllegalArgumentException if <code>buffer</code> is of invalid type. - */ - public static int getRemainingBytes(Object buffer) throws IllegalArgumentException { - if (buffer == null) { - return 0; - } - final int bytesRemaining; - if (buffer instanceof Buffer) { - int elementsRemaining = ((Buffer) buffer).remaining(); - if (buffer instanceof ByteBuffer) { - bytesRemaining = elementsRemaining; - } else if (buffer instanceof FloatBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_FLOAT; - } else if (buffer instanceof IntBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_INT; - } else if (buffer instanceof ShortBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_SHORT; - } else if (buffer instanceof DoubleBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_DOUBLE; - } else if (buffer instanceof LongBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_LONG; - } else if (buffer instanceof CharBuffer) { - bytesRemaining = elementsRemaining * SIZEOF_CHAR; - } else { - throw new InternalError("Unsupported Buffer type: "+buffer.getClass().getCanonicalName()); - } - } else if (buffer instanceof NativeBuffer) { - final NativeBuffer<?> nb = (NativeBuffer<?>) buffer; - bytesRemaining = nb.remaining() * nb.elementSize(); - } else { - throw new IllegalArgumentException("Unsupported anonymous buffer type: "+buffer.getClass().getCanonicalName()); - } - return bytesRemaining; - } - - /** * Appends Buffer details inclusive data to a StringBuilder instance. * @param sb optional pass through StringBuilder * @param f optional format string of one element, i.e. "%10.5f" for {@link FloatBuffer}, see {@link java.util.Formatter}, |