From 3bfc1e484ae247214d47f25fcc9b9da223f51fe1 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Fri, 27 Jun 2014 10:26:25 +0200 Subject: Buffers: Split slice2Float(..) into dedicated methods for FloatBuffer and float[] backing-array - refine API doc w/ backing-array semantics --- src/java/com/jogamp/common/nio/Buffers.java | 85 ++++++++++++++++++----------- 1 file changed, 53 insertions(+), 32 deletions(-) (limited to 'src/java/com/jogamp/common') diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index c63a094..2a37e47 100644 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -258,14 +258,13 @@ public class Buffers { } /** - * Slices a ByteBuffer or a primitive float array to a FloatBuffer at the given position with the given size - * in float-space. + * Slices a ByteBuffer or a FloatBuffer to a FloatBuffer + * at the given position with the given size in float-space. *

- * The returned sliced buffer's start position is not necessarily zero, - * but the float position within the host ByteBuffer. + * The returned sliced buffer's start position is always zero. *

*

- * The returned sliced buffer is {@link FloatBuffer#mark() marked} at it's starting position. Hence + * The returned sliced buffer is {@link FloatBuffer#mark() marked} at it's {@link FloatBuffer#position() start position}. Hence * {@link FloatBuffer#reset()} will rewind it to start after applying relative operations like {@link FloatBuffer#get()}. *

*

@@ -275,14 +274,13 @@ public class Buffers { * This bug is resolved at least in Android 3.2. *

* - * @param buf source Buffer, maybe ByteBuffer (recommended) or FloatBuffer or null. + * @param buf source Buffer, maybe ByteBuffer (recommended) or FloatBuffer. * Buffer's position is ignored and floatPos is being used. - * @param backing source float array or null - * @param floatPos {@link Buffers#SIZEOF_FLOAT} position + * @param floatStartPos {@link Buffers#SIZEOF_FLOAT} position * @param floatSize {@link Buffers#SIZEOF_FLOAT} size * @return FloatBuffer w/ native byte order as given ByteBuffer */ - public static final FloatBuffer slice2Float(Buffer buf, float[] backing, int floatPos, int floatSize) { + public static final FloatBuffer slice2Float(Buffer buf, int floatStartPos, int floatSize) { final int pos; final int limit; if(null != buf) { @@ -296,15 +294,13 @@ public class Buffers { try { if(buf instanceof ByteBuffer) { ByteBuffer bb = (ByteBuffer) buf; - bb.position( floatPos * Buffers.SIZEOF_FLOAT ); - bb.limit( (floatPos + floatSize) * Buffers.SIZEOF_FLOAT ); + bb.position( floatStartPos * Buffers.SIZEOF_FLOAT ); + bb.limit( (floatStartPos + floatSize) * Buffers.SIZEOF_FLOAT ); res = bb.slice().order(bb.order()).asFloatBuffer(); // slice and duplicate may change byte order - } else if(null != backing) { - res = FloatBuffer.wrap(backing, floatPos, floatSize); } else if(buf instanceof FloatBuffer) { FloatBuffer fb = (FloatBuffer) buf; - fb.position( floatPos ); - fb.limit( floatPos + floatSize ); + fb.position( floatStartPos ); + fb.limit( floatStartPos + floatSize ); res = fb.slice(); // slice and duplicate may change byte order } else { throw new InternalError("Buffer not ByteBuffer, nor FloarBuffer, nor backing array given"); @@ -318,6 +314,31 @@ public class Buffers { return res; } + /** + * Slices a primitive float backing array to a FloatBuffer at the given position with the given size + * in float-space by {@link FloatBuffer#wrap(float[], int, int) wrapping} the backing array. + *

+ * Due to {@link FloatBuffer#wrap(float[], int, int) wrapping} the backing array, + * the returned sliced buffer's {@link FloatBuffer#position() start position} equals + * the given floatStartPos within the given backing array + * while it's {@link FloatBuffer#arrayOffset() array-offset} is zero. + * This has the advantage of being able to dismiss the {@link FloatBuffer#arrayOffset() array-offset} + * in user code, while only being required to consider it's {@link FloatBuffer#position() position}. + *

+ *

+ * The returned sliced buffer is {@link FloatBuffer#mark() marked} at it's {@link FloatBuffer#position() start position}. Hence + * {@link FloatBuffer#reset()} will rewind it to start after applying relative operations like {@link FloatBuffer#get()}. + *

+ * + * @param backing source float array + * @param floatStartPos {@link Buffers#SIZEOF_FLOAT} position + * @param floatSize {@link Buffers#SIZEOF_FLOAT} size + * @return FloatBuffer w/ native byte order as given ByteBuffer + */ + public static final FloatBuffer slice2Float(float[] backing, int floatStartPos, int floatSize) { + return (FloatBuffer) FloatBuffer.wrap(backing, floatStartPos, floatSize).mark(); + } + /** * Helper routine to set a ByteBuffer to the native byte order, if @@ -462,7 +483,7 @@ public class Buffers { return 0; } if (buf instanceof Buffer) { - int pos = ((Buffer) buf).position(); + final int pos = ((Buffer) buf).position(); if (buf instanceof ByteBuffer) { return pos; } else if (buf instanceof FloatBuffer) { @@ -516,7 +537,7 @@ public class Buffers { return 0; } if (buf instanceof Buffer) { - int pos = ((Buffer) buf).position(); + final int pos = ((Buffer) buf).position(); if (buf instanceof ByteBuffer) { return (((ByteBuffer) buf).arrayOffset() + pos); } else if (buf instanceof FloatBuffer) { @@ -725,7 +746,7 @@ public class Buffers { throw new IllegalArgumentException("payload ("+len+") greater than remaining dest bytes [len "+dest.length+", offset "+doffset+"]"); } for(int i=0; i