Author: mturk Date: Wed Apr 22 11:19:27 2009 New Revision: 767469 URL: http://svn.apache.org/viewvc?rev=767469&view=rev Log: Add Memory.array method
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=767469&r1=767468&r2=767469&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 Wed Apr 22 11:19:27 2009 @@ -186,4 +186,23 @@ throws IndexOutOfBoundsException, IllegalArgumentException, NullPointerException; + /** + * Return the {...@code ptr} memory area from {...@code src} to {...@code byte} array. + * + * @param ptr {...@code Pointer} whose content to use. + * @param offset starting position in the memory. + * @param length the number of bytes use. + * @return new {...@link byte} 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}. + */ + public static native byte[] array(Pointer ptr, long offset, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException; + } 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=767469&r1=767468&r2=767469&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original) +++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Wed Apr 22 11:19:27 2009 @@ -447,3 +447,29 @@ memset(d + dn, c, cs); } +ACR_JNI_EXPORT_DECLARE(jbyteArray, Memory, array)(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; + char *p = (char *)ACR_PointerGet(_E, ptr, &pl); + if (!p) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0); + return NULL; + } + if (poff < 0L || size < 1) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0); + return NULL; + } + if ((poff + cs) > pl) { + ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0); + return NULL; + } + ba = (*_E)->NewByteArray(_E, (jsize)size); + if (ba) { + (*_E)->SetByteArrayRegion(_E, ba, 0, (jsize)size, (jbyte *)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=767469&r1=767468&r2=767469&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 Wed Apr 22 11:19:27 2009 @@ -138,5 +138,19 @@ dst.free(); } + public void testArray() + throws Throwable + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + byte[] b = Memory.array(p, 0, 1000); + assertNotNull("Array", b); + assertEquals("Length", 1000, b.length); + assertEquals("Value", (byte)0xFF, b[0]); + + p.free(); + } + }