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/27 02:11:12 UTC

svn commit: rev 9765 - in incubator/directory/snickers/trunk: . ber ber/src/java/org/apache/snickers ber/src/java/org/apache/snickers/asn ber/src/java/org/apache/snickers/ber ber/src/test/org/apache/snickers ber/src/test/org/apache/snickers/ber codec-stateful codec-stateful/src/java/org/apache/commons/codec/stateful xdocs xdocs/ber-codec xdocs/stub-compiler

Author: akarasulu
Date: Fri Mar 26 17:11:11 2004
New Revision: 9765

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractBufferedDecoderTestCase.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/java/org/apache/snickers/ber/AbstractBufferedDecoderTestCase.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractChunkingBERDecoderTestCase.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/java/org/apache/snickers/ber/AbstractChunkingBERDecoderTestCase.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERBufferedDecoder.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/java/org/apache/snickers/ber/BERBufferedDecoder.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/ChunkingBERDecoder.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/java/org/apache/snickers/ber/ChunkingBERDecoder.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/ChunkingTuple.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/java/org/apache/snickers/ber/ChunkingTuple.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERBufferedDecoderTest.java   (props changed)
      - copied unchanged from rev 9764, incubator/directory/snickers/branches/buffer-branch/ber/src/test/org/apache/snickers/ber/BERBufferedDecoderTest.java
Modified:
   incubator/directory/snickers/trunk/ber/berlib.properties   (props changed)
   incubator/directory/snickers/trunk/ber/project.properties   (props changed)
   incubator/directory/snickers/trunk/ber/project.xml   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/SnickersDecoder.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/BERDecoderUtils.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/asn/Primitive.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallback.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderMonitor.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderState.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Length.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/LengthDecoder.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/MutableTupleNode.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tag.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TagDecoder.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleNode.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleTreeAnalyzer.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleTreeDecoder.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TypeClass.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/SnickersDecoderTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/AbstractDecoderTestCaseTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderCallbackAdapterTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderStateTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/ConstructedTLVTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/DefaultMutableTupleNodeTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/EncodeDecodeTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/IntStackTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/LdapMessageTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/LengthTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/MultiByteLengthTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/MultiByteTagTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/ObjectVersePrimitiveTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/SimplePrimitiveTLVTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/SingleByteLengthTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/SingleByteTagTests.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TagTest.java   (props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTest.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java   (contents, props changed)
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TypeClassTest.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/project.properties   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/project.xml   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/AbstractStatefulDecoder.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/CallbackHistory.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/DecoderCallback.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/DecoderMonitor.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/DecoderMonitorAdapter.java   (props changed)
   incubator/directory/snickers/trunk/codec-stateful/src/java/org/apache/commons/codec/stateful/StatefulDecoder.java   (props changed)
   incubator/directory/snickers/trunk/maven.xml   (props changed)
   incubator/directory/snickers/trunk/project.properties   (props changed)
   incubator/directory/snickers/trunk/project.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/ber-codec/design.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/ber-codec/eureka.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/ber-codec/index.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/ber-codec/navigation.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/index.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/navigation.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/stub-compiler/index.xml   (props changed)
   incubator/directory/snickers/trunk/xdocs/stub-compiler/navigation.xml   (props changed)
Log:
merged buffer-branch changes r9743:9764 to trunk

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/AbstractDecoderTestCase.java	Fri Mar 26 17:11:11 2004
@@ -215,7 +215,7 @@
      */
     public Tuple decode( Tuple tlv ) throws DecoderException
     {
-        ByteBuffer buf = ByteBuffer.wrap( tlv.encode() ) ;
+        ByteBuffer buf = ByteBuffer.wrap( tlv.toEncodedArray() ) ;
         int lastSize = tlvList.size() ;
         decoder.decode( buf ) ;
         
@@ -302,6 +302,15 @@
     {
         assertTrue( decoder.getCurrentTuple().equals( tlv ) ) ;
         assertEquals( BERDecoderState.TAG, decoder.getState() ) ;
+    }
+    
+    
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
+     * org.apache.snickers.ber.Tuple)
+     */
+    public void partialValueDecoded( Tuple tlv )
+    {
     }
 
 

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallback.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallback.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallback.java	Fri Mar 26 17:11:11 2004
@@ -55,4 +55,11 @@
      * @param tlv the TLV tuple
      */
     void lengthDecoded( Tuple tlv ) ;
+    
+    /**
+     * Method used to recieve notification that a part of the value was decoded.
+     * 
+     * @param tlv the TLV tuple 
+     */
+    void partialValueDecoded( Tuple tlv ) ;
 }

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoderCallbackAdapter.java	Fri Mar 26 17:11:11 2004
@@ -48,6 +48,15 @@
 
     
     /* (non-Javadoc)
+     * @see org.apache.snickers.ber.BERDecoderCallback#partialValueDecoded(
+     * org.apache.snickers.ber.Tuple)
+     */
+    public void partialValueDecoded(Tuple tlv)
+    {
+    }
+
+    
+    /* (non-Javadoc)
      * @see org.apache.commons.codec.stateful.DecoderCallback#decodeOccurred(
      * org.apache.commons.codec.stateful.StatefulDecoder, java.lang.Object)
      */

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/DefaultMutableTupleNode.java	Fri Mar 26 17:11:11 2004
@@ -262,7 +262,7 @@
      */
     public void encode( ByteBuffer dest )
     {
-        dest.put( tuple.encode() ) ;
+        dest.put( tuple.toEncodedArray() ) ;
         
         if ( tuple.isPrimitive() )
         {

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java	Fri Mar 26 17:11:11 2004
@@ -17,15 +17,15 @@
 package org.apache.snickers.ber ;
 
 
+import java.nio.ByteBuffer ;
+
 import org.apache.commons.lang.ArrayUtils ;
-import org.apache.commons.codec.binary.Binary ;
 
 
 /**
- * Simple TLV Tuple.  Because the length fields is a primitive int it's maximum
+ * 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.  I don't think we'll be hitting this soon
- * especially with the value size protection feature planned for the decoder.
+ * amount or a value size over 2 GB.
  * 
  * @author <a href="mailto:directory-dev@incubator.apache.org">
  * Apache Directory Project</a>
@@ -33,10 +33,16 @@
  */
 public class Tuple
 {
-    /** precalculated left shift of 1 by 7 places */
-    static final int BIT_6 = 1 << 7 ;
-    /** precalculated left shift of 1 by 8 places */
-    static final int BIT_7 = 1 << 8 ;
+    private static final ByteBuffer EMPTY_BUFFER =
+        ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
+    
+    /** mask for bit 5 with 0-based index */
+    private static final int BIT_5 = 0x20 ;
+    /** mask for bit 6 with 0-based index */
+    private static final int BIT_6 = 0x40 ;
+    /** mask for bit 7 with 0-based index */
+    private static final int BIT_7 = 0x80 ;
+    
     /** precalculated left shift of 1 by 14 places */
     static final int BIT_13 = 1 << 14 ;
     /** precalculated left shift of 1 by 16 places */
@@ -57,7 +63,7 @@
     /** the length for this TLV tuple */
     int length = 0 ;
     /** the value for this TLV tuple */
-    Object value = ArrayUtils.EMPTY_BYTE_ARRAY ;
+    Object value ;
     
     /** tlv byte index */
     int index = Length.UNDEFINED ;
@@ -74,7 +80,8 @@
      * Empty do nothing tuple.
      */
     Tuple() 
-    { 
+    {
+        value = ArrayUtils.EMPTY_BYTE_ARRAY ;
     }
     
     
@@ -130,6 +137,19 @@
     
     
     /**
+     * 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
@@ -143,6 +163,19 @@
     
     
     /**
+     * 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.
      * 
@@ -185,6 +218,42 @@
             this.value = value ;
             this.length = value.length ;
         }
+        else
+        {
+            this.value = ArrayUtils.EMPTY_BYTE_ARRAY ;
+        }
+        
+        if ( typeClass != null )
+        {
+            this.typeClass = typeClass ;
+        }
+    }
+    
+    
+    /**
+     * Creates a tuple where every member is specified including the length 
+     * which is taken from the value array.
+     * 
+     * @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 )
+    {
+        this.id = id ;
+        this.isPrimitive = isPrimitive ;
+        
+        if ( value != null )
+        {
+            this.value = value ;
+            this.length = value.remaining() ;
+        }
+        else
+        {
+            this.value = EMPTY_BUFFER ;
+        }
         
         if ( typeClass != null )
         {
@@ -359,14 +428,39 @@
             
             if ( t.value == null && value == null )
             {
-                
+                return true ;
+            }
+            else if ( t.value == null || value == null )
+            {
+                return false ;
             }
-            else if ( ! ArrayUtils.isEquals( t.value, value ) )
+            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 true ;
+            return t.value.equals( value ) ;
         }
         
         return false ;
@@ -384,7 +478,29 @@
         t.isPrimitive = isPrimitive ;
         t.typeClass = typeClass ;
         t.length = length ;
-        t.value = value ;
+        
+        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 ;
+        }
+        
         t.index = index ;
         t.valueIndex = valueIndex ;
         
@@ -404,7 +520,7 @@
      * 
      * @return partial encoded image if constructed or complete TLV if primitive
      */
-    public byte[] encode()
+    public byte[] toEncodedArray()
     {
         byte[] octets = null ;
         int tagLength = getTagLength() ;
@@ -433,6 +549,57 @@
     
     
     /**
+     * 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()
+    {
+        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 ;
+        }
+        
+        octets = ByteBuffer.allocate( total ) ;
+        setTag( octets, tagLength ) ;
+        setLength( octets, lengthLength ) ;
+        
+        if ( isPrimitive )
+        {
+            int destPos = tagLength + lengthLength ;
+            
+            if ( value instanceof ByteBuffer )
+            {    
+                octets.put( ( ByteBuffer ) value ) ;
+            }
+            else if ( value instanceof byte[] )
+            {    
+                octets.put( ( byte [] ) value ) ;
+            }
+        }
+        
+        return ( ByteBuffer ) octets.flip() ;
+    }
+    
+    
+    /**
      * Sets the tag section within the array at the start of the array.
      * 
      * @param octets the array of bytes to set the tag in
@@ -444,7 +611,7 @@
         
         if ( ! isPrimitive )
         {
-            octets[0] |= Binary.BIT_5 ;
+            octets[0] |= BIT_5 ;
         }
         
         if ( tagLength == 1 )
@@ -458,11 +625,11 @@
         
         if ( tagLength >= 2 )
         {
-            octets[1] = ( byte ) ( ( int ) 0x0000007f & id ) ;
+            octets[1] = ( byte ) ( ( int ) 0x7f & id ) ;
             
             if ( tagLength > 2 )
             {
-                octets[1] |= Binary.BIT_7 ;
+                octets[1] |= BIT_7 ;
             }
         }
         else
@@ -484,7 +651,7 @@
             
             if ( tagLength > 3 )
             {
-                octets[2] |= Binary.BIT_7 ;
+                octets[2] |= BIT_7 ;
             }
         }
         else
@@ -506,7 +673,7 @@
             
             if ( tagLength > 4 )
             {
-                octets[3] |= Binary.BIT_7 ;
+                octets[3] |= BIT_7 ;
             }
         }
         else
@@ -528,7 +695,118 @@
             
             if ( tagLength > 5 )
             {
-                octets[4] |= Binary.BIT_7 ;
+                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
+     * @param tagLength the length of the tag section
+     */
+    void setTag( ByteBuffer octets, int tagLength )
+    {
+        octets.put( ( byte ) typeClass.getValue() ) ;
+        
+        if ( ! isPrimitive )
+        {
+            octets.put( 0, ( byte ) ( octets.get( 0 ) | BIT_5 ) ) ;
+        }
+        
+        if ( tagLength == 1 )
+        {
+            octets.put( 0, ( byte ) ( octets.get( 0 ) | id ) ) ;
+            return ;
+        }
+        
+        octets.put( 0, ( byte ) ( octets.get( 0 ) | Tag.SHORT_MASK ) ) ;
+        
+        if ( tagLength >= 2 )
+        {
+            octets.put( ( byte ) ( ( int ) 0x7f & id ) ) ;
+            
+            if ( tagLength > 2 )
+            {
+                octets.put( 1, ( byte ) ( octets.get( 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.put( ( byte ) ( ( ( int ) 0x3f80 & id ) >> 7 ) ) ;
+            
+            if ( tagLength > 3 )
+            {
+                octets.put( 2, ( byte ) ( octets.get( 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.put( ( byte ) ( ( ( int ) 0x1fc000 & id ) >> 14 ) ) ;
+            
+            if ( tagLength > 4 )
+            {
+                octets.put( 3, ( byte ) ( octets.get( 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.put( ( byte ) ( ( ( int ) 0x0fe00000 & id ) >> 21 ) ) ;
+            
+            if ( tagLength > 5 )
+            {
+                octets.put( 4, ( byte ) ( octets.get( 4 ) | BIT_7 ) ) ;
             }
         }
         else
@@ -551,20 +829,108 @@
      * @param offset the offset in the array to start the length section in
      * @param lengthBytes the number bytes for the length section
      */
+    void setLength( ByteBuffer octets, int lengthBytes )
+    {
+        if ( length == Length.INDEFINATE )
+        {
+            octets.put( ( byte ) BIT_7 ) ;
+            return ;
+        }
+        else if ( lengthBytes == 1 )
+        {
+            octets.put( ( 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.put( ( byte ) ( BIT_7 | ( lengthBytes - 1 ) ) ) ;
+        }
+        
+        if ( lengthBytes >= 2 )
+        {
+            octets.put( ( byte ) ( 0xff & length ) ) ;
+        }
+        else
+        {
+            return ;
+        }
+        
+        if ( lengthBytes >= 3 )
+        {
+            octets.put( ( byte ) ( ( 0xff00 & length ) >> 8 ) ) ;
+        }
+        else
+        {
+            return ;
+        }
+        
+        if ( lengthBytes >= 4 )
+        {
+            octets.put( ( byte ) ( ( 0xff0000 & length ) >> 16 ) ) ;
+        }
+        else
+        {
+            return ;
+        }
+        
+        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] |= BIT_6 ;
+            octets[offset] = ( byte ) BIT_7 ;
             return ;
         }
         else if ( lengthBytes == 1 )
         {
-            octets[offset] |= length ;
+            octets[offset] = ( byte ) length ;
+            return ;
         }
         else
         {
-            octets[offset] |= Binary.BIT_7 | lengthBytes - 1 ;
+            /*
+             * 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 )
@@ -665,12 +1031,12 @@
                     + " - unless we start using longs for"
                     + " the id there you've hit a limitation" ) ;
         }
-        else if ( length < BIT_6 )
+        else if ( length < BIT_7 )
         {
             return 1 ;
         }
-        else if ( length < BIT_7 )
-        {    
+        else if ( length < 256 )
+        {
             return 2 ;
         }
         else if ( length < BIT_15 )

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleTreeDecoder.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleTreeDecoder.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TupleTreeDecoder.java	Fri Mar 26 17:11:11 2004
@@ -57,6 +57,8 @@
         BERDecoderCallback berCallback = new BERDecoderCallback()
         {
             public void tagDecoded( Tuple tlv ) { }
+            
+            public void partialValueDecoded( Tuple tlv ) { }
         
             public void lengthDecoded( Tuple tlv )
             {

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/BERDecoderTest.java	Fri Mar 26 17:11:11 2004
@@ -140,7 +140,7 @@
         Tuple top = new Tuple( 1, t0.size() + t1.size() ) ;
         assertTrue( decode( top ).equals( top ) ) ;
 
-        byte[] all = t0.encode() ;
+        byte[] all = t0.toEncodedArray() ;
         byte[][] fragments = fragment( all, 10 ) ;
         Tuple decoded = null ;
         

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTest.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTest.java	Fri Mar 26 17:11:11 2004
@@ -17,6 +17,8 @@
 package org.apache.snickers.ber ;
 
 
+import java.nio.ByteBuffer;
+
 import org.apache.commons.lang.ArrayUtils ;
 import org.apache.commons.codec.binary.Binary ;
 
@@ -126,7 +128,7 @@
         assertEquals( 0, t.getLength() ) ;
         assertEquals( ArrayUtils.EMPTY_BYTE_ARRAY, t.getValue() ) ;
 
-        t = new Tuple( 2, TypeClass.PRIVATE, true, null ) ;
+        t = new Tuple( 2, TypeClass.PRIVATE, true, (byte[]) null ) ;
         assertEquals( 2, t.getId() ) ;
         assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
         assertEquals( true, t.isPrimitive() ) ;
@@ -141,7 +143,7 @@
         assertEquals( 7, t.getLength() ) ;
         assertEquals( bites, t.getValue() ) ;
         
-        t = new Tuple( 2, null, false,  null ) ; 
+        t = new Tuple( 2, null, false, (byte[]) null ) ; 
         assertEquals( 2, t.getId() ) ;
         assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
         assertEquals( false, t.isPrimitive() ) ;
@@ -152,6 +154,45 @@
     /*
      * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
      */
+    public void testTupleintTypeClassbooleanBuffer()
+    {
+        Tuple t = new Tuple( 2, TypeClass.PRIVATE, true, 
+                ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
+        assertEquals( true, t.isPrimitive() ) ;
+        assertEquals( 0, t.getLength() ) ;
+        assertEquals( ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ), 
+                t.getValue() ) ;
+
+        t = new Tuple( 2, TypeClass.PRIVATE, true, (ByteBuffer) null ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
+        assertEquals( true, t.isPrimitive() ) ;
+        assertEquals( 0, t.getLength() ) ;
+        assertEquals( ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ), 
+                t.getValue() ) ;
+
+        ByteBuffer bites = ByteBuffer.allocate( 7 ) ;
+        t = new Tuple( 2, (TypeClass) null, false, bites ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( false, t.isPrimitive() ) ;
+        assertEquals( 7, t.getLength() ) ;
+        assertEquals( bites, t.getValue() ) ;
+        
+        t = new Tuple( 2, null, false, (ByteBuffer) null ) ; 
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( false, t.isPrimitive() ) ;
+        assertEquals( 0, t.getLength() ) ;
+        assertEquals( ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ), 
+                t.getValue() ) ;
+    }
+
+    /*
+     * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
+     */
     public void testTupleintbyteArray()
     {
         Tuple t = new Tuple( 2, ArrayUtils.EMPTY_BYTE_ARRAY ) ;
@@ -173,6 +214,29 @@
     /*
      * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
      */
+    public void testTupleintBuffer()
+    {
+        Tuple t = new Tuple( 2, 
+                ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( true, t.isPrimitive() ) ;
+        assertEquals( 0, t.getLength() ) ;
+        assertEquals( ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ), 
+                t.getValue() ) ;
+        
+        ByteBuffer bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( 2, bites ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( true, t.isPrimitive() ) ;
+        assertEquals( 5, t.getLength() ) ;
+        assertEquals( bites, t.getValue() ) ;
+    }
+
+    /*
+     * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
+     */
     public void testTupleintbooleanbyteArray()
     {
         Tuple t = new Tuple( 2, false, ArrayUtils.EMPTY_BYTE_ARRAY ) ;
@@ -194,6 +258,29 @@
     /*
      * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
      */
+    public void testTupleintbooleanBuffer()
+    {
+        Tuple t = new Tuple( 2, false, 
+                ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( false, t.isPrimitive() ) ;
+        assertEquals( 0, t.getLength() ) ;
+        assertEquals( ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ), 
+                t.getValue() ) ;
+        
+        ByteBuffer bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( 2, false, bites ) ;
+        assertEquals( 2, t.getId() ) ;
+        assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
+        assertEquals( false, t.isPrimitive() ) ;
+        assertEquals( 5, t.getLength() ) ;
+        assertEquals( bites, t.getValue() ) ;
+    }
+
+    /*
+     * Class to test for void Tuple(int, int, TypeClass, boolean, byte[])
+     */
     public void testTupleintTypeClass()
     {
         Tuple t = new Tuple( 2, TypeClass.PRIVATE ) ;
@@ -229,7 +316,7 @@
         t.id = 32 ;
         assertEquals( 3, t.size() ) ;
         t.id = 127 ;
-        assertEquals( 3, t.size() ) ;
+        assertEquals( 4, t.size() ) ;
         t.id = 128 ;
         assertEquals( 4, t.size() ) ;
         t.id = 1 << 14 ;
@@ -422,14 +509,14 @@
         assertTrue( t.equals( t.clone() ) ) ;
     }
 
-    public void testEncodeConstructed()
+    public void testToEncodedArrayConstructed()
     {
         Tuple t = null ;
         byte[] encoded ;
         String binary ;
         
         t = new Tuple( 0, 0 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -437,7 +524,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 2, 0 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -445,7 +532,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 30, 0 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -453,7 +540,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 31, 0 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -462,7 +549,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 128, 0 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -472,7 +559,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 128, 127 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "01111111" +
@@ -482,7 +569,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 128, 128 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "10000000" +
@@ -493,7 +580,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 128, 255 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "11111111" +
@@ -504,7 +591,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( 128, 256 ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000001" +
@@ -516,7 +603,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
 
         t = new Tuple( Tuple.BIT_27-1, Integer.MAX_VALUE ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( Binary.toAsciiString( Integer.MAX_VALUE ) + 
                 "10000100" +
@@ -528,7 +615,7 @@
                 , Binary.toAsciiString( encoded ) ) ;
     }
 
-    public void testEncodePrimitive()
+    public void testToEncodedArrayPrimitive()
     {
         Tuple t = null ;
         byte[] encoded ;
@@ -537,7 +624,7 @@
         
         data = new byte[0] ;
         t = new Tuple( 0, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -546,7 +633,7 @@
 
         data = new byte[0] ;
         t = new Tuple( 2, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -555,7 +642,7 @@
 
         data = new byte[0] ;
         t = new Tuple( 30, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -564,7 +651,7 @@
 
         data = new byte[0] ;
         t = new Tuple( 31, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -574,7 +661,7 @@
 
         data = new byte[0] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -585,7 +672,7 @@
 
         data = new byte[1] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( 
                 "00000000" +
@@ -597,7 +684,7 @@
 
         data = new byte[127] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( Binary.toAsciiString( data ) +
                 "01111111" +
@@ -608,7 +695,7 @@
 
         data = new byte[128] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( Binary.toAsciiString( data ) +
                 "10000000" +
@@ -620,7 +707,7 @@
 
         data = new byte[255] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( Binary.toAsciiString( data ) +
                 "11111111" +
@@ -632,7 +719,7 @@
 
         data = new byte[256] ;
         t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
-        encoded = t.encode() ;
+        encoded = t.toEncodedArray() ;
         binary = Binary.toAsciiString( encoded ) ;
         assertEquals( Binary.toAsciiString( data ) + 
                 "00000001" +
@@ -644,7 +731,355 @@
                 , Binary.toAsciiString( encoded ) ) ;
     }
 
-    public void testSetTag()
+    
+    public void testToEncodedBufferConstructed()
+    {
+        Tuple t = null ;
+        ByteBuffer encoded ;
+        String binary ;
+        
+        t = new Tuple( 0, 0 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01100000"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 2, 0 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01100010"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 30, 0 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01111110"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 31, 0 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "00011111" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 128, 0 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "00000001" +
+                "10000000" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 128, 127 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "01111111" +
+                "00000001" +
+                "10000000" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 128, 128 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "10000000" +
+                "10000001" +
+                "00000001" +
+                "10000000" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 128, 255 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "11111111" +
+                "10000001" +
+                "00000001" +
+                "10000000" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( 128, 256 ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000001" +
+                "00000000" +
+                "10000010" +
+                "00000001" +
+                "10000000" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+
+        t = new Tuple( Tuple.BIT_27-1, Integer.MAX_VALUE ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( Binary.toAsciiString( Integer.MAX_VALUE ) + 
+                "10000100" +
+                "01111111" +
+                "11111111" +
+                "11111111" +
+                "11111111" +
+                "01111111"
+                , toAsciiString( encoded ) ) ;
+    }
+
+    public void testToEncodedBufferPrimitive()
+    {
+        Tuple t = null ;
+        ByteBuffer encoded ;
+        byte[] data ;
+        String binary ;
+        
+        data = new byte[0] ;
+        t = new Tuple( 0, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01000000"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[0] ;
+        t = new Tuple( 2, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01000010"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[0] ;
+        t = new Tuple( 30, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "01011110"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[0] ;
+        t = new Tuple( 31, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "00011111" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[0] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[1] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( 
+                "00000000" +
+                "00000001" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[127] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( Binary.toAsciiString( data ) +
+                "01111111" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[128] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( Binary.toAsciiString( data ) +
+                "10000000" +
+                "10000001" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[255] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( Binary.toAsciiString( data ) +
+                "11111111" +
+                "10000001" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+
+        data = new byte[256] ;
+        t = new Tuple( 128, TypeClass.APPLICATION, true, data ) ;
+        encoded = t.toEncodedBuffer() ;
+        binary = toAsciiString( encoded ) ;
+        assertEquals( Binary.toAsciiString( data ) + 
+                "00000001" +
+                "00000000" +
+                "10000010" +
+                "00000001" +
+                "10000000" +
+                "01011111"
+                , toAsciiString( encoded ) ) ;
+    }
+
+    
+    public String toAsciiString( ByteBuffer buf )
+    {
+        return Binary.toAsciiString( buf.array() ) ;
+    }
+    
+    
+    public void testSetTagBufferint()
+    {
+        ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+        Tuple t = new Tuple( 0, 0 ) ;
+        t.setTag( bites, 1 ) ;
+        String binary = toAsciiString( bites ) ;
+        assertEquals( "01100000", binary ) ;
+        
+        bites = ByteBuffer.allocate( 1 ) ;
+        t = new Tuple( 30, 0 ) ;
+        t.setTag( bites, 1 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111110", binary ) ;
+
+        bites = ByteBuffer.allocate( 1 ) ;
+        t = new Tuple( 30, 0 ) ;
+        t.isPrimitive = true ;
+        t.setTag( bites, 1 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01011110", binary ) ;
+
+        bites = ByteBuffer.allocate( 2 ) ;
+        t = new Tuple( 31, 0 ) ;
+        t.setTag( bites, 2 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00011111" + "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 2 ) ;
+        t = new Tuple( 127, 0 ) ;
+        t.setTag( bites, 2 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111111" + "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 3 ) ;
+        t = new Tuple( 128, 0 ) ;
+        t.setTag( bites, 3 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" + "10000000" + "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 3 ) ;
+        t = new Tuple( Tuple.BIT_13 - 1, 0 ) ;
+        t.setTag( bites, 3 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111111" + 
+                      "11111111" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( Tuple.BIT_13, 0 ) ;
+        t.setTag( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "10000000" +
+                      "10000000" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( Tuple.BIT_13 + 1, 0 ) ;
+        t.setTag( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "10000000" +
+                      "10000001" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( Tuple.BIT_20 - 1, 0 ) ;
+        t.setTag( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111111" + 
+                      "11111111" + 
+                      "11111111" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( Tuple.BIT_20, 0 ) ;
+        t.setTag( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "10000000" +
+                      "10000000" +
+                      "10000000" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( Tuple.BIT_20 + 1, 0 ) ;
+        t.setTag( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "10000000" +
+                      "10000000" +
+                      "10000001" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( Tuple.BIT_27 - 1, 0 ) ;
+        t.setTag( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111111" + 
+                      "11111111" + 
+                      "11111111" + 
+                      "11111111" + 
+                      "01111111", binary ) ;
+
+        bites = ByteBuffer.allocate( 6 ) ;
+        t = new Tuple( Tuple.BIT_27, 0 ) ;
+        
+        try
+        {
+            t.setTag( bites, 6 ) ;
+            fail( "should never reach this point due to thrown exception" ) ;
+        }
+        catch( IllegalArgumentException e )
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    public void testSetTagbyteArrayint()
     {
         byte[] bites = new byte[1] ;
         Tuple t = new Tuple( 0, 0 ) ;
@@ -762,7 +1197,126 @@
         }
     }
 
-    public void testSetLength()
+    public void testSetLengthBuffer()
+    {
+        ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
+        Tuple t = new Tuple( 0, 0 ) ;
+        t.setLength( bites, 1 ) ;
+        String binary = toAsciiString( bites ) ;
+        assertEquals( "00000000", binary ) ;
+
+        bites = ByteBuffer.allocate( 1 ) ;
+        t = new Tuple( 30, 15 ) ;
+        t.setLength( bites, 1 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00001111", binary ) ;
+
+        bites = ByteBuffer.allocate( 1 ) ;
+        t = new Tuple( 30, 127 ) ;
+        t.setLength( bites, 1 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "01111111", binary ) ;
+        
+        bites = ByteBuffer.allocate( 2 ) ;
+        t = new Tuple( 30, 128 ) ;
+        t.setLength( bites, 2 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "10000000" +
+                      "10000001", binary ) ;
+        
+        bites = ByteBuffer.allocate( 2 ) ;
+        t = new Tuple( 30, 255 ) ;
+        t.setLength( bites, 2 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "11111111" +
+                      "10000001", binary ) ;
+        
+        bites = ByteBuffer.allocate( 3 ) ;
+        t = new Tuple( 30, 256 ) ;
+        t.setLength( bites, 3 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "00000000" +
+                      "10000010", binary ) ;
+        
+        bites = ByteBuffer.allocate( 3 ) ;
+        t = new Tuple( 30, Tuple.BIT_15 - 1 ) ;
+        t.setLength( bites, 3 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "11111111" +
+                      "11111111" +
+                      "10000010", binary ) ;
+        
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( 30, Tuple.BIT_15 ) ;
+        t.setLength( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "00000000" +
+                      "00000000" +
+                      "10000011", binary ) ;
+        
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( 30, Tuple.BIT_15 + 1 ) ;
+        t.setLength( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "00000000" +
+                      "00000001" +
+                      "10000011", binary ) ;
+        
+        bites = ByteBuffer.allocate( 4 ) ;
+        t = new Tuple( 30, Tuple.BIT_23 - 1 ) ;
+        t.setLength( bites, 4 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "11111111" +
+                      "11111111" +
+                      "11111111" +
+                      "10000011", binary ) ;
+        
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( 30, Tuple.BIT_23 ) ;
+        t.setLength( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "00000000" +
+                      "00000000" +
+                      "00000000" +
+                      "10000100", binary ) ;
+        
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( 30, Tuple.BIT_23 + 1 ) ;
+        t.setLength( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( "00000001" +
+                      "00000000" +
+                      "00000000" +
+                      "00000001" +
+                      "10000100", binary ) ;
+        
+        bites = ByteBuffer.allocate( 5 ) ;
+        t = new Tuple( 30, Integer.MAX_VALUE ) ;
+        t.setLength( bites, 5 ) ;
+        binary = toAsciiString( bites ) ;
+        assertEquals( Binary.toAsciiString( Integer.MAX_VALUE ) +
+                      "10000100", binary ) ;
+        
+        
+        bites = ByteBuffer.allocate( 6 ) ;
+        t = new Tuple( 30, Integer.MAX_VALUE + 1 ) ;
+
+        try
+        {
+            t.setLength( bites, 6 ) ;
+            fail( "should never get here due to thrown exception" ) ;
+        }
+        catch( IllegalArgumentException e ) 
+        {
+            assertNotNull( e ) ;
+        }
+    }
+
+    public void testSetLengthbyteArrayintint()
     {
         byte[] bites = new byte[1] ;
         Tuple t = new Tuple( 0, 0 ) ;
@@ -891,9 +1445,9 @@
         t.id = 31 ;
         assertEquals( 2, t.getTagLength() ) ;
         t.id = 100 ;
-        assertEquals( 2, t.getTagLength() ) ;
+        assertEquals( 3, t.getTagLength() ) ;
         t.id = 127 ;
-        assertEquals( 2, t.getTagLength() ) ;
+        assertEquals( 3, t.getTagLength() ) ;
         t.id = 128 ;
         assertEquals( 3, t.getTagLength() ) ;
         t.id = 129 ;

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/TupleTreeDecoderTest.java	Fri Mar 26 17:11:11 2004
@@ -64,15 +64,15 @@
     {
         TupleTreeDecoder decoder = new TupleTreeDecoder() ;
         decoder.decode(ByteBuffer.wrap( 
-                        new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).encode())) ;
+                        new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).toEncodedArray())) ;
         decoder.decode( ByteBuffer.wrap( new Tuple( 1, 
-                                ArrayUtils.EMPTY_BYTE_ARRAY ).encode() ) ) ;
+                                ArrayUtils.EMPTY_BYTE_ARRAY ).toEncodedArray() ) ) ;
     }    
     
 
     public void testTTD4() throws Exception
     {
-        byte[] bites = new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).encode() ;
+        byte[] bites = new Tuple( 1, ArrayUtils.EMPTY_BYTE_ARRAY ).toEncodedArray() ;
         byte[] shorter = new byte[bites.length-1] ;
         System.arraycopy( bites, 0, shorter, 0, shorter.length ) ;