diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/java/com/jogamp/common/nio/Buffers.java | 58 | ||||
-rw-r--r-- | src/java/com/jogamp/common/os/Platform.java | 21 | ||||
-rw-r--r-- | src/java/com/jogamp/common/util/ReflectionUtil.java | 14 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/IntIntHashMapTest.java | 13 | ||||
-rw-r--r-- | src/junit/com/jogamp/common/util/LongIntHashMapTest.java | 13 |
5 files changed, 90 insertions, 29 deletions
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/src/java/com/jogamp/common/os/Platform.java b/src/java/com/jogamp/common/os/Platform.java index 0d8b276..62a95d7 100644 --- a/src/java/com/jogamp/common/os/Platform.java +++ b/src/java/com/jogamp/common/os/Platform.java @@ -34,6 +34,8 @@ import com.jogamp.common.nio.Buffers; import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; +import java.security.AccessController; +import java.security.PrivilegedAction; /** * Utility class for querying platform specific properties. @@ -60,14 +62,14 @@ public class Platform { ARCH = System.getProperty("os.arch"); pointerSizeInBits = getPointerSizeInBitsImpl(); - is32Bit = initAch(); + is32Bit = initArch(); JAVA_SE = initIsJavaSE(); LITTLE_ENDIAN = initByteOrder(); } private Platform() {} - private static boolean initAch() throws RuntimeException { + private static boolean initArch() throws RuntimeException { // Try to use Sun's sun.ARCH.data.model first .. if ( 32 == pointerSizeInBits || 64 == pointerSizeInBits ) { return 32 == pointerSizeInBits; @@ -105,8 +107,13 @@ public class Platform { private static boolean initIsJavaSE() { - // fast path for desktop - if(System.getSecurityManager() == null && System.getProperty("java.runtime.name").indexOf("Java SE") != -1) { + // the fast path, check property Java SE instead of traversing through the ClassLoader + String java_runtime_name = (String) AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + return System.getProperty("java.runtime.name"); + } + }); + if(java_runtime_name.indexOf("Java SE") != -1) { return true; } @@ -115,9 +122,11 @@ public class Platform { Class.forName("java.nio.LongBuffer"); Class.forName("java.nio.DoubleBuffer"); return true; - }catch(ClassNotFoundException ex) { - return false; + } catch(ClassNotFoundException ex) { + // continue with Java SE check } + + return false; } private static boolean initByteOrder() { diff --git a/src/java/com/jogamp/common/util/ReflectionUtil.java b/src/java/com/jogamp/common/util/ReflectionUtil.java index fc4352f..cd4c062 100644 --- a/src/java/com/jogamp/common/util/ReflectionUtil.java +++ b/src/java/com/jogamp/common/util/ReflectionUtil.java @@ -75,7 +75,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the constructor can not be delivered. */ - public static final Constructor getConstructor(String clazzName, ClassLoader cl, Class[] cstrArgTypes) + public static final Constructor getConstructor(String clazzName, Class[] cstrArgTypes, ClassLoader cl) throws JogampRuntimeException { try { return getConstructor(getClassImpl(clazzName, true, cl), cstrArgTypes); @@ -111,7 +111,7 @@ public final class ReflectionUtil { public static final Constructor getConstructor(String clazzName, ClassLoader cl) throws JogampRuntimeException { - return getConstructor(clazzName, cl, new Class[0]); + return getConstructor(clazzName, new Class[0], cl); } /** @@ -147,7 +147,7 @@ public final class ReflectionUtil { return createInstance(clazz, cstrArgTypes, cstrArgs); } - public static final Object createInstance(String clazzName, ClassLoader cl, Class[] cstrArgTypes, Object[] cstrArgs) + public static final Object createInstance(String clazzName, Class[] cstrArgTypes, Object[] cstrArgs, ClassLoader cl) throws JogampRuntimeException, RuntimeException { try { @@ -157,20 +157,20 @@ public final class ReflectionUtil { } } - public static final Object createInstance(String clazzName, ClassLoader cl, Object[] cstrArgs) + public static final Object createInstance(String clazzName, Object[] cstrArgs, ClassLoader cl) throws JogampRuntimeException, RuntimeException { Class[] cstrArgTypes = new Class[cstrArgs.length]; for(int i=0; i<cstrArgs.length; i++) { cstrArgTypes[i] = cstrArgs[i].getClass(); } - return createInstance(clazzName, cl, cstrArgTypes, cstrArgs); + return createInstance(clazzName, cstrArgTypes, cstrArgs, cl); } public static final Object createInstance(String clazzName, ClassLoader cl) throws JogampRuntimeException, RuntimeException { - return createInstance(clazzName, cl, new Class[0], null); + return createInstance(clazzName, new Class[0], null, cl); } public static final boolean instanceOf(Object obj, String clazzName) { @@ -214,7 +214,7 @@ public final class ReflectionUtil { /** * @throws JogampRuntimeException if the instance can not be created. */ - public static final Object callStaticMethod(String clazzName, ClassLoader cl, String methodName, Class[] argTypes, Object[] args) + public static final Object callStaticMethod(String clazzName, String methodName, Class[] argTypes, Object[] args, ClassLoader cl) throws JogampRuntimeException, RuntimeException { Class clazz; diff --git a/src/junit/com/jogamp/common/util/IntIntHashMapTest.java b/src/junit/com/jogamp/common/util/IntIntHashMapTest.java index bc02947..9d58db0 100644 --- a/src/junit/com/jogamp/common/util/IntIntHashMapTest.java +++ b/src/junit/com/jogamp/common/util/IntIntHashMapTest.java @@ -105,6 +105,11 @@ public class IntIntHashMapTest { @Test public void benchmark() { + benchmark(true); + benchmark(false); + } + + void benchmark(boolean warmup) { // simple benchmark final IntIntHashMap intmap = new IntIntHashMap(1024); @@ -158,9 +163,11 @@ public class IntIntHashMapTest { map.remove(rndValues[i]); } - assertTrue("'put' to slow", intmapPutTime <= mapPutTime); - assertTrue("'get' to slow", intmapGetTime <= mapGetTime); - assertTrue("'remove' to slow", intmapRemoveTime <= mapRemoveTime); + if(!warmup) { + assertTrue("'put' too slow", intmapPutTime <= mapPutTime); + assertTrue("'get' too slow", intmapGetTime <= mapGetTime); + assertTrue("'remove' too slow", intmapRemoveTime <= mapRemoveTime); + } } diff --git a/src/junit/com/jogamp/common/util/LongIntHashMapTest.java b/src/junit/com/jogamp/common/util/LongIntHashMapTest.java index b51211c..7fc8978 100644 --- a/src/junit/com/jogamp/common/util/LongIntHashMapTest.java +++ b/src/junit/com/jogamp/common/util/LongIntHashMapTest.java @@ -105,6 +105,11 @@ public class LongIntHashMapTest { @Test public void benchmark() { + benchmark(true); + benchmark(false); + } + + void benchmark(boolean warmup) { // simple benchmark final LongIntHashMap intmap = new LongIntHashMap(1024); @@ -158,9 +163,11 @@ public class LongIntHashMapTest { map.remove(rndValues[i]); } - assertTrue("'put' to slow", intmapPutTime <= mapPutTime); - assertTrue("'get' to slow", intmapGetTime <= mapGetTime); - assertTrue("'remove' to slow", intmapRemoveTime <= mapRemoveTime); + if(!warmup) { + assertTrue("'put' too slow", intmapPutTime <= mapPutTime); + assertTrue("'get' too slow", intmapGetTime <= mapGetTime); + assertTrue("'remove' too slow", intmapRemoveTime <= mapRemoveTime); + } } |