commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r884839 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/util/ main/native/ main/native/shared/ test/org/apache/commons/runtime/
Date Fri, 27 Nov 2009 11:49:45 GMT
Author: mturk
Date: Fri Nov 27 11:49:44 2009
New Revision: 884839

URL: http://svn.apache.org/viewvc?rev=884839&view=rev
Log:
Add Array class that can copy to byte array from different primitive arrays

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/array.c   (with props)
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java   (with
props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.in
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java

Added: 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=884839&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
Fri Nov 27 11:49:44 2009
@@ -0,0 +1,158 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.util;
+
+/**
+ * Array routines.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class Array
+{
+    private Array()
+    {
+        // No instance
+    }
+
+    private static native void copy0(Object src, int srcPos, byte[] dst,
+                                     int dstPos, int length, int elemSize);
+
+
+    /**
+     * System.arraycopy replacement that can copy between arrays of
+     * different primitive types.
+     * <p>
+     * Copies the number of {@code length} elements of the Array {@code src}
+     * starting at the offset {@code srcPos} into the Array {@code dst} at
+     * the position {@code dstPos}.
+     * </p>
+
+     * @param src
+     *            the source array to copy the content.
+     * @param srcPos
+     *            the starting index of the content in {@code src}.
+     * @param dst
+     *            the destination array to copy the data into.
+     * @param dstPos
+     *            the starting index for the copied content in {@code dst}.
+     * @param length
+     *            the number of elements of the {@code src} content they have
+     *            to be copied.
+     */
+    public static void copy(Object src, int srcPos, byte[] dst,
+                            int dstPos, int length)
+    {
+        // sending getClass() to both arguments will check for null
+        Class<?> type1 = src.getClass();
+        if (!type1.isArray()) {
+            throw new ArrayStoreException();
+        }
+        Class<?> componentType1 = type1.getComponentType();
+
+        if (!componentType1.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();
+        }
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.in?rev=884839&r1=884838&r2=884839&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Fri Nov 27 11:49:44 2009
@@ -85,6 +85,7 @@
 
 COMMON_OBJS=\
 	$(SRCDIR)/shared/buildmark.$(OBJ) \
+	$(SRCDIR)/shared/array.$(OBJ) \
 	$(SRCDIR)/shared/bzip2.$(OBJ) \
 	$(SRCDIR)/shared/callback.$(OBJ) \
 	$(SRCDIR)/shared/clazz.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=884839&r1=884838&r2=884839&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Nov 27 11:49:44 2009
@@ -72,6 +72,7 @@
 
 COMMON_OBJS=\
 	$(SRCDIR)/shared/buildmark.$(OBJ) \
+	$(SRCDIR)/shared/array.$(OBJ) \
 	$(SRCDIR)/shared/bzip2.$(OBJ) \
 	$(SRCDIR)/shared/callback.$(OBJ) \
 	$(SRCDIR)/shared/clazz.$(OBJ) \

Added: 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=884839&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Fri Nov 27 11:49:44 2009
@@ -0,0 +1,51 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ *
+ * @author Mladen Turk
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_clazz.h"
+#include "acr_memory.h"
+#include "acr_pointer.h"
+#include "acr_vm.h"
+#include "acr_error.h"
+
+/**
+ * Array utilities
+ */
+
+ACR_UTIL_EXPORT_DECLARE(void, Array, copy0)(ACR_JNISTDARGS,
+                                            jarray src,
+                                            jint srcPos,
+                                            jbyteArray dst,
+                                            jint dstPos,
+                                            jint length,
+                                            jint esize)
+{
+    jbyte *scp;
+    jsize  len = (jsize)length * esize;
+    jsize  off = (jsize)srcPos * esize;
+
+    scp = (*_E)->GetPrimitiveArrayCritical(_E, src, NULL);
+    if (scp) {
+        (*_E)->SetByteArrayRegion(_E, dst, (jsize)dstPos, len, scp + off);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, src, scp, 0);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=884839&r1=884838&r2=884839&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Fri Nov
27 11:49:44 2009
@@ -35,6 +35,7 @@
         suite.addTest(TestProperties.suite());
         suite.addTest(TestOS.suite());
         suite.addTest(TestUUID.suite());
+        suite.addTest(TestArray.suite());
         suite.addTest(TestSyslog.suite());
         suite.addTest(TestUser.suite());
         suite.addTest(TestGroup.suite());

Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java?rev=884839&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java Fri Nov
27 11:49:44 2009
@@ -0,0 +1,57 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import org.apache.commons.runtime.util.Array;
+
+import java.lang.System;
+import junit.framework.*;
+
+/**
+ * Array Test.
+ *
+ */
+public class TestArray extends TestCase
+{
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestArray.class);
+        return suite;
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        Loader.load();
+    }
+
+    public void testArrayCopy()
+        throws Exception
+    {
+        int[]  ia = { 1, 2, 3, 4 };
+        byte[] ba = new byte[64];
+
+        Array.copy(ia, 1, ba, 0, 3);
+        // bytes 0 .. 3 contains integer[1] (2)
+        // ### Assertion works on LSB machines only
+        assertEquals("Value", 2, (int)ba[0] & 0xff);
+        assertEquals("Value", 3, (int)ba[4] & 0xff);
+        assertEquals("Value", 4, (int)ba[8] & 0xff);
+    }
+
+
+}

Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestArray.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message