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/05/04 23:14:55 UTC

svn commit: r168193 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java

Author: elecharny
Date: Wed May  4 14:14:54 2005
New Revision: 168193

URL: http://svn.apache.org/viewcvs?rev=168193&view=rev
Log:
Changed the storage from a int array to a byte array : much simpler code, less bugs...

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

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java?rev=168193&r1=168192&r2=168193&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/BitString.java Wed May  4 14:14:54 2005
@@ -49,11 +49,11 @@
     /** Tells if the OctetString is streamed or not */
     private boolean isStreamed;
 
-    /** The string is stored in a int array */
-    private int[] ints;
+    /** The string is stored in a byte array */
+    private byte[] bytes;
 
-    /** Actual length of the int array */
-    private int nbInts;
+    /** Actual length of the byte array */
+    private int nbBytes;
 
     /** Actual length of the bit string */
     private int nbBits;
@@ -65,8 +65,8 @@
      */
     public BitString()
     {
-        ints         = new int[DEFAULT_LENGTH];
-        nbInts       = 0;
+        bytes        = new byte[DEFAULT_LENGTH];
+        nbBytes      = 0;
         isStreamed   = false;
         nbUnusedBits = 0;
         nbBits       = 0;
@@ -74,30 +74,30 @@
 
     /**
      * Creates a BitString with a specific length (length is the number
-     * of ints).
+     * of bytes).
      *
-     * @param length The BitString length (it's a byte number)
+     * @param length The BitString length (it's a number of bits)
     */
     public BitString(  int length )
     {
         nbBits = length;
 
-        // As we store values in an int, we must divide the length by 32
-        nbInts       = ( length / 32 ) + ( ( ( length % 32 ) != 0 ) ? 1 : 0 );
+        // As we store values in bytes, we must divide the length by 8
+        nbBytes      = ( length / 8 ) + ( ( ( length % 8 ) != 0 ) ? 1 : 0 );
 
         nbUnusedBits = length % 8;
 
-        if ( nbInts > DEFAULT_LENGTH )
+        if ( nbBytes > DEFAULT_LENGTH )
         {
 
             // TODO : implement the streaming
             isStreamed = true;
-            ints       = new int[nbInts];
+            bytes      = new byte[nbBytes];
         }
         else
         {
             isStreamed = false;
-            ints       = new int[nbInts];
+            bytes      = new byte[nbBytes];
         }
     }
 
@@ -106,14 +106,14 @@
      * Actually, it's just a simple BitString.
      * TODO Implement streaming.
      * 
-     * @param length The BitString length, in number of ints 
+     * @param length The BitString length, in number of bits 
      * @param isStreamed Tells if the BitString must be streamed or not 
     */
     public BitString(  int length, boolean isStreamed )
     {
         nbBits          = length;
         this.isStreamed = isStreamed;
-        nbInts          = ( length / 32 ) + ( ( ( length % 32 ) != 0 ) ? 1 : 0 );
+        nbBytes         = ( length / 8 ) + ( ( ( length % 8 ) != 0 ) ? 1 : 0 );
 
         nbUnusedBits    = length % 8;
 
@@ -121,11 +121,11 @@
         {
 
             // TODO : implement the streaming
-            ints = new int[nbInts];
+            bytes = new byte[nbBytes];
         }
         else
         {
-            ints = new int[nbInts];
+            bytes = new byte[nbBytes];
         }
     }
 
@@ -133,87 +133,51 @@
      * Creates a BitString with a value.  
      * 
      * @param bytes The value to store. The first byte contains the number 
-     * of unused ints
+     * of unused bits
      */
     public BitString(  byte[] bytes )
     {
-        int nbBytes = bytes.length - 1;
-        nbInts  = ( nbBytes / 4 ) + ( ( ( nbBytes % 4 ) != 0 ) ? 1 : 0 );
+        nbBytes = bytes.length - 1;
 
-        if ( nbInts > DEFAULT_LENGTH )
+        if ( nbBytes > DEFAULT_LENGTH )
         {
             isStreamed = true;
 
             // It will be a streamed OctetString.
             // TODO : implement the streaming
-            ints = new int[nbInts];
+            bytes = new byte[nbBytes];
         }
         else
         {
             isStreamed = false;
 
-            ints       = new int[nbInts];
+            bytes      = new byte[nbBytes];
         }
 
-        setBytes(bytes, nbBytes);
+        setBytes( bytes, nbBytes );
     }
 
     //~ Methods ------------------------------------------------------------------------------------
 
     /**
-     * Set the bytes into the ints.
-     */
-    private void setBytes(byte[] bytes, int nbBytes)
+     * Set the value into the bytes.
+     * @param bytes DOCUMENT ME!
+     * @param nbBytes DOCUMENT ME!
+    */
+    private void setBytes( byte[] bytes, int nbBytes )
     {
-	    // The first byte contains the number of unused ints
-	    nbUnusedBits = bytes[0] & 0x07;
-	    nbBits       = ( nbBytes * 8 ) - nbUnusedBits;
-	
-	    // We have to transfer the data from bytes to ints
-	    int pos      = 1;
-	    int bytesPos = 1;
-	
-	    for ( int i = 0; i < ( nbInts - 1 ); i++ )
-	    {
-	        ints[i] =
-	            ( ( bytes[( i * 4 ) + 1] & 0x00FF ) << 24 ) +
-	            ( ( bytes[( i * 4 ) + 2] & 0x00FF ) << 16 ) +
-	            ( ( bytes[( i * 4 ) + 3] & 0x00FF ) << 8 ) +
-	            ( bytes[( i * 4 ) + 4] & 0x00FF );
-	    }
-	
-	    switch ( nbBytes % 4 )
-	    {
-	
-	        case 0 :
-	            ints[nbInts - 1] = ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
-	            pos++;
-	            // fallthrough
-	
-	        case 3 :
-	            ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
-	                ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
-	            pos++;
-	            // fallthrough
-	
-	        case 2 :
-	            ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
-	                ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
-	            pos++;
-	            // fallthrough
-	
-	        case 1 :
-	            ints[nbInts - 1] = ( ints[nbInts - 1] << 8 ) +
-	                ( bytes[( ( nbInts - 1 ) * 4 ) + pos] & 0x00FF );
-	            pos++;
-	            // fallthrough
-	
-	        default :
-	        // Nothing to do...
-	    }
-	
-	    ints[nbInts - 1] >>= nbUnusedBits;
-	}
+
+        // The first byte contains the number of unused ints
+        nbUnusedBits = bytes[0] & 0x07;
+        nbBits       = ( nbBytes * 8 ) - nbUnusedBits;
+
+        // We have to transfer the data from bytes to ints
+        for ( int i = 0; i < nbBytes; i++ )
+        {
+            this.bytes[i] = bytes[i + 1];
+        }
+    }
+
     /**
      * Set a new BitString in the BitString. It will replace the old BitString,
      * and reset the current length with the new one.
@@ -231,18 +195,16 @@
 
         int nbBytes = bytes.length - 1;
 
-        nbInts  = ( nbBytes / 4 ) + ( ( ( nbBytes % 4 ) != 0 ) ? 1 : 0 );
-
-        if ( ( nbInts > DEFAULT_LENGTH ) && ( ints.length < nbInts ) )
+        if ( ( nbBytes > DEFAULT_LENGTH ) && ( bytes.length < nbBytes ) )
         {
 
             // The current size is too small.
             // We have to allocate more space
             // TODO : implement the streaming
-            ints = new int[nbInts];
+            bytes = new byte[nbBytes];
         }
 
-        setBytes(bytes, nbBytes);
+        setBytes( bytes, nbBytes );
     }
 
     /**
@@ -268,22 +230,11 @@
                 nbBits + " ints" );
         }
 
-        int posInt = pos / 32;
-
-        if ( ( pos / 32 ) < ( nbBits / 32 ) )
-        {
-
-            int bitNumber = 31 - ( pos % 32 );
-            int res       = ints[posInt] & ( 1 << bitNumber );
-            return res != 0;
-        }
-        else
-        {
-
-            int bitNumber = ( nbBits % 32 ) - ( pos % 32 );
-            return ( ints[posInt] & ( ( 0x01 << bitNumber ) - 1 ) ) != 0;
-        }
+        int posInt    = pos / 8;
 
+        int bitNumber = 7 - ( pos % 8 );
+        int res       = bytes[posInt] & ( 1 << bitNumber );
+        return res != 0;
     }
 
     /**