directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r165429 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java
Date Sat, 30 Apr 2005 23:10:37 GMT
Author: elecharny
Date: Sat Apr 30 16:10:37 2005
New Revision: 165429

URL: http://svn.apache.org/viewcvs?rev=165429&view=rev
Log:
Implemented the OctetString ASN.1 primitive.

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java?rev=165429&r1=165428&r2=165429&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OctetString.java
Sat Apr 30 16:10:37 2005
@@ -16,9 +16,202 @@
  */
 package org.apache.asn1.primitives;
 
+import org.apache.asn1.util.StringUtils;
+import org.apache.asn1.util.pools.PoolObject;
+
+
 /**
+ * Emplement the Octet String primitive type.
+ *
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public class OctetString {
+public class OctetString extends PoolObject
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** A null MutableString */
+    public static final OctetString EMPTY_STRING = new OctetString();
+
+    /** A flag to mark the OctetString as Streamed (for OctetString larger than 1024 chars)
*/
+    // TODO implement the streaming...
+    public static final boolean STREAMED = true;
+
+    /** The default length of an octet string */
+    private static final int DEFAULT_LENGTH = 1024;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** Tells if the OctetString is streamed or not */
+    private boolean isStreamed;
+
+    /** The string is stored in a char array */
+    private byte[] bytes;
+
+    /** Actual length of the string */
+    private int length;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a OctetString, with a default length.
+     */
+    public OctetString()
+    {
+        bytes      = new byte[DEFAULT_LENGTH];
+        length     = 0;
+        isStreamed = false;
+    }
+
+    /**
+     * Creates a OctetString with a specific length.
+     * @param length DOCUMENT ME!
+    */
+    public OctetString(  int length )
+    {
+        this.length = length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+
+            // TODO : implement the streaming
+            isStreamed = true;
+            bytes      = new byte[length];
+        }
+        else
+        {
+            isStreamed = false;
+            bytes      = new byte[length];
+        }
+    }
+
+    /**
+     * Creates a streamed OctetString with a specific length.
+     * Actually, it's just a simple OctetString.
+     * TODO Implement streaming.
+     * @param length The OctetString length 
+     * @param isStreamed Tells if the OctetString must be streamed or not 
+    */
+    public OctetString(  int length, boolean isStreamed )
+    {
+        this.isStreamed = isStreamed;
+        this.length     = length;
+
+        if ( isStreamed )
+        {
+
+            // TODO : implement the streaming
+            bytes = new byte[length];
+        }
+        else
+        {
+            bytes = new byte[length];
+
+        }
+    }
+
+    /**
+     * Creates a OctetString with a value.  
+     * 
+     * @param bytes The value to store.
+     */
+    public OctetString(  byte[] bytes )
+    {
+        length = bytes.length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+            isStreamed = true;
+
+            // It will be a streamed OctetString.
+            // TODO : implement the streaming
+            this.bytes = new byte[length];
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+        else
+        {
+            isStreamed = false;
+
+            this.bytes = new byte[length];
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Set a new octetString in the OctetString. It will replace the old OctetString,
+     * and reset the current length with the new one.
+     * 
+     * @param bytes The string to store
+     */
+    public void setData( byte[] bytes )
+    {
+        length = bytes.length;
+
+        if ( length > DEFAULT_LENGTH )
+        {
+
+            if ( this.bytes.length < length )
+            {
+
+                // The current size is too small.
+                // We have to allocate more space
+                // It will be a streamed OctetString.
+                // TODO : implement the streaming
+                this.bytes = new byte[length];
+            }
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+        else
+        {
+
+            // We have to copy the data, because the parameter
+            // is not a copy.
+            System.arraycopy( bytes, 0, this.bytes, 0, length );
+        }
+    }
+
+    /**
+     * Get the data stored into the OctetString
+     * @return A byte array
+     */
+    public byte[] getValue()
+    {
+        return bytes;
+    }
+
+    /**
+     * Return a native String representation of the OctetString.
+     * @return DOCUMENT ME!
+    */
+    public String toString()
+    {
+
+        StringBuffer sb = new StringBuffer();
+
+        for ( int i = 0; i < length; i++ )
+        {
+            sb.append( StringUtils.dumpByte( bytes[i] ) );
+        }
+
+        return sb.toString();
+    }
 
+    /**
+     * Tells if the OctetString is streamed or not
+     * @return <code>true</code> if the OctetString is streamed.
+     */
+    public boolean isStreamed()
+    {
+        return isStreamed;
+    }
 }



Mime
View raw message