diff options
author | Michael Bien <[email protected]> | 2010-07-04 22:00:26 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-07-04 22:00:26 +0200 |
commit | e125cd5b1a20221b338cc4f03810b3b8b69004c9 (patch) | |
tree | 576dd1f7ff6c8b9ec21c7ec070ff92187ab8a938 | |
parent | 13f196036debc83dd024b9460e986407c3b5f652 (diff) |
added slice utility methods to Buffers + rudimentary test.
-rwxr-xr-x | make/build.xml | 9 | ||||
-rw-r--r-- | nbproject/ide-file-targets.xml | 4 | ||||
-rwxr-xr-x | src/java/com/jogamp/common/nio/Buffers.java | 58 | ||||
-rw-r--r-- | test/junit/com/jogamp/common/nio/BuffersTest.java | 29 |
4 files changed, 84 insertions, 16 deletions
diff --git a/make/build.xml b/make/build.xml index 193c353..b881597 100755 --- a/make/build.xml +++ b/make/build.xml @@ -647,18 +647,18 @@ </delete> </target> - <target name="test" depends="init, gluegen.cpptasks.detect.os, junit.run"> + <target name="test" depends="init, gluegen.cpptasks.detect.os"> <mkdir dir="${build}/test/build/classes"/> <mkdir dir="${build}/test/results"/> <property name="tools.jar" value="${java.home}/../lib/tools.jar"/> - <javac destdir="${build}/test/build/classes" failonerror="false" source="1.5" srcdir="${gluegen.root}/test/junit" debug="true" debuglevel="lines,vars,source" - includeAntRuntime="false"> + <javac destdir="${build}/test/build/classes" srcdir="${gluegen.root}/test/junit" source="1.5" failonerror="false" + debug="true" debuglevel="lines,vars,source" includeAntRuntime="false"> <classpath path="${junit.jar}:${build}/classes:${build}/test/gensrc/java:${tools.jar}:${ant.core.lib}"/> </javac> - <junit forkmode="once" showoutput="true" fork="true"> + <junit forkmode="once" showoutput="true" fork="true" includeAntRuntime="true"> <env key="${system.env.library.path}" path="${gluegen.lib.dir}${path.separator}${build}/test/build/natives"/> <jvmarg value="-Djava.library.path=${gluegen.lib.dir}${path.separator}${build}/test/build/natives"/> <jvmarg value="-Drootrel.build=${rootrel.build}"/> @@ -667,6 +667,7 @@ <batchtest todir="${build}/test/results"> <fileset dir="${build}/test/build/classes"> <include name="com/sun/gluegen/**Test*"/> + <include name="com/jogamp/common/nio/**Test*"/> <include name="com/jogamp/common/util/**Test*"/> </fileset> <formatter usefile="false" type="plain"/> diff --git a/nbproject/ide-file-targets.xml b/nbproject/ide-file-targets.xml index d879376..7f1c61b 100644 --- a/nbproject/ide-file-targets.xml +++ b/nbproject/ide-file-targets.xml @@ -27,7 +27,7 @@ <fail unless="run.class">Must set property 'run.class'</fail> <junit errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="once" showoutput="true"> <test name="${run.class}"/> - <jvmarg value="-Djava.library.path=${basedir}/build/test/build/natives"/> + <jvmarg value="-Djava.library.path=${basedir}/build/test/build/natives:${basedir}/build/obj"/> <classpath> <path path="build/test/build/classes:build/test/gensrc/java:build/classes:lib/antlr-3.2.jar:make/lib/junit.jar:${jdk.home}/lib/tools.jar:${ant.core.lib}"/> </classpath> @@ -45,7 +45,7 @@ <junit errorproperty="tests.failed" failureproperty="tests.failed" fork="true" forkmode="once" showoutput="true"> <test name="${run.class}"/> <classpath refid="cp"/> - <jvmarg value="-Djava.library.path=${basedir}/build/test/build/natives"/> + <jvmarg value="-Djava.library.path=${basedir}/build/test/build/natives:${basedir}/build/obj"/> <jvmarg value="-Xdebug"/> <jvmarg value="-Xrunjdwp:transport=dt_socket,address=${jpda.address}"/> <formatter type="brief" usefile="false"/> diff --git a/src/java/com/jogamp/common/nio/Buffers.java b/src/java/com/jogamp/common/nio/Buffers.java index 0c16b4d..7e5f8eb 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -206,6 +206,44 @@ public class Buffers { } /** + * Calls slice on the concrete buffer type. + */ + public static Buffer slice(Buffer buffer) { + if (buffer instanceof ByteBuffer) { + return ((ByteBuffer) buffer).slice(); + } else if (buffer instanceof IntBuffer) { + return ((IntBuffer) buffer).slice(); + } else if (buffer instanceof ShortBuffer) { + return ((ShortBuffer) buffer).slice(); + } else if (buffer instanceof FloatBuffer) { + return ((FloatBuffer) buffer).slice(); + } else if (Platform.isJavaSE()) { + if (buffer instanceof DoubleBuffer) { + return ((DoubleBuffer) buffer).slice(); + } else if (buffer instanceof LongBuffer) { + return ((LongBuffer) buffer).slice(); + } else if (buffer instanceof CharBuffer) { + return ((CharBuffer) buffer).slice(); + } + } + throw new IllegalArgumentException("unexpected buffer type: " + buffer.getClass()); + } + + /** + * Slices the specified buffer with offset as position and offset+size as limit. + */ + public static Buffer slice(Buffer buffer, int offset, int size) { + int pos = buffer.position(); + int limit = buffer.limit(); + + buffer.position(offset).limit(offset+size); + Buffer slice = slice(buffer); + + buffer.position(pos).limit(limit); + return slice; + } + + /** * Helper routine to set a ByteBuffer to the native byte order, if * that operation is supported by the underlying NIO * implementation. @@ -277,7 +315,7 @@ public class Buffers { return ((CharBuffer) buf).isDirect(); } } - throw new RuntimeException("Unexpected buffer type " + buf.getClass().getName()); + throw new IllegalArgumentException("Unexpected buffer type " + buf.getClass().getName()); } /** @@ -316,7 +354,7 @@ public class Buffers { return pointerBuffer.position() * PointerBuffer.elementSize(); } - throw new RuntimeException("Disallowed array backing store type in buffer " + buf.getClass().getName()); + throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName()); } /** @@ -349,7 +387,7 @@ public class Buffers { } } - throw new RuntimeException("Disallowed array backing store type in buffer " + buf.getClass().getName()); + throw new IllegalArgumentException("Disallowed array backing store type in buffer " + buf.getClass().getName()); } /** @@ -389,7 +427,7 @@ public class Buffers { return PointerBuffer.elementSize() * (pointerBuffer.arrayOffset() + pointerBuffer.position()); } - throw new RuntimeException("Unknown buffer type " + buf.getClass().getName()); + throw new IllegalArgumentException("Unknown buffer type " + buf.getClass().getName()); } @@ -543,7 +581,7 @@ public class Buffers { return ((CharBuffer) dest).put((CharBuffer) src); } } - throw new RuntimeException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName()); + throw new IllegalArgumentException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName()); } public static Buffer putb(Buffer dest, byte v) { @@ -554,7 +592,7 @@ public class Buffers { } else if (dest instanceof IntBuffer) { return ((IntBuffer) dest).put((int) v); } else { - throw new RuntimeException("Byte doesn't match Buffer Class: " + dest); + throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest); } } @@ -564,7 +602,7 @@ public class Buffers { } else if (dest instanceof IntBuffer) { return ((IntBuffer) dest).put((int) v); } else { - throw new RuntimeException("Short doesn't match Buffer Class: " + dest); + throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest); } } @@ -572,7 +610,7 @@ public class Buffers { if (dest instanceof IntBuffer) { ((IntBuffer) dest).put(v); } else { - throw new RuntimeException("Integer doesn't match Buffer Class: " + dest); + throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest); } } @@ -584,14 +622,14 @@ public class Buffers { ((IntBuffer) dest).put(FixedPoint.toFixed(v)); */ } else { - throw new RuntimeException("Float doesn't match Buffer Class: " + dest); + throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest); } } public static void putd(Buffer dest, double v) { if (dest instanceof FloatBuffer) { ((FloatBuffer) dest).put((float) v); } else { - throw new RuntimeException("Double doesn't match Buffer Class: " + dest); + throw new IllegalArgumentException("Double doesn't match Buffer Class: " + dest); } } diff --git a/test/junit/com/jogamp/common/nio/BuffersTest.java b/test/junit/com/jogamp/common/nio/BuffersTest.java new file mode 100644 index 0000000..c5e5758 --- /dev/null +++ b/test/junit/com/jogamp/common/nio/BuffersTest.java @@ -0,0 +1,29 @@ +/* + * Created on Sunday, July 04 2010 20:00 + */ +package com.jogamp.common.nio; + +import java.nio.IntBuffer; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Michael Bien + */ +public class BuffersTest { + + @Test + public void slice() { + IntBuffer buffer = Buffers.newDirectIntBuffer(6); + buffer.put(new int[]{1,2,3,4,5,6}).rewind(); + + IntBuffer threefour = (IntBuffer)Buffers.slice(buffer, 2, 2); + + assertEquals(3, threefour.get(0)); + assertEquals(4, threefour.get(1)); + assertEquals(2, threefour.capacity()); + + } + +} |