Author: mturk Date: Thu Jun 18 16:33:46 2009 New Revision: 786154 URL: http://svn.apache.org/viewvc?rev=786154&view=rev Log: Add toCharArray
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=786154&r1=786153&r2=786154&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:33:46 2009 @@ -376,4 +376,35 @@ return array0(ptr, offset, length); } + + private static native char[] array1(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, OutOfMemoryError; + /** + * Return the {...@code ptr} memory area from {...@code src} to {...@code char} 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 char} 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 char[] toCharArray(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 array1(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=786154&r1=786153&r2=786154&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:33:46 2009 @@ -510,3 +510,22 @@ return ba; } +ACR_JNI_EXPORT_DECLARE(jcharArray, Memory, array1)(ACR_JNISTDARGS, jobject ptr, + jlong poff, jint size) +{ + jbyteArray ba; + size_t pl; + size_t dn = (size_t)poff; + size_t cs = (size_t)size; + jchar *p = (jchar *)ACR_PointerGet(_E, ptr, &pl); + + if (((poff + cs) * sizeof(jchar)) > pl) { + 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); + } + return ba; +} 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=786154&r1=786153&r2=786154&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:33:46 2009 @@ -196,6 +196,20 @@ p.free(); } + public void testCharArray() + throws Throwable + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + char[] c = Memory.toCharArray(p, 0, 500); + assertNotNull("Array", c); + assertEquals("Length", 500, c.length); + assertEquals("Value", (char)0xFFFF, c[0]); + + p.free(); + } + public void testPoke() throws Throwable {