commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r884865 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/util/Array.java native/shared/array.c
Date Fri, 27 Nov 2009 13:05:27 GMT
Author: mturk
Date: Fri Nov 27 13:05:27 2009
New Revision: 884865

URL: http://svn.apache.org/viewvc?rev=884865&view=rev
Log:
Make array copy generic for all primitive type destinations

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
    commons/sandbox/runtime/trunk/src/main/native/shared/array.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java?rev=884865&r1=884864&r2=884865&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
Fri Nov 27 13:05:27 2009
@@ -29,10 +29,31 @@
         // No instance
     }
 
-    private static native void copy0(Object src, int srcPos, byte[] dst,
-                                     int dstPos, int length, int elemSize);
-
+    private static native boolean copy0(Object src, int srcPos, int srcSiz,
+                                        Object dst, int dstPos, int dstSiz,
+                                        int length);
 
+    private static int getComponentSize(Class<?> componentType)
+    {
+        if (componentType == Integer.TYPE)
+            return 4;
+        else if (componentType == Byte.TYPE)
+            return 1;
+        else if (componentType == Long.TYPE)
+            return 8;
+        else if (componentType == Short.TYPE)
+            return 2;
+        else if (componentType == Character.TYPE)
+            return 2;
+        else if (componentType == Boolean.TYPE)
+            return 1;
+        else if (componentType == Double.TYPE)
+            return 8;
+        else if (componentType == Float.TYPE)
+            return 4;
+        else
+            return 0;
+    }
     /**
      * System.arraycopy replacement that can copy between arrays of
      * different primitive types.
@@ -54,104 +75,31 @@
      *            the number of elements of the {@code src} content they have
      *            to be copied.
      */
-    public static void copy(Object src, int srcPos, byte[] dst,
+    public static void copy(Object src, int srcPos, Object dst,
                             int dstPos, int length)
     {
-        // sending getClass() to both arguments will check for null
+        // Sending getClass() to both arguments will check for null
         Class<?> type1 = src.getClass();
-        if (!type1.isArray()) {
+        Class<?> type2 = dst.getClass();
+        if (!type1.isArray() || !type2.isArray()) {
             throw new ArrayStoreException();
         }
         Class<?> componentType1 = type1.getComponentType();
+        Class<?> componentType2 = type2.getComponentType();
 
-        if (!componentType1.isPrimitive()) {
+        if (!componentType1.isPrimitive() ||
+            !componentType2.isPrimitive()) {
             throw new ArrayStoreException();
         }
         if (srcPos < 0 || dstPos < 0 || length < 0) {
             // Sanity check
             throw new ArrayIndexOutOfBoundsException();
         }
-        int elemSize = 0;
-        if (componentType1 == Integer.TYPE) {
-            elemSize = 4;
-            int[] A1 = (int[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Byte.TYPE) {
-            elemSize = 1;
-            byte[] A1 = (byte[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Long.TYPE) {
-            elemSize = 8;
-            long[] A1 = (long[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Short.TYPE) {
-            elemSize = 2;
-            short[] A1 = (short[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Character.TYPE) {
-            elemSize = 2;
-            char[] A1 = (char[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Boolean.TYPE) {
-            elemSize = 1;
-            boolean[] A1 = (boolean[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Double.TYPE) {
-            elemSize = 8;
-            double[] A1 = (double[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
-        else if (componentType1 == Float.TYPE) {
-            elemSize = 4;
-            float[] A1 = (float[])src;
-            if (length <= A1.length - srcPos &&
-               (length * elemSize) <= dst.length - dstPos) {
-                copy0(A1, srcPos, dst, dstPos, length, elemSize);
-            }
-            else
-                throw new ArrayIndexOutOfBoundsException();
-        }
+        int elemSize1 = getComponentSize(componentType1);
+        int elemSize2 = getComponentSize(componentType2);
+
+        if (!copy0(src, srcPos, elemSize1, dst, dstPos, elemSize2, length))
+            throw new ArrayIndexOutOfBoundsException();
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/array.c?rev=884865&r1=884864&r2=884865&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Fri Nov 27 13:05:27 2009
@@ -31,21 +31,39 @@
  * Array utilities
  */
 
-ACR_UTIL_EXPORT_DECLARE(void, Array, copy0)(ACR_JNISTDARGS,
-                                            jarray src,
-                                            jint srcPos,
-                                            jbyteArray dst,
-                                            jint dstPos,
-                                            jint length,
-                                            jint esize)
+ACR_UTIL_EXPORT_DECLARE(jboolean, Array, copy0)(ACR_JNISTDARGS,
+                                                jarray src,
+                                                jint srcPos,
+                                                jint srcSiz,
+                                                jarray dst,
+                                                jint dstPos,
+                                                jint dstSiz,
+                                                jint length)
 {
     jbyte *scp;
-    jsize  len = (jsize)length * esize;
-    jsize  off = (jsize)srcPos * esize;
+    jbyte *dcp;
+    jint   srcLen;
+    jint   dstLen;
+    jint   srcOff = srcPos * srcSiz;
+    jint   dstOff = dstPos * dstSiz;
+    jint   nbytes = length * srcSiz;
+
+    srcLen = (jint)(*_E)->GetArrayLength(_E, src);
+    dstLen = (jint)(*_E)->GetArrayLength(_E, dst);
+
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((dstLen * dstSiz) - dstOff)))
+        return JNI_FALSE;
 
     scp = (*_E)->GetPrimitiveArrayCritical(_E, src, NULL);
-    if (scp) {
-        (*_E)->SetByteArrayRegion(_E, dst, (jsize)dstPos, len, scp + off);
+    dcp = (*_E)->GetPrimitiveArrayCritical(_E, dst, NULL);
+    if (scp && dcp) {
+        memcpy(dcp + (size_t)dstOff,
+               scp + (size_t)srcOff, (size_t)nbytes);
         (*_E)->ReleasePrimitiveArrayCritical(_E, src, scp, 0);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, src, dcp, 0);
+        return JNI_TRUE;
     }
+    else
+        return JNI_FALSE;
 }



Mime
View raw message