directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r226614 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java
Date Sun, 31 Jul 2005 08:03:48 GMT
Author: elecharny
Date: Sun Jul 31 01:03:45 2005
New Revision: 226614

URL: http://svn.apache.org/viewcvs?rev=226614&view=rev
Log:
Added two methods to encode strings and integers

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java?rev=226614&r1=226613&r2=226614&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Value.java Sun
Jul 31 01:03:45 2005
@@ -16,12 +16,15 @@
  */
 package org.apache.asn1.ber.tlv;
 
+import org.apache.asn1.EncoderException;
+import org.apache.asn1.ldap.codec.LdapConstants;
+import org.apache.asn1.util.MutableString;
 import org.apache.asn1.util.StringUtils;
 
 import java.io.Serializable;
+import java.nio.BufferOverflowException;
 import java.nio.ByteBuffer;
 
-
 /**
  * This class stores the data decoded from a TLV.
  * 
@@ -47,7 +50,7 @@
      */
     public void init( int size )
     {
-        data       = new byte[size];
+        data = new byte[size];
         currentPos = 0;
     }
 
@@ -56,7 +59,7 @@
      */
     public void reset()
     {
-        data       = null;
+        data = null;
         currentPos = 0;
     }
 
@@ -90,9 +93,9 @@
      */
     public void setData( ByteBuffer data )
     {
-    	int length = data.remaining();
-    	data.get(this.data, 0, length);
-    	currentPos = length;
+        int length = data.remaining();
+        data.get( this.data, 0, length );
+        currentPos = length;
     }
 
     /**
@@ -102,9 +105,9 @@
      */
     public void addData( ByteBuffer data )
     {
-    	int length = data.remaining();
-    	data.get(this.data , currentPos, length);
-    	currentPos += length;
+        int length = data.remaining();
+        data.get( this.data, currentPos, length );
+        currentPos += length;
     }
 
     /**
@@ -171,7 +174,7 @@
             return 4;
         }
     }
-    
+
     /**
      * Utility function that return a byte array representing the Value
      * @param value The value to store in a byte array
@@ -217,6 +220,64 @@
         }
 
         return bytes;
+    }
+
+    /**
+     * Encode a MutableString value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The MutableString to be encoded
+     */
+    public static void encode( ByteBuffer buffer, MutableString string ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try 
+        {
+            buffer.put( LdapConstants.OCTET_STRING_TAG );
+            buffer.put( Length.getBytes( string.getLength() ) );
+    
+            if ( string.getLength() != 0 )
+            {
+                buffer.put( string.getData() );
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
+    }
+
+    /**
+     * Encode an integer value 
+     * 
+     * @param buffer The PDU in which the value will be put
+     * @param string The interger to be encoded
+     */
+    public static void encode( ByteBuffer buffer, int value ) throws EncoderException
+    {
+        if ( buffer == null )
+        {
+            throw new EncoderException( "Cannot put a PDU in a null buffer !" );
+        }
+
+        try
+        {
+            buffer.put( LdapConstants.INTEGER_TAG );
+            buffer.put( Length.getBytes( value ) );
+            buffer.put( getBytes( value ) );
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+
+        return;
     }
 
     /**



Mime
View raw message