You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/08/03 00:27:56 UTC

svn commit: r227097 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java

Author: elecharny
Date: Tue Aug  2 15:27:51 2005
New Revision: 227097

URL: http://svn.apache.org/viewcvs?rev=227097&view=rev
Log:
Added the encoding method

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java?rev=227097&r1=227096&r2=227097&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/Control.java Tue Aug  2 15:27:51 2005
@@ -16,8 +16,14 @@
  */
 package org.apache.asn1.ldap.pojo;
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+
 import org.apache.asn1.Asn1Object;
+import org.apache.asn1.EncoderException;
 import org.apache.asn1.ber.tlv.Length;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
 import org.apache.asn1.primitives.OID;
 import org.apache.asn1.primitives.OctetString;
 
@@ -39,6 +45,9 @@
 
     /** Optionnal control value */
     private OctetString controlValue;
+    
+    /** The control length */
+    private transient int controlLength;
 
     //~ Methods ------------------------------------------------------------------------------------
 
@@ -120,8 +129,6 @@
      */
     public int computeLength()
     {
-        int controlLength = 0;
-
         // The controlType
         int controlTypeLengh = controlType.getOIDLength();
         controlLength = 1 + Length.getNbBytes( controlTypeLengh ) + controlTypeLengh;  
@@ -141,6 +148,52 @@
         return 1 + Length.getNbBytes( controlLength ) + controlLength;
     }
     
+    /**
+     * Generate the PDU which contains the Control.
+     * 
+     * Control :
+     * 0x30 LL
+     *   0x04 LL type 
+     *   [0x01 0x01 criticality]
+     *   [0x04 LL value]
+     *   
+     * @param object The encoded PDU
+     * @return A ByteBuffer that contaons the PDU
+     * @throws EncoderException If anything goes wrong.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        try 
+        {
+            // The LdapMessage Sequence
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            
+            // The length has been calculated by the computeLength method
+            buffer.put( Length.getBytes( controlLength ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+            
+        // The control type
+        Value.encode( buffer, controlType );
+
+        // The control criticality, if true
+        if ( criticality == true )
+        {
+            Value.encode( buffer, criticality );
+        }
+        
+        // The control value, if any
+        if ( controlValue != null )
+        {
+            Value.encode( buffer, controlValue );
+        }
+
+        return buffer;
+    }
+
     /**
      * Return a String representing a Control
      */