commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r788742 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/MbString.java main/native/shared/mbstr.c test/org/apache/commons/runtime/TestStrings.java
Date Fri, 26 Jun 2009 15:35:20 GMT
Author: mturk
Date: Fri Jun 26 15:35:19 2009
New Revision: 788742

URL: http://svn.apache.org/viewvc?rev=788742&view=rev
Log:
Add few MbString methods

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java
    commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java

Modified: 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=788742&r1=788741&r2=788742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/MbString.java Fri
Jun 26 15:35:19 2009
@@ -35,23 +35,41 @@
      */
     public static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
 
+    /**
+     * Default string length.
+     * <p>
+     * Default string length is used for creating unknown strings
+     * </p>
+     * <pre>
+     * MbString s = new MbString();
+     * </pre>
+     */
+    public static final int    DEFAULT_STRING_LENGTH      = 1024;
+
     /*
      * Local Multibyte string container
      */
     private byte[] mbstr;
 
-	public byte[] getBytes()
-	{
-		return mbstr;
-	}
-
     public MbString()
     {
-        mbstr = null;
+        this(DEFAULT_STRING_LENGTH);
     }
 
     /**
-     * Create new {@code MbString} instance from the provided byte array.
+     * Create new {@code MbString} instance with the {@code size}
+     * length.
+     *
+     * @param size size of the string.
+     */
+    public MbString(int size)
+    {
+        mbstr = new byte[size];
+    }
+
+    /**
+     * Create new {@code MbString} instance from the provided
+     * {@code zero} terminated byte array.
      *
      * @param str byte array to use.
      */
@@ -60,33 +78,144 @@
         mbstr = str;
     }
 
+    /**
+     * Create new {@code MbString} instance from the provided
+     * {@code src} Multibyte String.
+     *
+     * @param src {@code MbString} to use.
+     */
+    public MbString(MbString src)
+        throws NullPointerException
+    {
+        int l = src.length();
+        mbstr = new byte[l + 1];
+        try {
+            System.arraycopy(src.mbstr, 0, mbstr, 0, l + 1);
+        } catch (Exception e) {
+            // Make compiler happy
+        }
+    }
+
+    /**
+     * Create new {@code MbString} instance from the provided
+     * {@code src} Multibyte String.
+     *
+     * @param src {@code MbString} to use.
+     * @param srcPos starting position in the {@code src} string.
+     */
+    public MbString(MbString src, int srcPos)
+        throws ArrayStoreException, IndexOutOfBoundsException,
+               NullPointerException
+    {
+        int l = src.length();
+        mbstr = new byte[l + 1];
+        System.arraycopy(src.mbstr, srcPos, mbstr, 0, l + 1);
+    }
+
     private static native byte[] init0(Pointer ptr)
         throws NullPointerException;
     /**
      * Create new {@code MbString} instance from the memory area
-     * pointed by {@code ptr}.
+     * pointed by {@code src}.
      *
-     * @param ptr memory {@code Pointer} to use.
+     * @param src memory {@code Pointer} to use.
      */
-    public MbString(Pointer ptr)
+    public MbString(Pointer src)
         throws NullPointerException
     {
-        mbstr = init0(ptr);
+        mbstr = init0(src);
+    }
+
+    private static native byte[] init2(Pointer ptr, int n)
+        throws NullPointerException;
+    /**
+     * Create new {@code MbString} instance from the memory area
+     * pointed by {@code src}.
+     *
+     * @param src memory {@code Pointer} to use.
+     */
+    public MbString(Pointer src, int n)
+        throws NullPointerException
+    {
+        mbstr = init2(src, n);
     }
 
     private static native byte[] init1(Pointer ptr, long off)
         throws IndexOutOfBoundsException, NullPointerException;
     /**
      * Create new {@code MbString} instance from the memory area
-     * pointed by {@code ptr}.
+     * pointed by {@code src}.
+     *
+     * @param src memory {@code Pointer} to use.
+     * @param srcPos starting position in the {@code src} Pointer.
+     */
+    public MbString(Pointer src, long srcPos)
+        throws NullPointerException
+    {
+        mbstr = init1(src, srcPos);
+    }
+
+    private static native byte[] init3(Pointer ptr, long off, int n)
+        throws IndexOutOfBoundsException, NullPointerException;
+    /**
+     * Create new {@code MbString} instance from the memory area
+     * pointed by {@code src}.
+     *
+     * @param src memory {@code Pointer} to use.
+     * @param srcPos starting position in the {@code src} Pointer.
+     */
+    public MbString(Pointer src, long srcPos, int n)
+        throws NullPointerException
+    {
+        mbstr = init3(src, srcPos, n);
+    }
+
+    /**
+     * Return the underlying storage byte array.
+     *
+     * @return byte array.
+     */
+    public byte[] getBytes()
+    {
+        return mbstr;
+    }
+
+    /**
+     * Return the string length.
      *
-     * @param ptr memory {@code Pointer} to use.
-     * @param off offset from the {@code Pointer} memory.
+     * @return string length in bytes.
      */
-    public MbString(Pointer ptr, long off)
+    public int length()
+    {
+        int i = 0;
+        while (i < mbstr.length && mbstr[i] != 0)
+            i++;
+        return i;
+    }
+
+
+    public static final byte[] strdup(Pointer src)
         throws NullPointerException
     {
-        mbstr = init1(ptr, off);
+        return init0(src);
+    }
+
+    public static final byte[] strdup(Pointer src, long srcPos)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        return init1(src, srcPos);
+    }
+
+    public static final byte[] strndup(Pointer src, int n)
+        throws NullPointerException
+    {
+        return init2(src, n);
+    }
+
+    public static final byte[] strndup(Pointer src, long srcPos, int n)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        return init3(src, srcPos, n);
     }
 
 }

Modified: 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=788742&r1=788741&r2=788742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/mbstr.c Fri Jun 26 15:35:19 2009
@@ -18,6 +18,7 @@
 #include "acr_private.h"
 #include "acr_string.h"
 #include "acr_memory.h"
+#include "acr_pointer.h"
 #include "acr_error.h"
 #include "acr_types.h"
 #include "acr_clazz.h"
@@ -89,8 +90,8 @@
         ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
         return NULL;
     }
-    cs = strlen(p) + 1;
-    ba = (*_E)->NewByteArray(_E, cs);
+    cs = strlen(p);
+    ba = (*_E)->NewByteArray(_E, cs + 1);
     if (ba) {
         (*_E)->SetByteArrayRegion(_E, ba, 0, cs, (jbyte *)p);
     }
@@ -114,8 +115,56 @@
         ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
         return NULL;
     }
-    cs = strlen(p + dn) + 1;
-    ba = (*_E)->NewByteArray(_E, cs);
+    cs = strlen(p + dn);
+    ba = (*_E)->NewByteArray(_E, cs + 1);
+    if (ba) {
+        (*_E)->SetByteArrayRegion(_E, ba, 0, cs, (jbyte *)(p + dn));
+    }
+    return ba;
+}
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, MbString, init2)(ACR_JNISTDARGS, jobject ptr,
+                                                    jint n)
+{
+    jbyteArray ba;
+    size_t  cs;
+    char    *p = (char *)ACR_PointerGet(_E, ptr, NULL);
+
+    if (!p) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+        return NULL;
+    }
+    cs = strlen(p);
+    if ((size_t)n < cs)
+        cs = (size_t)n;
+    ba = (*_E)->NewByteArray(_E, cs + 1);
+    if (ba) {
+        (*_E)->SetByteArrayRegion(_E, ba, 0, cs, (jbyte *)p);
+    }
+    return ba;
+}
+
+ACR_JNI_EXPORT_DECLARE(jbyteArray, MbString, init3)(ACR_JNISTDARGS, jobject ptr,
+                                                    jlong off, jint n)
+{
+    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);
+    if ((size_t)n < cs)
+        cs = (size_t)n;
+    ba = (*_E)->NewByteArray(_E, cs + 1);
     if (ba) {
         (*_E)->SetByteArrayRegion(_E, ba, 0, cs, (jbyte *)(p + dn));
     }

Modified: 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=788742&r1=788741&r2=788742&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStrings.java Fri
Jun 26 15:35:19 2009
@@ -62,6 +62,64 @@
         assertEquals("length", ba.length, ca.length);
     }
 
+    public void testMbStringC1()
+        throws Exception
+    {
+        Pointer p = Memory.malloc(16);
+        p.poke(0, 'A');
+        p.poke(1, 'B');
+        p.poke(2, 0);
+        MbString s = new MbString(p);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", 'A', ca[0]);
+        assertEquals("Value", 'B', ca[1]);
+        assertEquals("Value",  0,  ca[2]);
+        assertEquals("length", 2,  s.length());
+    }
+
+    public void testMbStringC2()
+        throws Exception
+    {
+        Pointer p = Memory.malloc(16);
+        p.poke(0, 'A');
+        p.poke(1, 'B');
+        p.poke(2, 0);
+        MbString s = new MbString(p, 1);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", 'A', ca[0]);
+        assertEquals("Value",  0,  ca[1]);
+        assertEquals("length", 1,  s.length());
+    }
+
+    public void testMbStringC3()
+        throws Exception
+    {
+        Pointer p = Memory.malloc(16);
+        p.poke(0, 'A');
+        p.poke(1, 'B');
+        p.poke(2, 0);
+        MbString s = new MbString(p, 1L);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", 'B', ca[0]);
+        assertEquals("Value",  0,  ca[1]);
+        assertEquals("length", 1,  s.length());
+    }
+
+    public void testMbStringC4()
+        throws Exception
+    {
+        Pointer p = Memory.malloc(16);
+        p.poke(0, 'A');
+        p.poke(1, 'B');
+        p.poke(2, 'C');
+        p.poke(3, 0);
+        MbString s = new MbString(p, 1L, 1);
+        byte [] ca = s.getBytes();
+        assertEquals("Value", 'B', ca[0]);
+        assertEquals("Value",  0,  ca[1]);
+        assertEquals("length", 1,  s.length());
+    }
+
 
 }
 



Mime
View raw message