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 2008/07/24 02:03:30 UTC

svn commit: r679239 - in /directory/shared/branches/bigbang/asn1/src: main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java

Author: elecharny
Date: Wed Jul 23 17:03:29 2008
New Revision: 679239

URL: http://svn.apache.org/viewvc?rev=679239&view=rev
Log:
o Added a getNbBytes(long) method for Long values in a PDU
o Added a test to check that this method works correctly

Modified:
    directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
    directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java

Modified: directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java?rev=679239&r1=679238&r2=679239&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java (original)
+++ directory/shared/branches/bigbang/asn1/src/main/java/org/apache/directory/shared/asn1/ber/tlv/Value.java Wed Jul 23 17:03:29 2008
@@ -77,6 +77,22 @@
 
     private static final int THREE_BYTE_MIN = -( 1 << 23 );
 
+    private static final long FOUR_BYTE_MAX = ( 1L << 31 ) - 1L; // 0x7FFFFFFF
+
+    private static final long FOUR_BYTE_MIN = -( 1L << 31 ); 
+
+    private static final long FIVE_BYTE_MAX = ( 1L << 39 ) - 1L; // 0x7FFFFFFFFF
+
+    private static final long FIVE_BYTE_MIN = -( 1L << 39 ); 
+
+    private static final long SIX_BYTE_MAX = ( 1L << 47 ) - 1L; // 0x7FFFFFFFFFFF
+
+    private static final long SIX_BYTE_MIN = -( 1L << 47 ); 
+
+    private static final long SEVEN_BYTE_MAX = ( 1L << 55 ) - 1L; // 0x7FFFFFFFFFFFFF
+
+    private static final long SEVEN_BYTE_MIN = -( 1L << 55 ); 
+
     // ~ Methods
     // ------------------------------------------------------------------------------------
 
@@ -226,6 +242,51 @@
 
 
     /**
+     * Utility function that return the number of bytes necessary to store a
+     * long value. Note that this value must be in [Long.MIN_VALUE,
+     * Long.MAX_VALUE].
+     * 
+     * @param value The value to store in a byte array
+     * @return The number of bytes necessary to store the value.
+     */
+    public static int getNbBytes( long value )
+    {
+        if ( value >= ONE_BYTE_MIN && value <= ONE_BYTE_MAX )
+        {
+            return 1;
+        }
+        else if ( value >= TWO_BYTE_MIN && value <= TWO_BYTE_MAX )
+        {
+            return 2;
+        }
+        else if ( value >= THREE_BYTE_MIN && value <= THREE_BYTE_MAX )
+        {
+            return 3;
+        }
+        else if ( value >= FOUR_BYTE_MIN && value <= FOUR_BYTE_MAX )
+        {
+            return 4;
+        }
+        else if ( value >= FIVE_BYTE_MIN && value <= FIVE_BYTE_MAX )
+        {
+            return 5;
+        }
+        else if ( value >= SIX_BYTE_MIN && value <= SIX_BYTE_MAX )
+        {
+            return 6;
+        }
+        else if ( value >= SEVEN_BYTE_MIN && value <= SEVEN_BYTE_MAX )
+        {
+            return 7;
+        }
+        else
+        {
+            return 8;
+        }
+    }
+
+
+    /**
      * Utility function that return a byte array representing the Value We must
      * respect the ASN.1 BER encoding scheme : 
      * 1) positive integer 

Modified: directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java?rev=679239&r1=679238&r2=679239&view=diff
==============================================================================
--- directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java (original)
+++ directory/shared/branches/bigbang/asn1/src/test/java/org/apache/directory/shared/asn1/ber/tlv/ValueTest.java Wed Jul 23 17:03:29 2008
@@ -30,6 +30,7 @@
 import org.apache.directory.shared.asn1.primitives.BitString;
 import org.apache.directory.shared.asn1.util.Asn1StringUtils;
 import org.apache.directory.shared.asn1.util.IntegerDecoder;
+import org.junit.Test;
 
 
 
@@ -43,9 +44,10 @@
 {
 
     /**
-     * Test the getNbBytes method
+     * Test the getNbBytes method for an int value
      */
-    public void testValueGetNbBytes()
+    @Test
+    public void testValueIntGetNbBytes()
     {
         assertEquals( 1, Value.getNbBytes( 0x00000000 ) );
         assertEquals( 1, Value.getNbBytes( 0x00000001 ) );
@@ -60,6 +62,36 @@
         assertEquals( 1, Value.getNbBytes( 0xFFFFFFFF ) );
     }
 
+
+    /**
+     * Test the getNbBytes method for a long value
+     */
+    @Test
+    public void testValueLongGetNbBytes()
+    {
+        assertEquals( 1, Value.getNbBytes( 0x0000000000000000L ) );
+        assertEquals( 1, Value.getNbBytes( 0x0000000000000001L ) );
+        assertEquals( 2, Value.getNbBytes( 0x00000000000000FFL ) );
+        assertEquals( 2, Value.getNbBytes( 0x0000000000000100L ) );
+        assertEquals( 3, Value.getNbBytes( 0x000000000000FFFFL ) );
+        assertEquals( 3, Value.getNbBytes( 0x0000000000010000L ) );
+        assertEquals( 4, Value.getNbBytes( 0x0000000000FFFFFFL ) );
+        assertEquals( 4, Value.getNbBytes( 0x0000000001000000L ) );
+        assertEquals( 5, Value.getNbBytes( 0x00000000FFFFFFFFL ) );
+        assertEquals( 5, Value.getNbBytes( 0x0000000100000000L ) );
+        assertEquals( 6, Value.getNbBytes( 0x000000FFFFFFFFFFL ) );
+        assertEquals( 6, Value.getNbBytes( 0x0000010000000000L ) );
+        assertEquals( 7, Value.getNbBytes( 0x0000FFFFFFFFFFFFL ) );
+        assertEquals( 7, Value.getNbBytes( 0x0001000000000000L ) );
+        assertEquals( 8, Value.getNbBytes( 0x00FFFFFFFFFFFFFFL ) );
+        assertEquals( 8, Value.getNbBytes( 0x0100000000000000L ) );
+        assertEquals( 1, Value.getNbBytes( -1L ) );
+        assertEquals( 8, Value.getNbBytes( 0x7FFFFFFFFFFFFFFFL ) );
+        assertEquals( 1, Value.getNbBytes( 0xFFFFFFFFFFFFFFFFL ) );
+    }
+
+    
+    @Test
     public void testGetBytes()
     {
         byte[] bb = Value.getBytes( 0x00000000 );
@@ -211,6 +243,8 @@
         assertEquals( 0x00, bb[3] );
     }
 
+
+    @Test
     public void testEncodeInt2Bytes()
     {
         byte[] encoded = Value.getBytes( 128 );
@@ -226,6 +260,7 @@
     }
 
 
+    @Test
     public void testEncodeInt3Bytes()
     {
 
@@ -237,6 +272,7 @@
     }
 
 
+    @Test
     public void testEncodeInt()
     {
         byte[] encoded = null;
@@ -256,6 +292,7 @@
     }
 
 
+    @Test
     public void testDecodeInt() throws Exception
     {
         byte[] encoded = null;
@@ -274,6 +311,8 @@
         }
     }
     
+
+    @Test
     public void testNewByteArrayValue()
     {
         byte[] bb = new byte[]{0x01, (byte)0xFF};
@@ -289,6 +328,7 @@
     }
 
     
+    @Test
     public void testEncodeBitString()
     {
         BitString bs = new BitString( 10 );