summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2013-04-27 04:50:47 +0200
committerSven Gothel <[email protected]>2013-04-27 04:50:47 +0200
commit5039f22bf0a89d658f613d14000e71be4e27f56a (patch)
tree90ab06e341cc12c9be17efa76c9ab3e554fe8164
parentb5ede1f7779bb55e25df4a5724c72f79d8f96640 (diff)
Buffers: Expose 'getRemainingBytes(Object buffer)'
-rw-r--r--src/java/com/jogamp/common/nio/Buffers.java36
-rw-r--r--src/java/com/jogamp/common/nio/NativeBuffer.java1
2 files changed, 31 insertions, 6 deletions
diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java
index 283cf07..22647d7 100644
--- a/src/java/com/jogamp/common/nio/Buffers.java
+++ b/src/java/com/jogamp/common/nio/Buffers.java
@@ -971,12 +971,34 @@ public class Buffers {
}
}
- public static void rangeCheckBytes(Object buffer, int minBytesRemaining) {
+ /**
+ * @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 &lt; <code>minBytesRemaining<code>.
+ */
+ public static void rangeCheckBytes(Object buffer, int minBytesRemaining) throws IllegalArgumentException, IndexOutOfBoundsException {
if (buffer == null) {
return;
}
-
- int bytesRemaining = 0;
+ final int bytesRemaining = getRemainingBytes(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) {
@@ -993,14 +1015,16 @@ public class Buffers {
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());
}
- if (bytesRemaining < minBytesRemaining) {
- throw new IndexOutOfBoundsException("Required " + minBytesRemaining + " remaining bytes in buffer, only had " + bytesRemaining);
- }
+ return bytesRemaining;
}
/**
diff --git a/src/java/com/jogamp/common/nio/NativeBuffer.java b/src/java/com/jogamp/common/nio/NativeBuffer.java
index 59d589b..0273976 100644
--- a/src/java/com/jogamp/common/nio/NativeBuffer.java
+++ b/src/java/com/jogamp/common/nio/NativeBuffer.java
@@ -39,6 +39,7 @@ import java.nio.Buffer;
* @author Sven Gothel
* @author Michael Bien
*/
+@SuppressWarnings("rawtypes")
public interface NativeBuffer<B extends NativeBuffer> {
public int elementSize();