You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/04/03 19:38:55 UTC

svn commit: r159938 - directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java

Author: elecharny
Date: Sun Apr  3 10:38:55 2005
New Revision: 159938

URL: http://svn.apache.org/viewcvs?view=rev&rev=159938
Log:
Created a very simple and first draft of a MutableString which is Poolable. Many method are to be added, but actually, this is enough for the decoder to work.

Added:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java?view=auto&rev=159938
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/MutableString.java Sun Apr  3 10:38:55 2005
@@ -0,0 +1,119 @@
+/*
+ *   Copyright 2005 The Apache Software Foundation
+ *
+ *   Licensed 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.asn1.util;
+
+import org.apache.asn1.util.pools.PoolObject;
+
+/**
+ * A Mutable version of the String class. This mutable Strins can be
+ * stored in a pool.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class MutableString extends PoolObject {
+    /** The string is stored in a char array */
+    private char[] string;
+    
+    /** Actual length of the string */
+    private int length;
+    
+    /** A null MutableString */
+    public static final MutableString EMPTY_STRING = new MutableString();
+    
+    /** A flag to mark the MutableString as Streamed (for string larger than 1024 chars) */
+    // TODO implement the streaming...
+    public static final boolean STREAMED = true;
+    
+    /**
+     * Creates a MutableString, with a default length of 16.
+     */
+    public MutableString()
+    {
+        string = new char[16];
+        length = 0;
+    }
+    
+    /**
+     * Creates a MutableString with a specific length.
+     */
+    public MutableString(int length)
+    {
+        string = new char[length];
+        this.length = length;
+    }
+    
+    /**
+     * Creates a streamed MutableString with a specific length.
+     * Actually, it's just a simple MutableString.
+     * TODO Implement streaming.
+     */
+    public MutableString(int length, boolean isStreamed)
+    {
+        string = new char[length];
+        this.length = length;
+    }
+    
+    /**
+     * Creates a MutableString with a value. The stored string
+     * is coded in Unicode, so the bytes *MUST* be valid ! 
+     * 
+     * The char array size will be equal to 16, 32, 64 ... depending on
+     * the string length to store. (some memory is lost, but this is the 
+     * price to pay for a performant pool mechanism : we are not going to 
+     * create a pool for each size !)
+     * 
+     * @param bytes The value to store.
+     */
+    public MutableString(byte[] bytes)
+    {
+        length = StringUtils.countChars(bytes);
+        string = new char[16 * (1 + length / 16)];
+        int pos = 0;
+        
+        for ( int i = 0; i < length; i++ )
+        {
+            string[i] = StringUtils.bytesToChar(bytes, pos);
+            pos += StringUtils.countBytesPerChar(bytes, pos);
+        }
+    }
+    
+    /**
+     * Set a new string in the MutableString. It will replace the old string,
+     * and reset the current length with the new one.
+     * 
+     * @param bytes The string to store
+     */
+    public void setData(byte[] bytes)
+    {
+        length = StringUtils.countChars(bytes);
+        int pos = 0;
+        
+        for ( int i = 0; i < length; i++ )
+        {
+            string[i] = StringUtils.bytesToChar(bytes, pos);
+            pos += StringUtils.countBytesPerChar(bytes, pos); 
+        }
+    }
+    
+    /**
+     * Return a native String representation of the MutableString.
+     */
+    public String toString()
+    {
+        return new String(string, 0, length);
+    }
+}