You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/06/26 16:08:00 UTC

svn commit: r788708 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/ main/native/shared/ test/org/apache/commons/runtime/

Author: mturk
Date: Fri Jun 26 14:07:59 2009
New Revision: 788708

URL: http://svn.apache.org/viewvc?rev=788708&view=rev
Log:
Add Multibyte String prototype class

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java   (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c   (with props)
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.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/main/native/shared/clazz.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java?rev=788708&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java Fri Jun 26 14:07:59 2009
@@ -0,0 +1,99 @@
+/* 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;
+
+/**
+ * Multibyte String class.
+ * <p>
+ * This class implements the Multibyte String class similar to C/C++ strings.
+ * As a side effect all strings are {@code zero} terminated, allowing easy
+ * exchange between native methods that require the {@code C} strings.
+ * </p>
+ *
+ * @see OS
+ * @since Runtime 1.0
+ */
+public class MbString
+{
+    /**
+     * Default encoding used to convert to strings. It should be {@code UTF-8},
+     * as most standards seem to converge, but the servlet API requires
+     * {@code 8859_1}, and this class is used mostly for web applications.
+     */
+    public static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
+
+    /*
+     * Local Multibyte string container
+     */
+    private byte[] mbstr;
+
+	public byte[] getBytes()
+	{
+		return mbstr;
+	}
+
+    public MbString()
+    {
+        mbstr = null;
+    }
+
+    /**
+     * Create new {@code MbString} instance from the provided byte array.
+     *
+     * @param str byte array to use.
+     */
+    public MbString(byte[] str)
+    {
+        mbstr = str;
+    }
+
+    private static native byte[] init0(Pointer ptr, long off)
+        throws IndexOutOfBoundsException, NullPointerException;
+    /**
+     * Create new {@code MbString} instance from the memory area
+     * pointed by {@code ptr}.
+     *
+     * @param ptr memory {@code Pointer} to use.
+     */
+    public MbString(Pointer ptr)
+        throws NullPointerException
+    {
+        try {
+            mbstr = init0(ptr, 0L);
+        } catch (IndexOutOfBoundsException e) {
+            // Just to make the compiler happy.
+        }
+    }
+
+    /**
+     * Create new {@code MbString} instance from the memory area
+     * pointed by {@code ptr}.
+     *
+     * @param ptr memory {@code Pointer} to use.
+     * @param off offset from the {@code Pointer} memory.
+     */
+    public MbString(Pointer ptr, long off)
+        throws NullPointerException
+    {
+        try {
+            mbstr = init0(ptr, off);
+        } catch (IndexOutOfBoundsException e) {
+            // Just to make the compiler happy.
+        }
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.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=788708&r1=788707&r2=788708&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.in Fri Jun 26 14:07:59 2009
@@ -79,6 +79,7 @@
 	$(SRCDIR)/shared/dbb.$(OBJ) \
 	$(SRCDIR)/shared/error.$(OBJ) \
 	$(SRCDIR)/shared/fco.$(OBJ) \
+	$(SRCDIR)/shared/mbstr.$(OBJ) \
 	$(SRCDIR)/shared/memory.$(OBJ) \
 	$(SRCDIR)/shared/modules.$(OBJ) \
 	$(SRCDIR)/shared/native.$(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=788708&r1=788707&r2=788708&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Jun 26 14:07:59 2009
@@ -73,6 +73,7 @@
 	$(SRCDIR)/shared/dbb.$(OBJ) \
 	$(SRCDIR)/shared/error.$(OBJ) \
 	$(SRCDIR)/shared/fco.$(OBJ) \
+	$(SRCDIR)/shared/mbstr.$(OBJ) \
 	$(SRCDIR)/shared/memory.$(OBJ) \
 	$(SRCDIR)/shared/modules.$(OBJ) \
 	$(SRCDIR)/shared/native.$(OBJ) \

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c?rev=788708&r1=788707&r2=788708&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/clazz.c Fri Jun 26 14:07:59 2009
@@ -246,6 +246,7 @@
 /* Forward class loading declarations */
 ACR_CLASS_LDEC(Descriptor);
 ACR_CLASS_LDEC(Pointer);
+ACR_CLASS_LDEC(MbString);
 ACR_CLASS_LDEC(Group);
 ACR_CLASS_LDEC(User);
 ACR_CLASS_LDEC(io_File);
@@ -255,6 +256,7 @@
 
     ACR_CLASS_LRUN(Descriptor);
     ACR_CLASS_LRUN(Pointer);
+    ACR_CLASS_LRUN(MbString);
     ACR_CLASS_LRUN(Group);
     ACR_CLASS_LRUN(User);
     ACR_CLASS_LRUN(io_File);
@@ -270,6 +272,7 @@
 
     ACR_CLASS_URUN(Descriptor);
     ACR_CLASS_URUN(Pointer);
+    ACR_CLASS_URUN(MbString);
     ACR_CLASS_URUN(Group);
     ACR_CLASS_URUN(User);
     ACR_CLASS_URUN(io_File);

Added: commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c?rev=788708&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c Fri Jun 26 14:07:59 2009
@@ -0,0 +1,106 @@
+/* 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.
+ */
+
+#include "acr.h"
+#include "acr_private.h"
+#include "acr_string.h"
+#include "acr_memory.h"
+#include "acr_error.h"
+#include "acr_types.h"
+#include "acr_clazz.h"
+#include "acr_vm.h"
+
+J_DECLARE_CLAZZ = {
+    NULL,
+    NULL,
+    ACR_CLASS_PATH "MbString"
+};
+
+J_DECLARE_M_ID(0000) = {
+    NULL,
+    "<init>",
+    "([B)V"
+};
+
+J_DECLARE_M_ID(0001) = {
+    NULL,
+    "getBytes",
+    "()[B"
+};
+
+ACR_CLASS_LDEF(MbString)
+{
+    int rv;
+
+    if ((rv = ACR_LoadClass(_E, &_clazzn, 0)) != ACR_SUCCESS)
+        return rv;
+    J_LOAD_METHOD(0000);
+    J_LOAD_METHOD(0001);
+
+    return ACR_SUCCESS;
+}
+
+ACR_CLASS_UDEF(MbString)
+{
+    ACR_UnloadClass(_E, &_clazzn);
+}
+
+ACR_DECLARE(jobject) ACR_MbStringObjectCreate(JNIEnv *_E, const char *str)
+{
+    if (_clazzn.i && J4MID(0000)) {
+	    jbyteArray ba;
+    	size_t sl = strlen(str) + 1;
+
+        if ((ba = (*_E)->NewByteArray(_E, sl))) {
+            (*_E)->SetByteArrayRegion(_E, ba, 0, (jsize)sl, (jbyte *)str);
+            return (*_E)->NewObject(_E, _clazzn.i, J4MID(0000), ba);
+        }
+        else {
+            ACR_SET_OS_ERROR(ACR_ENOMEM);
+            return NULL;
+        }
+    }
+    else {
+        ACR_SET_OS_ERROR(ACR_ECLASSNOTFOUND);
+        return NULL;
+    }
+}
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, MbString, init0)(ACR_JNISTDARGS, jobject ptr,
+                                                    jlong off)
+{
+    jbyteArray ba;
+    size_t  pl;
+    size_t  dn = (size_t)off;
+    size_t  cs;
+    char    *p = (char *)ACR_PointerGet(_E, ptr, &pl);
+
+    if (!p) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return NULL;
+    }
+    if (dn >= pl) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return NULL;
+    }
+    cs = strlen(p + dn) + 1;
+    ba = (*_E)->NewByteArray(_E, cs);
+    if (ba) {
+        (*_E)->SetByteArrayRegion(_E, ba, 0, cs, p + dn);
+    }
+    return ba;
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.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=788708&r1=788707&r2=788708&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 Jun 26 14:07:59 2009
@@ -43,6 +43,7 @@
         suite.addTest(TestDirectByteBuffer.suite());
         suite.addTest(TestPrivate.suite());
         suite.addTest(TestFile.suite());
+        suite.addTest(TestStrings.suite());
         return suite;
     }
 

Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java?rev=788708&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java Fri Jun 26 14:07:59 2009
@@ -0,0 +1,67 @@
+/* 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.exception.*;
+import java.lang.System;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import junit.framework.*;
+
+/**
+ * Strings tests
+ */
+public class TestStrings extends TestCase
+{
+
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestStrings.class);
+        return suite;
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        System.loadLibrary("acr");
+    }
+
+    public void testMbString()
+        throws Exception
+    {
+        byte [] ba = { (byte)'S', (byte)'t', (byte)'r', (byte)0 };
+        MbString s = new MbString(ba);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", ba[0], ca[0]);
+        assertEquals("length", ba.length, ca.length);
+    }
+
+    public void testMbStringPtr()
+        throws Exception
+    {
+        byte [] ba = { (byte)'S', (byte)'t', (byte)'r', (byte)0 };
+        Pointer p = Memory.malloc(16);
+        Memory.copy(ba, 0, p, 0, ba.length);
+        MbString s = new MbString(p);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", ba[0], ca[0]);
+        assertEquals("length", ba.length, ca.length);
+    }
+
+
+}
+

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