You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/03/26 06:25:24 UTC

svn commit: rev 9759 - incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber

Author: akarasulu
Date: Thu Mar 25 21:25:24 2004
New Revision: 9759

Modified:
   incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/Tuple.java
Log:
chunking changes

Modified: incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/Tuple.java
==============================================================================
--- incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/Tuple.java	(original)
+++ incubator/directory/snickers/branches/chunking/ber/src/java/org/apache/snickers/ber/Tuple.java	Thu Mar 25 21:25:24 2004
@@ -23,15 +23,15 @@
 
 
 /**
- * Simple TLV Tuple.  Because the length field is a primitive int it's maximum
- * value is 2,147,483,647 a single TLV's tuple cannot have a length over this
- * amount or a value size over 2 GB.
+ * TLV Tuple used by the value chunking decoder.  Because the length field is 
+ * a primitive int it's maximum value is 2,147,483,647 a single TLV's tuple 
+ * cannot have a length over this amount or a value size over 2 GB.
  * 
  * @author <a href="mailto:directory-dev@incubator.apache.org">
  * Apache Directory Project</a>
  * @version $Rev$
  */
-public class Tuple
+public class Tuple 
 {
     private static final ByteBuffer EMPTY_BUFFER =
         ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
@@ -63,7 +63,7 @@
     /** the length for this TLV tuple */
     int length = 0 ;
     /** the value for this TLV tuple */
-    Object value ;
+    ByteBuffer value = EMPTY_BUFFER ;
     
     /** tlv byte index */
     int index = Length.UNDEFINED ;
@@ -81,7 +81,6 @@
      */
     Tuple() 
     {
-        value = ArrayUtils.EMPTY_BYTE_ARRAY ;
     }
     
     
@@ -113,7 +112,7 @@
     {
         this.id = id ;
         this.length = length ;
-        value = ArrayUtils.EMPTY_BYTE_ARRAY ;
+        value = EMPTY_BUFFER ;
         isPrimitive = false ;
         
         if ( typeClass != null )
@@ -124,104 +123,21 @@
     
     
     /**
-     * Creates primitive application type tlv tuples.  This will be the primary
-     * constructor used to build primitives.
-     * 
-     * @param id the tag id of the tlv
-     * @param value the value portion for this Tuple
-     */
-    public Tuple( int id, byte[] value )
-    {
-        this( id, TypeClass.APPLICATION, true, value ) ;
-    }
-    
-    
-    /**
-     * Creates primitive application type tlv tuples.  This will be the primary
-     * constructor used to build primitives.
-     * 
-     * @param id the tag id of the tlv
-     * @param value the value portion for this Tuple
-     */
-    public Tuple( int id, ByteBuffer value )
-    {
-        this( id, TypeClass.APPLICATION, true, value ) ;
-    }
-    
-    
-    /**
-     * Creates an application type tlv with a set value and length.  
-     * 
-     * @param id the tag id of the tlv
-     * @param isPrimitive whether or not the tlv is primitive or constructed
-     * @param value the value portion for this Tuple
-     */
-    public Tuple( int id, boolean isPrimitive, byte[] value )
-    {
-        this( id, TypeClass.APPLICATION, isPrimitive, value ) ;
-    }
-    
-    
-    /**
-     * Creates an application type tlv with a set value and length.  
-     * 
-     * @param id the tag id of the tlv
-     * @param isPrimitive whether or not the tlv is primitive or constructed
-     * @param value the value portion for this Tuple
-     */
-    public Tuple( int id, boolean isPrimitive, ByteBuffer value )
-    {
-        this( id, TypeClass.APPLICATION, isPrimitive, value ) ;
-    }
-    
-    
-    /**
-     * Creates a tuple where the length is indefinate.  The tuple according to
-     * the BER encoding must be of the constructed type.
-     * 
-     * @param id the tag id of the tlv
-     * @param typeClass the type class for the tlv
-     * @param isPrimitive whether or not the tlv is primitive or constructed
-     * @param value the value portion for this Tuple
-     */
-    public Tuple( int id, TypeClass typeClass )
-    {
-        this.id = id ;
-        this.isPrimitive = false ;
-        value = ArrayUtils.EMPTY_BYTE_ARRAY ;
-        length = Length.INDEFINATE ;
-        
-        if ( typeClass != null )
-        {
-            this.typeClass = typeClass ;
-        }
-    }
-    
-    
-    /**
-     * Creates a tuple where every member is specified including the length 
-     * which is taken from the value array.
+     * Creates constructed application type tlv tuples.  Constructed TLV's with
+     * a definate length will use this constructor predominantly.
      * 
      * @param id the tag id of the tlv
-     * @param typeClass the type class for the tlv
-     * @param isPrimitive whether or not the tlv is primitive or constructed
-     * @param value the value portion for this Tuple
+     * @param length the length of the value which is the length of all the 
+     *      nested tuples.
+     * @param isPrimitive whether or not this Tuple is primitive or constructed
+     * @param typeClass the type class of this tlv tuple
      */
-    public Tuple( int id, TypeClass typeClass, boolean isPrimitive, 
-                  byte[] value )
+    public Tuple( int id, int length, boolean isPrimitive, TypeClass typeClass )
     {
         this.id = id ;
-        this.isPrimitive = isPrimitive ;
-        
-        if ( value != null )
-        {
-            this.value = value ;
-            this.length = value.length ;
-        }
-        else
-        {
-            this.value = ArrayUtils.EMPTY_BYTE_ARRAY ;
-        }
+        this.length = length ;
+        value = EMPTY_BUFFER ;
+        isPrimitive = false ;
         
         if ( typeClass != null )
         {
@@ -231,29 +147,18 @@
     
     
     /**
-     * Creates a tuple where every member is specified including the length 
-     * which is taken from the value array.
+     * Creates a tuple where the length is indefinate.  The tuple according to
+     * the BER encoding must be of the constructed type.
      * 
      * @param id the tag id of the tlv
      * @param typeClass the type class for the tlv
-     * @param isPrimitive whether or not the tlv is primitive or constructed
-     * @param value the value portion for this Tuple
      */
-    public Tuple( int id, TypeClass typeClass, boolean isPrimitive, 
-                  ByteBuffer value )
+    public Tuple( int id, TypeClass typeClass )
     {
         this.id = id ;
-        this.isPrimitive = isPrimitive ;
-        
-        if ( value != null )
-        {
-            this.value = value ;
-            this.length = value.remaining() ;
-        }
-        else
-        {
-            this.value = EMPTY_BUFFER ;
-        }
+        this.isPrimitive = false ;
+        value = EMPTY_BUFFER ;
+        length = Length.INDEFINATE ;
         
         if ( typeClass != null )
         {
@@ -341,7 +246,7 @@
      * 
      * @return the value field for this TLV Tuple
      */
-    public Object getValue()
+    public ByteBuffer getValue()
     {
         return value ;
     }
@@ -382,16 +287,17 @@
         this.isPrimitive = true ;
         this.length = Length.UNDEFINED ;
         this.typeClass = TypeClass.APPLICATION ;
-        this.value = ArrayUtils.EMPTY_BYTE_ARRAY ;
+        this.value = EMPTY_BUFFER ;
         this.valueIndex = Length.UNDEFINED ;
     }
 
     
     /**
-     * Does not take into account the index or the valueIndex values when
+     * Does not take into account the value, index or the valueIndex values when
      * checking for equality.  Technically if both are being constructed by
      * the decoder then they should only be equal when these values are equal
-     * because the tag, length or value would not be correct.
+     * because the tag, length or value would not be correct.  Plus since this
+     * is a chunking tuple the value may only be a part of the final value.
      *
      * @see java.lang.Object#equals(java.lang.Object)
      */
@@ -426,41 +332,7 @@
                 return false ;
             }
             
-            if ( t.value == null && value == null )
-            {
-                return true ;
-            }
-            else if ( t.value == null || value == null )
-            {
-                return false ;
-            }
-            else if ( t.value == value )
-            {
-                return true ;
-            }
-            else if ( t.value.getClass() != value.getClass() )
-            {
-                if ( t.value instanceof ByteBuffer && value instanceof byte[] )
-                {
-                    ByteBuffer bb = ByteBuffer.wrap( ( byte[] ) value ) ;
-                    return bb.equals( t.value ) ;
-                }
-                else if ( value instanceof ByteBuffer && 
-                            t.value instanceof byte[] )
-                {
-                    ByteBuffer bb = ByteBuffer.wrap( ( byte[] ) t.value ) ;
-                    return bb.equals( value ) ;
-                }
-                
-                return false ;
-            }
-            else if ( t.value instanceof byte[] &&
-                        ArrayUtils.isEquals( t.value, value ) )
-            {  
-                return true ;
-            }
-            
-            return t.value.equals( value ) ;
+            return true ;
         }
         
         return false ;
@@ -479,27 +351,15 @@
         t.typeClass = typeClass ;
         t.length = length ;
         
-        if ( value instanceof byte[] )
-        {
-            byte[] bites = ( byte[] ) value ;
-            t.value = bites.clone() ;
-        }
-        else if ( value instanceof ByteBuffer )
-        {
-            ByteBuffer bb = ( ByteBuffer ) value ;
-            ByteBuffer cloned = ByteBuffer.allocate( bb.capacity() ) ;
-            int oldPos = bb.position() ;
-            bb.rewind() ;
-            cloned.put( bb ) ;
-            cloned.limit( bb.limit() ) ;
-            bb.position( oldPos ) ;
-            cloned.rewind() ;
-            t.value = cloned ;
-        }
-        else
-        {    
-            t.value = value ;
-        }
+        ByteBuffer bb = ( ByteBuffer ) value ;
+        ByteBuffer cloned = ByteBuffer.allocate( bb.capacity() ) ;
+        int oldPos = bb.position() ;
+        bb.rewind() ;
+        cloned.put( bb ) ;
+        cloned.limit( bb.limit() ) ;
+        bb.position( oldPos ) ;
+        cloned.rewind() ;
+        t.value = cloned ;
         
         t.index = index ;
         t.valueIndex = valueIndex ;
@@ -514,67 +374,23 @@
     
     
     /**
-     * If this is a primitive TLV then it is encoded fully.  If it is not then
+     * If this is a primitive TLV then the valueBytes argument is used to 
+     * produce an encoded image of this TLV.  If it is constructed then
      * only the TL part of the tuple is encoded leaving the value to be encoded
      * by the set of child TLVs.
      * 
      * @return partial encoded image if constructed or complete TLV if primitive
      */
-    public byte[] toEncodedArray()
-    {
-        byte[] octets = null ;
-        int tagLength = getTagLength() ;
-        int lengthLength = getLengthLength() ;
-        int valueLength = 0 ;
-        int total = tagLength + lengthLength ;
-
-        if ( isPrimitive )
-        {
-            valueLength = ( ( byte[] ) value ).length ;
-            total += valueLength ;
-        }
-        
-        octets = new byte[total] ;
-        setTag( octets, tagLength ) ;
-        setLength( octets, tagLength, lengthLength ) ;
-        
-        if ( isPrimitive )
-        {
-            int destPos = tagLength + lengthLength ;
-            System.arraycopy( value, 0, octets, destPos, valueLength ) ;
-        }
-        
-        return octets ;
-    }
-    
-    
-    /**
-     * If this is a primitive TLV then it is encoded fully.  If it is not then
-     * only the TL part of the tuple is encoded leaving the value to be encoded
-     * by the set of child TLVs.
-     * 
-     * @return partial encoded image if constructed or complete TLV if primitive
-     */
-    public ByteBuffer toEncodedBuffer()
+    public ByteBuffer toEncodedBuffer( ByteBuffer valueBytes )
     {
         ByteBuffer octets = null ;
         int tagLength = getTagLength() ;
         int lengthLength = getLengthLength() ;
-        int valueLength = 0 ;
         int total = tagLength + lengthLength ;
 
         if ( isPrimitive )
         {
-            if ( value instanceof ByteBuffer )
-            {
-                valueLength = ( ( ByteBuffer ) value ).remaining() ;
-            }
-            else if ( value instanceof byte[] )
-            {    
-                valueLength = ( ( byte[] ) value ).length ;
-            }
-            
-            total += valueLength ;
+            total += ( ( ByteBuffer ) valueBytes ).remaining() ;
         }
         
         octets = ByteBuffer.allocate( total ) ;
@@ -583,16 +399,7 @@
         
         if ( isPrimitive )
         {
-            int destPos = tagLength + lengthLength ;
-            
-            if ( value instanceof ByteBuffer )
-            {    
-                octets.put( ( ByteBuffer ) value ) ;
-            }
-            else if ( value instanceof byte[] )
-            {    
-                octets.put( ( byte [] ) value ) ;
-            }
+            octets.put( valueBytes ) ;
         }
         
         return ( ByteBuffer ) octets.flip() ;
@@ -600,118 +407,6 @@
     
     
     /**
-     * Sets the tag section within the array at the start of the array.
-     * 
-     * @param octets the array of bytes to set the tag in
-     * @param tagLength the length of the tag section
-     */
-    void setTag( byte[] octets, int tagLength )
-    {
-        octets[0] = ( byte ) typeClass.getValue() ;
-        
-        if ( ! isPrimitive )
-        {
-            octets[0] |= BIT_5 ;
-        }
-        
-        if ( tagLength == 1 )
-        {
-            octets[0] |= id ;
-        }
-        else
-        {
-            octets[0] |= Tag.SHORT_MASK ;
-        }
-        
-        if ( tagLength >= 2 )
-        {
-            octets[1] = ( byte ) ( ( int ) 0x7f & id ) ;
-            
-            if ( tagLength > 2 )
-            {
-                octets[1] |= BIT_7 ;
-            }
-        }
-        else
-        {
-            return ;
-        }
-        
-        /* 
-         * Take bits [8-14] (1 based bit indexing) inclusive in id and set the
-         * value for the second byte to this when shifted down 7 positions so
-         * we need the following mask:
-         * 
-         * 0011 1111 1000 0000 => 0x3f80
-         * 
-         */
-        if ( tagLength >= 3 )
-        {
-            octets[2] = ( byte ) ( ( ( int ) 0x3f80 & id ) >> 7 ) ;
-            
-            if ( tagLength > 3 )
-            {
-                octets[2] |= BIT_7 ;
-            }
-        }
-        else
-        {
-            return ;
-        }
-        
-        /* 
-         * Take bits [15-21] (1 based bit indexing) inclusive in id and set the
-         * value for the second byte to this when shifted down 14 positions so
-         * we need the following mask:
-         * 
-         * 0001 1111 1100 0000 0000 0000 => 0x1fc000
-         * 
-         */
-        if ( tagLength >= 4 )
-        {
-            octets[3] = ( byte ) ( ( ( int ) 0x1fc000 & id ) >> 14 ) ;
-            
-            if ( tagLength > 4 )
-            {
-                octets[3] |= BIT_7 ;
-            }
-        }
-        else
-        {
-            return ;
-        }
-        
-        /* 
-         * Take bits [22-28] (1 based bit indexing) inclusive in id and set the
-         * value for the second byte to this when shifted down 21 positions so
-         * we need the following mask:
-         * 
-         * 0000 1111 1110 0000 0000 0000 0000 0000 => 0x0fe00000
-         * 
-         */
-        if ( tagLength >= 5 )
-        {
-            octets[4] = ( byte ) ( ( ( int ) 0x0fe00000 & id ) >> 21 ) ;
-            
-            if ( tagLength > 5 )
-            {
-                octets[4] |= BIT_7 ;
-            }
-        }
-        else
-        {
-            return ;
-        }
-        
-        if ( tagLength >= 6 )
-        {    
-            throw new IllegalArgumentException( "cannot support id's as large "
-                    + "as " + id + " unless we start using longs for the id" ) ;
-        }
-    }
-
-
-    /**
      * Sets the tag section within the buffer.
      * 
      * @param octets the buffer to set the tag in
@@ -885,84 +580,6 @@
         if ( lengthBytes >= 5 )
         {
             octets.put( ( byte ) ( ( 0xff000000 & length ) >> 24 ) ) ;
-        }
-        else
-        {
-            return ;
-        }
-        
-        if ( lengthBytes >= 6 )
-        {    
-            throw new IllegalArgumentException( "cannot support lengths as "
-                    + "large as " + length 
-                    + " unless we start using longs for the length" ) ;
-        }
-    }
-
-
-    /**
-     * Sets the length bytes.
-     * 
-     * @param octets the byte [] to set length in
-     * @param offset the offset in the array to start the length section in
-     * @param lengthBytes the number bytes for the length section
-     */
-    void setLength( byte[] octets, int offset, int lengthBytes )
-    {
-        if ( length == Length.INDEFINATE )
-        {
-            octets[offset] = ( byte ) BIT_7 ;
-            return ;
-        }
-        else if ( lengthBytes == 1 )
-        {
-            octets[offset] = ( byte ) length ;
-            return ;
-        }
-        else
-        {
-            /*
-             * the lengthBytes argument is the number of octets for the L field
-             * total which for the long form includes the first octet for the 
-             * length of length (N) value where N < 127.  Technically with the 
-             * 7 bits we can specify an N of up to 127 but this value of N is 
-             * reserved.  Anyway below we subtract one from lengthBytes to get
-             * N which is set as the last 7 bits of the first octet of the L
-             * field. 
-             */
-            octets[offset] |= BIT_7 | lengthBytes - 1 ;
-        }
-        
-        if ( lengthBytes >= 2 )
-        {
-            octets[offset + 1] =  ( byte ) ( 0xff & length ) ;
-        }
-        else
-        {
-            return ;
-        }
-        
-        if ( lengthBytes >= 3 )
-        {
-            octets[offset + 2] = ( byte ) ( ( 0xff00 & length ) >> 8 ) ;
-        }
-        else
-        {
-            return ;
-        }
-        
-        if ( lengthBytes >= 4 )
-        {
-            octets[offset + 3] = ( byte ) ( ( 0xff0000 & length ) >> 16 ) ;
-        }
-        else
-        {
-            return ;
-        }
-        
-        if ( lengthBytes >= 5 )
-        {
-            octets[offset + 4] = ( byte ) ( ( 0xff000000 & length ) >> 24 ) ;
         }
         else
         {