Author: mturk Date: Thu Jun 18 16:44:28 2009 New Revision: 786157 URL: http://svn.apache.org/viewvc?rev=786157&view=rev Log: Add remaining toPrimitiveArray methods
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java commons/sandbox/runtime/trunk/src/main/native/shared/memory.c commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=786157&r1=786156&r2=786157&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Thu Jun 18 16:44:28 2009 @@ -407,4 +407,94 @@ return array1(ptr, offset, length); } + private static native short[] array2(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, OutOfMemoryError; + /** + * Return the {...@code ptr} memory area from {...@code src} to {...@code short} array. + * + * @param ptr {...@code Pointer} whose content to use. + * @param offset starting position in {...@code chars} in the memory. + * @param length the number of chars use. + * @return new {...@code short} array with values from {...@code src} memory area. + * + * @throws IllegalArgumentException if the {...@code offset} + * is {...@code negative} or {...@code length} + * is {...@code zero}. + * @throws IndexOutOfBoundsException if the operation would cause + * access of data outside allocated memory bounds. + * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws OutOfMemoryError if memory cannot be dipicated. + */ + public static short[] toShortArray(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException, OutOfMemoryError + { + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1) + throw new IllegalArgumentException(); + + return array2(ptr, offset, length); + } + + private static native int[] array3(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, OutOfMemoryError; + /** + * Return the {...@code ptr} memory area from {...@code src} to {...@code int} array. + * + * @param ptr {...@code Pointer} whose content to use. + * @param offset starting position in {...@code ints} in the memory. + * @param length the number of chars use. + * @return new {...@code int} array with values from {...@code src} memory area. + * + * @throws IllegalArgumentException if the {...@code offset} + * is {...@code negative} or {...@code length} + * is {...@code zero}. + * @throws IndexOutOfBoundsException if the operation would cause + * access of data outside allocated memory bounds. + * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws OutOfMemoryError if memory cannot be dipicated. + */ + public static int[] toIntegerArray(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException, OutOfMemoryError + { + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1) + throw new IllegalArgumentException(); + + return array3(ptr, offset, length); + } + + private static native long[] array4(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, OutOfMemoryError; + /** + * Return the {...@code ptr} memory area from {...@code src} to {...@code long} array. + * + * @param ptr {...@code Pointer} whose content to use. + * @param offset starting position in {...@code longs} in the memory. + * @param length the number of chars use. + * @return new {...@code long} array with values from {...@code src} memory area. + * + * @throws IllegalArgumentException if the {...@code offset} + * is {...@code negative} or {...@code length} + * is {...@code zero}. + * @throws IndexOutOfBoundsException if the operation would cause + * access of data outside allocated memory bounds. + * @throws NullPointerException if {...@code ptr} is {...@code null}. + * @throws OutOfMemoryError if memory cannot be dipicated. + */ + public static long[] toLongArray(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException, OutOfMemoryError + { + if (ptr == null || ptr.IsNull()) + throw new NullPointerException(); + if (offset < 0L || length < 1) + throw new IllegalArgumentException(); + + return array4(ptr, offset, length); + } + } Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=786157&r1=786156&r2=786157&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Thu Jun 18 16:44:28 2009 @@ -513,7 +513,7 @@ ACR_JNI_EXPORT_DECLARE(jcharArray, Memory, array1)(ACR_JNISTDARGS, jobject ptr, jlong poff, jint size) { - jbyteArray ba; + jcharArray oa; size_t pl; size_t dn = (size_t)poff; size_t cs = (size_t)size; @@ -523,9 +523,69 @@ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); return NULL; } - ba = (*_E)->NewCharArray(_E, cs); - if (ba) { - (*_E)->SetCharArrayRegion(_E, ba, 0, cs, p + dn); + oa = (*_E)->NewCharArray(_E, cs); + if (oa) { + (*_E)->SetCharArrayRegion(_E, oa, 0, cs, p + dn); } - return ba; + return oa; +} + +ACR_JNI_EXPORT_DECLARE(jshortArray, Memory, array2)(ACR_JNISTDARGS, jobject ptr, + jlong poff, jint size) +{ + jshortArray oa; + size_t pl; + size_t dn = (size_t)poff; + size_t cs = (size_t)size; + jshort *p = (jshort *)ACR_PointerGet(_E, ptr, &pl); + + if (((poff + cs) * sizeof(jshort)) > pl) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); + return NULL; + } + oa = (*_E)->NewShortArray(_E, cs); + if (oa) { + (*_E)->SetShortArrayRegion(_E, oa, 0, cs, p + dn); + } + return oa; +} + +ACR_JNI_EXPORT_DECLARE(jintArray, Memory, array3)(ACR_JNISTDARGS, jobject ptr, + jlong poff, jint size) +{ + jintArray oa; + size_t pl; + size_t dn = (size_t)poff; + size_t cs = (size_t)size; + jint *p = (jint *)ACR_PointerGet(_E, ptr, &pl); + + if (((poff + cs) * sizeof(jint)) > pl) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); + return NULL; + } + oa = (*_E)->NewIntArray(_E, cs); + if (oa) { + (*_E)->SetIntArrayRegion(_E, oa, 0, cs, p + dn); + } + return oa; +} + +ACR_JNI_EXPORT_DECLARE(jlongArray, Memory, array4)(ACR_JNISTDARGS, jobject ptr, + jlong poff, jint size) +{ + jlongArray oa; + size_t pl; + size_t dn = (size_t)poff; + size_t cs = (size_t)size; + jlong *p = (jlong *)ACR_PointerGet(_E, ptr, &pl); + + if (((poff + cs) * sizeof(jlong)) > pl) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); + return NULL; + } + oa = (*_E)->NewLongArray(_E, cs); + if (oa) { + (*_E)->SetLongArrayRegion(_E, oa, 0, cs, p + dn); + } + return oa; } Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java?rev=786157&r1=786156&r2=786157&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java (original) +++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java Thu Jun 18 16:44:28 2009 @@ -210,6 +210,48 @@ p.free(); } + public void testShortArray() + throws Throwable + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + short[] s = Memory.toShortArray(p, 0, 500); + assertNotNull("Array", s); + assertEquals("Length", 500, s.length); + assertEquals("Value", (short)0xFFFF, s[0]); + + p.free(); + } + + public void testIntArray() + throws Throwable + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + int[] i = Memory.toIntegerArray(p, 0, 250); + assertNotNull("Array", i); + assertEquals("Length", 250, i.length); + assertEquals("Value", (int)0xFFFFFFFF, i[0]); + + p.free(); + } + + public void testLongArray() + throws Throwable + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + long[] l = Memory.toLongArray(p, 0, 125); + assertNotNull("Array", l); + assertEquals("Length", 125, l.length); + assertEquals("Value", (long)0xFFFFFFFFFFFFFFFFL, l[0]); + + p.free(); + } + public void testPoke() throws Throwable {