From 7a1e5564a4ad86d0b122c056a71373bbeb472be6 Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 13 Feb 2011 13:16:02 +0100 Subject: - generified com.jogamp.common.nio.Buffers. - class is now final (change it back on demand) - added concurrency warning to slice() method doc --- src/java/com/jogamp/common/nio/Buffers.java | 83 ++++++++++++++++------------- 1 file changed, 46 insertions(+), 37 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 9b7db23..2a1ab18 100755 --- a/src/java/com/jogamp/common/nio/Buffers.java +++ b/src/java/com/jogamp/common/nio/Buffers.java @@ -41,11 +41,13 @@ package com.jogamp.common.nio; import java.nio.*; /** + * Utility methods allowing easy {@link java.nio.Buffer} manipulations. + * * @author Kenneth Russel * @author Sven Gothel * @author Michael Bien */ -public class Buffers { +public final class Buffers { public static final int SIZEOF_BYTE = 1; public static final int SIZEOF_SHORT = 2; @@ -55,7 +57,7 @@ public class Buffers { public static final int SIZEOF_LONG = 8; public static final int SIZEOF_DOUBLE = 8; - protected Buffers() {} + private Buffers() {} /** * Allocates a new direct ByteBuffer with the specified number of @@ -205,38 +207,45 @@ public class Buffers { } /** - * Calls slice on the concrete buffer type. + * Calls slice on the specified buffer. + * @see #slice(java.nio.Buffer, int, int) */ - public static Buffer slice(Buffer buffer) { + public static B slice(B buffer) { if (buffer instanceof ByteBuffer) { - return ((ByteBuffer) buffer).slice(); + return (B) ((ByteBuffer) buffer).slice(); } else if (buffer instanceof IntBuffer) { - return ((IntBuffer) buffer).slice(); + return (B) ((IntBuffer) buffer).slice(); } else if (buffer instanceof ShortBuffer) { - return ((ShortBuffer) buffer).slice(); + return (B) ((ShortBuffer) buffer).slice(); } else if (buffer instanceof FloatBuffer) { - return ((FloatBuffer) buffer).slice(); + return (B) ((FloatBuffer) buffer).slice(); } else if (buffer instanceof DoubleBuffer) { - return ((DoubleBuffer) buffer).slice(); + return (B) ((DoubleBuffer) buffer).slice(); } else if (buffer instanceof LongBuffer) { - return ((LongBuffer) buffer).slice(); + return (B) ((LongBuffer) buffer).slice(); } else if (buffer instanceof CharBuffer) { - return ((CharBuffer) buffer).slice(); + return (B) ((CharBuffer) buffer).slice(); } throw new IllegalArgumentException("unexpected buffer type: " + buffer.getClass()); } /** * Slices the specified buffer with offset as position and offset+size as limit. + * Concurrency warning: this method changes the buffers position and limit but + * will restore it before return. */ - public static Buffer slice(Buffer buffer, int offset, int size) { + public static B slice(B buffer, int offset, int size) { int pos = buffer.position(); int limit = buffer.limit(); - buffer.position(offset).limit(offset+size); - Buffer slice = slice(buffer); + B slice = null; + try{ + buffer.position(offset).limit(offset+size); + slice = slice(buffer); + }finally{ + buffer.position(pos).limit(limit); + } - buffer.position(pos).limit(limit); return slice; } @@ -623,69 +632,69 @@ public class Buffers { //---------------------------------------------------------------------- // Convenient put methods with generic target Buffer // - public static Buffer put(Buffer dest, Buffer src) { + public static B put(B dest, Buffer src) { if ((dest instanceof ByteBuffer) && (src instanceof ByteBuffer)) { - return ((ByteBuffer) dest).put((ByteBuffer) src); + return (B) ((ByteBuffer) dest).put((ByteBuffer) src); } else if ((dest instanceof ShortBuffer) && (src instanceof ShortBuffer)) { - return ((ShortBuffer) dest).put((ShortBuffer) src); + return (B) ((ShortBuffer) dest).put((ShortBuffer) src); } else if ((dest instanceof IntBuffer) && (src instanceof IntBuffer)) { - return ((IntBuffer) dest).put((IntBuffer) src); + return (B) ((IntBuffer) dest).put((IntBuffer) src); } else if ((dest instanceof FloatBuffer) && (src instanceof FloatBuffer)) { - return ((FloatBuffer) dest).put((FloatBuffer) src); + return (B) ((FloatBuffer) dest).put((FloatBuffer) src); } else if ((dest instanceof LongBuffer) && (src instanceof LongBuffer)) { - return ((LongBuffer) dest).put((LongBuffer) src); + return (B) ((LongBuffer) dest).put((LongBuffer) src); } else if ((dest instanceof DoubleBuffer) && (src instanceof DoubleBuffer)) { - return ((DoubleBuffer) dest).put((DoubleBuffer) src); + return (B) ((DoubleBuffer) dest).put((DoubleBuffer) src); } else if ((dest instanceof CharBuffer) && (src instanceof CharBuffer)) { - return ((CharBuffer) dest).put((CharBuffer) src); + return (B) ((CharBuffer) dest).put((CharBuffer) src); } throw new IllegalArgumentException("Incompatible Buffer classes: dest = " + dest.getClass().getName() + ", src = " + src.getClass().getName()); } - public static Buffer putb(Buffer dest, byte v) { + public static B putb(B dest, byte v) { if (dest instanceof ByteBuffer) { - return ((ByteBuffer) dest).put(v); + return (B) ((ByteBuffer) dest).put(v); } else if (dest instanceof ShortBuffer) { - return ((ShortBuffer) dest).put((short) v); + return (B) ((ShortBuffer) dest).put((short) v); } else if (dest instanceof IntBuffer) { - return ((IntBuffer) dest).put((int) v); + return (B) ((IntBuffer) dest).put((int) v); } else { throw new IllegalArgumentException("Byte doesn't match Buffer Class: " + dest); } } - public static Buffer puts(Buffer dest, short v) { + public static B puts(B dest, short v) { if (dest instanceof ShortBuffer) { - return ((ShortBuffer) dest).put(v); + return (B) ((ShortBuffer) dest).put(v); } else if (dest instanceof IntBuffer) { - return ((IntBuffer) dest).put((int) v); + return (B) ((IntBuffer) dest).put((int) v); } else { throw new IllegalArgumentException("Short doesn't match Buffer Class: " + dest); } } - public static void puti(Buffer dest, int v) { + public static B puti(B dest, int v) { if (dest instanceof IntBuffer) { - ((IntBuffer) dest).put(v); + return (B) ((IntBuffer) dest).put(v); } else { throw new IllegalArgumentException("Integer doesn't match Buffer Class: " + dest); } } - public static void putf(Buffer dest, float v) { + public static B putf(B dest, float v) { if (dest instanceof FloatBuffer) { - ((FloatBuffer) dest).put(v); + return (B) ((FloatBuffer) dest).put(v); /* TODO FixedPoint required } else if (dest instanceof IntBuffer) { - ((IntBuffer) dest).put(FixedPoint.toFixed(v)); + return (B) ((IntBuffer) dest).put(FixedPoint.toFixed(v)); */ } else { throw new IllegalArgumentException("Float doesn't match Buffer Class: " + dest); } } - public static void putd(Buffer dest, double v) { + public static B putd(B dest, double v) { if (dest instanceof FloatBuffer) { - ((FloatBuffer) dest).put((float) v); + return (B) ((FloatBuffer) dest).put((float) v); } else { throw new IllegalArgumentException("Double doesn't match Buffer Class: " + dest); } -- cgit v1.2.3 From 21e0b226492fe1db0126528c5fddfb29d222a9cd Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Sun, 13 Feb 2011 13:23:21 +0100 Subject: moved struct and buffer test to its nio friends, cleaned up imports --- .../jogamp/common/nio/TestPointerBufferEndian.java | 48 ++++++++++++++++++++ .../common/nio/TestStructAccessorEndian.java | 41 +++++++++++++++++ .../junit/runtime/TestPointerBufferEndian.java | 53 ---------------------- .../junit/runtime/TestStructAccessorEndian.java | 43 ------------------ 4 files changed, 89 insertions(+), 96 deletions(-) create mode 100644 src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java create mode 100644 src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/runtime/TestPointerBufferEndian.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/runtime/TestStructAccessorEndian.java (limited to 'src') diff --git a/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java b/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java new file mode 100644 index 0000000..da232a2 --- /dev/null +++ b/src/junit/com/jogamp/common/nio/TestPointerBufferEndian.java @@ -0,0 +1,48 @@ + +package com.jogamp.common.nio; + +import com.jogamp.common.os.*; + +import org.junit.Assert; +import org.junit.Test; + +import static java.lang.System.*; + +public class TestPointerBufferEndian { + + protected void testImpl (boolean direct) { + int bitsPtr = Platform.getPointerSizeInBits(); + String bitsProp = System.getProperty("sun.arch.data.model"); + String os = System.getProperty("os.name"); + String cpu = System.getProperty("os.arch"); + out.println("OS: <"+os+"> CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); + out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); + out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); + + long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; + long[] values32Bit = { 0x0000000089ABCDEFL, 0x0000000044332211L, 0x00000000BEEFAFFEL }; + + PointerBuffer ptr = direct ? PointerBuffer.allocateDirect(3) : PointerBuffer.allocate(valuesSource.length); + ptr.put(valuesSource, 0, valuesSource.length); + ptr.rewind(); + + int i=0; + while(ptr.hasRemaining()) { + long v = ptr.get() ; + long t = Platform.is32Bit() ? values32Bit[i] : valuesSource[i]; + Assert.assertTrue("Value["+i+"] shall be 0x"+Long.toHexString(t)+", is: 0x"+Long.toHexString(v), t == v); + i++; + } + Assert.assertTrue("iterator "+i+" != "+valuesSource.length, i==valuesSource.length); + } + + @Test + public void testDirect () { + testImpl (true); + } + + @Test + public void testIndirect () { + testImpl (false); + } +} diff --git a/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java b/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java new file mode 100644 index 0000000..09781ec --- /dev/null +++ b/src/junit/com/jogamp/common/nio/TestStructAccessorEndian.java @@ -0,0 +1,41 @@ +package com.jogamp.common.nio; + +import com.jogamp.common.nio.*; +import com.jogamp.common.os.*; + +import java.nio.*; + +import org.junit.Assert; +import org.junit.Test; + +import static java.lang.System.*; + +public class TestStructAccessorEndian { + + @Test + public void testStructAccessorEndian1 () { + int bitsPtr = Platform.getPointerSizeInBits(); + String bitsProp = System.getProperty("sun.arch.data.model"); + String os = System.getProperty("os.name"); + String cpu = System.getProperty("os.arch"); + out.println("OS: <"+os+"> CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); + out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); + out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); + + long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; + ByteBuffer tst = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG * valuesSource.length); + StructAccessor acc = new StructAccessor(tst); + + int i; + + for(i=0; i CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); - System.out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); - System.out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); - - long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; - long[] values32Bit = { 0x0000000089ABCDEFL, 0x0000000044332211L, 0x00000000BEEFAFFEL }; - - PointerBuffer ptr = direct ? PointerBuffer.allocateDirect(3) : PointerBuffer.allocate(valuesSource.length); - ptr.put(valuesSource, 0, valuesSource.length); - ptr.rewind(); - - int i=0; - while(ptr.hasRemaining()) { - long v = ptr.get() ; - long t = Platform.is32Bit() ? values32Bit[i] : valuesSource[i]; - Assert.assertTrue("Value["+i+"] shall be 0x"+Long.toHexString(t)+", is: 0x"+Long.toHexString(v), t == v); - i++; - } - Assert.assertTrue("iterator "+i+" != "+valuesSource.length, i==valuesSource.length); - } - - @Test - public void testDirect () { - testImpl (true); - } - - @Test - public void testIndirect () { - testImpl (false); - } -} diff --git a/src/junit/com/jogamp/gluegen/test/junit/runtime/TestStructAccessorEndian.java b/src/junit/com/jogamp/gluegen/test/junit/runtime/TestStructAccessorEndian.java deleted file mode 100644 index 264c86b..0000000 --- a/src/junit/com/jogamp/gluegen/test/junit/runtime/TestStructAccessorEndian.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.jogamp.gluegen.test.junit.runtime; - -import com.jogamp.common.nio.*; -import com.jogamp.common.os.*; - -import java.nio.*; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -import static java.lang.System.*; - -public class TestStructAccessorEndian { - - @Test - public void testStructAccessorEndian1 () { - int bitsPtr = Platform.getPointerSizeInBits(); - String bitsProp = System.getProperty("sun.arch.data.model"); - String os = System.getProperty("os.name"); - String cpu = System.getProperty("os.arch"); - System.out.println("OS: <"+os+"> CPU: <"+cpu+"> Bits: <"+bitsPtr+"/"+bitsProp+">"); - System.out.println("CPU is: "+ (Platform.is32Bit()?"32":"64") + " bit"); - System.out.println("Buffer is in: "+ (Platform.isLittleEndian()?"little":"big") + " endian"); - - long[] valuesSource = { 0x0123456789ABCDEFL, 0x8877665544332211L, 0xAFFEDEADBEEFAFFEL }; - ByteBuffer tst = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG * valuesSource.length); - StructAccessor acc = new StructAccessor(tst); - - int i; - - for(i=0; i Date: Sun, 13 Feb 2011 13:40:35 +0100 Subject: improved BuffersTest.slice(). --- src/junit/com/jogamp/common/nio/BuffersTest.java | 33 +++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/junit/com/jogamp/common/nio/BuffersTest.java b/src/junit/com/jogamp/common/nio/BuffersTest.java index 0b72cd4..836e46d 100644 --- a/src/junit/com/jogamp/common/nio/BuffersTest.java +++ b/src/junit/com/jogamp/common/nio/BuffersTest.java @@ -43,14 +43,45 @@ 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); + IntBuffer threefour = Buffers.slice(buffer, 2, 2); assertEquals(3, threefour.get(0)); assertEquals(4, threefour.get(1)); assertEquals(2, threefour.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + IntBuffer fourfivesix = Buffers.slice(buffer, 3, 3); + + assertEquals(4, fourfivesix.get(0)); + assertEquals(5, fourfivesix.get(1)); + assertEquals(6, fourfivesix.get(2)); + assertEquals(3, fourfivesix.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + IntBuffer onetwothree = Buffers.slice(buffer, 0, 3); + + assertEquals(1, onetwothree.get(0)); + assertEquals(2, onetwothree.get(1)); + assertEquals(3, onetwothree.get(2)); + assertEquals(3, onetwothree.capacity()); + + assertEquals(0, buffer.position()); + assertEquals(6, buffer.limit()); + + // is it really sliced? + buffer.put(2, 42); + + assertEquals(42, buffer.get(2)); + assertEquals(42, onetwothree.get(2)); + } -- cgit v1.2.3