commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1142890 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/util/Array.java test/org/apache/commons/runtime/TestArray.java
Date Tue, 05 Jul 2011 04:31:57 GMT
Author: mturk
Date: Tue Jul  5 04:31:57 2011
New Revision: 1142890

URL: http://svn.apache.org/viewvc?rev=1142890&view=rev
Log:
Implement Array.dup

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java

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=1142890&r1=1142889&r2=1142890&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
Tue Jul  5 04:31:57 2011
@@ -40,6 +40,8 @@ public final class Array
         throws ArrayIndexOutOfBoundsException;
     private static native boolean memset0(Object dst, int dstPos, int dstSiz,
                                           int number, int length);
+    private static native byte[]  dup0(Object src, int srcPos, int srcSiz,
+                                       int length);
 
     private static int getComponentSize(Class<?> componentType)
     {
@@ -62,6 +64,7 @@ public final class Array
         else
             return 0;
     }
+
     /**
      * System.arraycopy replacement that can copy between arrays of
      * different primitive types.
@@ -193,5 +196,42 @@ public final class Array
         return memcmp0(src, srcPos, elemSize1, dst, dstPos, elemSize2, length);
     }
 
+    /**
+     * Duplicate array to byte attay..
+     * <p>
+     * Duplicates the number of {@code length} elements of the Array {@code src}
+     * starting at the offset {@code srcPos} into the new byte array.
+     * </p>
+
+     * @param src
+     *            the source array to duplicate the content.
+     * @param srcPos
+     *            the starting index of the content in {@code src}.
+     * @param length
+     *            the number of elements of the {@code src} content they have
+     *            to be copied.
+     */
+    public static byte[] dup(Object src, int srcPos, int length)
+        throws ArrayIndexOutOfBoundsException, ArrayStoreException,
+               NullPointerException
+    {
+        if (srcPos < 0 || length < 0) {
+            // Sanity check
+            throw new ArrayIndexOutOfBoundsException();
+        }
+        // Sending getClass() to argument will check for null
+        Class<?> type1 = src.getClass();
+        if (!type1.isArray()) {
+            throw new ArrayStoreException();
+        }
+        Class<?> componentType1 = type1.getComponentType();
+
+        if (!componentType1.isPrimitive()) {
+            throw new ArrayStoreException();
+        }
+        int elemSize1 = getComponentSize(componentType1);
+        return dup0(src, srcPos, elemSize1, length);
+    }
+
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java?rev=1142890&r1=1142889&r2=1142890&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
(original)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
Tue Jul  5 04:31:57 2011
@@ -40,4 +40,19 @@ public class TestArray extends Assert
         assertEquals((int)ba[8] & 0xff, 4);
     }
 
+    @Test(groups = { "core" })
+    public void arrayDuplicate()
+        throws Exception
+    {
+        int[]  ia = { 1, 2, 3, 4 };
+        byte[] ba;
+
+        ba = Array.dup(ia, 1, 3);
+        // bytes 0 .. 3 contains integer[1] (2)
+        // ### Assertion works on LSB machines only
+        assertEquals((int)ba[0] & 0xff, 2);
+        assertEquals((int)ba[4] & 0xff, 3);
+        assertEquals((int)ba[8] & 0xff, 4);
+    }
+
 }



Mime
View raw message