From 5039f22bf0a89d658f613d14000e71be4e27f56a Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Sat, 27 Apr 2013 04:50:47 +0200 Subject: Buffers: Expose 'getRemainingBytes(Object buffer)' --- src/java/com/jogamp/common/nio/Buffers.java | 36 ++++++++++++++++++++---- src/java/com/jogamp/common/nio/NativeBuffer.java | 1 + 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src') 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 buffer is of invalid type. + * @throws IndexOutOfBoundsException if {@link #getRemainingBytes(Object)} is < minBytesRemaining. + */ + 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 buffer. + * + * @param buffer Anonymous Buffer of type {@link NativeBuffer} or a derivation of {@link Buffer}. + * @return If buffer is null, returns 0, otherwise the remaining size in bytes. + * @throws IllegalArgumentException if buffer 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 { public int elementSize(); -- cgit v1.2.3