directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1033452 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/asn1/ber/tlv/ main/java/org/apache/directory/shared/asn1/primitives/ test/java/org/apache/directory/shared/asn1/ber/tlv/ test/java/org/apache/directory/sh...
Date Wed, 10 Nov 2010 13:06:26 GMT
Author: elecharny
Date: Wed Nov 10 13:06:26 2010
New Revision: 1033452

URL: http://svn.apache.org/viewvc?rev=1033452&view=rev
Log:
Fixed the BitString implementation.

Modified:
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java?rev=1033452&r1=1033451&r2=1033452&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
Wed Nov 10 13:06:26 2010
@@ -685,8 +685,7 @@ public class Value implements Serializab
             int length = bitString.size() + 1;
 
             buffer.put( TLV.getBytes( length ) );
-            buffer.put( bitString.getUnusedBits() );
-            buffer.put( bitString.getDataRef() );
+            buffer.put( bitString.getData() );
         }
         catch ( BufferOverflowException boe )
         {

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java?rev=1033452&r1=1033451&r2=1033452&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/asn1/primitives/BitString.java
Wed Nov 10 13:06:26 2010
@@ -39,21 +39,9 @@ public class BitString implements Serial
     /** A null MutableString */
     public static final BitString EMPTY_STRING = new BitString( 1 );
 
-    /**
-     * A flag to mark the OctetString as Streamed (for OctetString larger than
-     * 1024 chars)
-     */
-    public static final boolean STREAMED = true;
-
-    /** The default length of a BitString */
-    private static final int DEFAULT_LENGTH = 1024;
-
     /** The number of unused ints */
     private int nbUnusedBits;
 
-    /** Tells if the OctetString is streamed or not */
-    private boolean isStreamed;
-
     /** The string is stored in a byte array */
     private byte[] bytes;
 
@@ -88,46 +76,7 @@ public class BitString implements Serial
             nbBytes += 1;
         }
 
-        nbUnusedBits = ( 8 - length % 8 ) % 8;
-
-        if ( nbBytes > DEFAULT_LENGTH )
-        {
-            isStreamed = true;
-            bytes = new byte[nbBytes];
-        }
-        else
-        {
-            isStreamed = false;
-            bytes = new byte[nbBytes];
-        }
-    }
-
-
-    /**
-     * Creates a streamed BitString with a specific length. Actually, it's just
-     * a simple BitString.
-     * 
-     * @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 )
-    {
-        if ( length <= 0 )
-        {
-            // This is not allowed
-            throw new IndexOutOfBoundsException( I18n.err( I18n.ERR_00029_NULL_OR_NEG_LENGTH_NOT_ALLOWED
) );
-        }
-
-        nbBits = length;
-        this.isStreamed = isStreamed;
-        nbBytes = ( length / 8 );
-
-        if ( ( length % 8 ) != 0 )
-        {
-            nbBytes += 1;
-        }
-
-        nbUnusedBits = length % 8;
+        nbUnusedBits = ( 8 - ( length % 8 ) ) & 0x07;
 
         bytes = new byte[nbBytes];
     }
@@ -141,23 +90,13 @@ public class BitString implements Serial
      */
     public BitString( byte[] bytes )
     {
-        nbBytes = bytes.length - 1;
-
-        if ( nbBytes > DEFAULT_LENGTH )
-        {
-            isStreamed = true;
-
-            // It will be a streamed OctetString.
-            this.bytes = new byte[nbBytes];
-        }
-        else
+        if ( ( bytes == null ) || ( bytes.length == 0 ) )
         {
-            isStreamed = false;
-
-            this.bytes = new byte[nbBytes];
+            nbBits = -1;
+            return;
         }
 
-        setBytes( bytes, nbBytes );
+        setBytes( bytes );
     }
 
 
@@ -167,13 +106,15 @@ public class BitString implements Serial
      * @param bytes The bytes to copy
      * @param nbBytes Number of bytes to copy
      */
-    private void setBytes( byte[] bytes, int nbBytes )
+    private void setBytes( byte[] bytes )
     {
-        // The first byte contains the number of unused ints
+        // The first byte contains the number of unused bits
         nbUnusedBits = bytes[0] & 0x07;
+        nbBytes = bytes.length - 1;
         nbBits = ( nbBytes * 8 ) - nbUnusedBits;
+        this.bytes = new byte[nbBytes];
 
-        // We have to transfer the data from bytes to ints
+        // We have to transfer the data
         for ( int i = 0; i < nbBytes; i++ )
         {
             this.bytes[i] = bytes[i + 1];
@@ -195,46 +136,28 @@ public class BitString implements Serial
             return;
         }
 
-        int nbb = bytes.length - 1;
-
-        if ( ( nbb > DEFAULT_LENGTH ) && ( bytes.length < nbb ) )
-        {
-            // The current size is too small.
-            // We have to allocate more space
-            bytes = new byte[nbb];
-        }
-
-        setBytes( bytes, nbb );
+        setBytes( bytes );
     }
 
 
     /**
-     * Get the representation of a BitString
+     * Get the representation of a BitString. A first byte containing the number
+     * of unused bits is added
      * 
      * @return A byte array which represent the BitString
      */
     public byte[] getData()
     {
-        byte[] copy = new byte[bytes.length];
+        byte[] copy = new byte[bytes.length + 1];
 
-        System.arraycopy( bytes, 0, copy, 0, bytes.length );
+        System.arraycopy( bytes, 0, copy, 1, bytes.length );
+        copy[0] = (byte)nbUnusedBits;
 
         return copy;
     }
 
 
     /**
-     * Get the representation of a BitString
-     * 
-     * @return A byte array which represent the BitString (this is a reference to the internal
bytes)
-     */
-    public byte[] getDataRef()
-    {
-        return bytes;
-    }
-
-
-    /**
      * Get the number of unused bits
      * 
      * @return A byte which represent the number of unused bits
@@ -259,10 +182,10 @@ public class BitString implements Serial
             throw new IndexOutOfBoundsException( I18n.err( I18n.ERR_00030_BIT_NUMBER_OUT_OF_BOUND
) );
         }
 
-        int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
-        int bitNumber = ( pos + nbUnusedBits ) % 8;
+        int posBytes = ( pos >> 3 );
+        int bitNumber = 7 - pos % 8;
 
-        bytes[posInt] |= ( 1 << bitNumber );
+        bytes[posBytes] |= ( 1 << bitNumber );
     }
 
 
@@ -281,27 +204,28 @@ public class BitString implements Serial
             throw new IndexOutOfBoundsException( I18n.err( I18n.ERR_00030_BIT_NUMBER_OUT_OF_BOUND
) );
         }
 
-        int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
-        int bitNumber = ( pos + nbUnusedBits ) % 8;
+        int realPos = pos + nbUnusedBits;
+        int posBytes = nbBytes - 1 - ( realPos >> 3 );
+        int bitNumber = realPos % 8;
 
-        bytes[posInt] &= ~( 1 << bitNumber );
+        bytes[posBytes] &= ~( 1 << bitNumber );
     }
 
 
     /**
      * Get the bit stored into the BitString at a specific position. 
-     * The bits are stored from left to right, the LSB on the right and the
-     * MSB on the left
+     * The bits are stored from left to right, the LSB on the left and the
+     * MSB on the right
      * For instance, if we have 10 bits, then they are coded as 
-     * b9 b8 b7 b6 - b5 b4 b3 b2 - b1 b0 x x - x x x x
+     * b0 b1 b2 b3 - b4 b5 b6 b7 - b8 b9 x x - x x x x
      * 
      * With '1001 000x', where x is an unused bit, 
      *       ^ ^    ^ 
      *       | |    | 
      *       | |    |  
-     *       | |    +----- getBit(0) = 0 
-     *       | +---------- getBit(4) = 0 
-     *       +------------ getBit(6) = 1
+     *       | |    +----- getBit(6) = 0 
+     *       | +---------- getBit(2) = 0 
+     *       +------------ getBit(0) = 1
      *       
      * @param pos The position of the requested bit.  
      * 
@@ -315,10 +239,12 @@ public class BitString implements Serial
             throw new IndexOutOfBoundsException( I18n.err( I18n.ERR_00031_CANNOT_FIND_BIT,
pos, nbBits ) );
         }
 
-        int posInt = nbBytes - 1 - ( ( pos + nbUnusedBits ) >> 3 );
-        int bitNumber = ( pos + nbUnusedBits ) % 8;
+        int realPos = pos + nbUnusedBits;
+        int posBytes = nbBytes - ( realPos >> 3 ) - 1;
+        int bitNumber = realPos % 8;
 
-        int res = bytes[posInt] & ( 1 << bitNumber );
+        int res = bytes[posBytes] & ( 1 << bitNumber );
+        
         return res != 0;
     }
 
@@ -333,17 +259,6 @@ public class BitString implements Serial
 
 
     /**
-     * Tells if the OctetString is streamed or not
-     * 
-     * @return <code>true</code> if the OctetString is streamed.
-     */
-    public boolean isStreamed()
-    {
-        return isStreamed;
-    }
-
-
-    /**
      * Return a native String representation of the BitString.
      * 
      * @return A String representing the BitString
@@ -352,7 +267,7 @@ public class BitString implements Serial
     {
         StringBuilder sb = new StringBuilder();
 
-        for ( int i = nbBits; i > 0; i-- )
+        for ( int i = nbBits-1; i >= 0; i-- )
         {
             if ( getBit( i ) )
             {

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java?rev=1033452&r1=1033451&r2=1033452&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
Wed Nov 10 13:06:26 2010
@@ -20,6 +20,10 @@
 package org.apache.directory.shared.asn1.ber.tlv;
 
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
@@ -34,10 +38,6 @@ import org.apache.directory.shared.asn1.
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
 
 
 /**
@@ -783,7 +783,7 @@ public class ValueTest
             fail();
         }
         
-        assertEquals( "0x03 0x03 0x06 0x80 0x00 ", Asn1StringUtils.dumpBytes( buffer.array()
)  );
+        assertEquals( "0x03 0x03 0x06 0x00 0x40 ", Asn1StringUtils.dumpBytes( buffer.array()
)  );
     }
 }
 

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java?rev=1033452&r1=1033451&r2=1033452&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/asn1/primitives/BitStringTest.java
Wed Nov 10 13:06:26 2010
@@ -20,6 +20,10 @@
 package org.apache.directory.shared.asn1.primitives;
 
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 import org.apache.directory.junit.tools.Concurrent;
 import org.apache.directory.junit.tools.ConcurrentJunitRunner;
 import org.apache.directory.shared.asn1.codec.DecoderException;
@@ -27,10 +31,6 @@ import org.apache.directory.shared.asn1.
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-import static org.junit.Assert.assertTrue;
-
 
 /**
  * Test the Bit String primitive
@@ -98,10 +98,7 @@ public class BitStringTest
     public void testSingleBitBitString() throws DecoderException
     {
 
-        BitString bitString = new BitString( 1 );
-
-        bitString.setData( new byte[]
-            { 0x07, ( byte ) 0x80 } );
+        BitString bitString = new BitString(new byte[] {0x07, ( byte ) 0x80 });
 
         assertEquals( true, bitString.getBit( 0 ) );
     }
@@ -196,7 +193,7 @@ public class BitStringTest
         BitString bitString = new BitString( 32 );
 
         byte[] bytes = new byte[]
-            { (byte)0xAA, 0x11, (byte)0x88, (byte)0xFE };
+            { 0x00, (byte)0xAA, 0x11, (byte)0x88, (byte)0xFE };
         
         int[] bits = new int[]
             {
@@ -210,7 +207,7 @@ public class BitStringTest
         {
             if ( bits[i] == 1 )
             {
-                bitString.setBit( bits.length - i - 1 );
+                bitString.setBit( i );
             }
         }
         
@@ -234,13 +231,13 @@ public class BitStringTest
         {
             if ( bits[i] == 1 )
             {
-                bitString.setBit( bits.length - i - 1 );
+                bitString.setBit( i );
             }
         }
         
         bitString.setBit( 9 );
         byte[] bytesModified = new byte[]
-            { (byte)0xAA, 0x11, (byte)0x8A, (byte)0xFE };
+            { 0x00, (byte)0xAA, 0x51, (byte)0x88, (byte)0xFE };
                             
         assertEquals( Asn1StringUtils.dumpBytes( bytesModified ), Asn1StringUtils.dumpBytes(
bitString.getData() ) );
     }
@@ -262,13 +259,13 @@ public class BitStringTest
         {
             if ( bits[i] == 1 )
             {
-                bitString.setBit( bits.length - i - 1 );
+                bitString.setBit( i );
             }
         }
         
         bitString.clearBit( 11 );
         byte[] bytesModified = new byte[]
-            { (byte)0xAA, 0x11, (byte)0x80, (byte)0xFE };
+            { 0x00, (byte)0xAA, 0x11, (byte)0x80, (byte)0xFE };
                             
         assertEquals( Asn1StringUtils.dumpBytes( bytesModified ), Asn1StringUtils.dumpBytes(
bitString.getData() ) );
     }



Mime
View raw message