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/05/10 01:12:19 UTC

svn commit: r169375 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java

Author: elecharny
Date: Mon May  9 16:12:17 2005
New Revision: 169375

URL: http://svn.apache.org/viewcvs?rev=169375&view=rev
Log:
Added two utility functions :
- a function the return the number of bytes necessary to encode a Length
- a function that returns the byte array which represent the Length

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

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java?rev=169375&r1=169374&r2=169375&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java Mon May  9 16:12:17 2005
@@ -197,4 +197,95 @@
     {
         this.size = size;
     }
+    
+    /**
+     * Utility function that return the number of bytes necessary to store 
+     * the length
+     * @param length The length to store in a byte array
+     * @return The number of bytes necessary to store the length.
+     */
+    public static int getNbBytes(int length)
+    {
+        if (length >= 0)
+        {
+	        if (length < 128)
+	        {
+	            return 1;
+	        }
+	        else if (length < 256)
+	        {
+	            return 2;
+	        }
+	        else if (length < 65536)
+	        {
+	            return 3;
+	        }
+	        else if (length < 16777216)
+	        {
+	            return 4;
+	        }
+	        else
+	        {
+	            return 5;
+	        }
+        }
+        else
+        {
+            return 5;
+        }
+    }
+
+    /**
+     * Utility function that return a byte array representing the length
+     * @param length The length to store in a byte array
+     * @return The byte array representing the length.
+     */
+    public static byte[] getBytes(int length)
+    {
+        byte[] bytes = new byte[getNbBytes(length)];
+        
+        if (length >= 0) 
+        {
+		    if (length < 128)
+		    {
+		        bytes[0] = (byte)length;
+		    }
+		    else if (length < 256)
+		    {
+		        bytes[0] = (byte)0x81;
+		        bytes[1] = (byte)length;
+		    }
+		    else if (length < 65536)
+		    {
+		        bytes[0] = (byte)0x82;
+		        bytes[1] = (byte)(length >> 8);
+		        bytes[2] = (byte)(length & 0x00FF);
+		    }
+		    else if (length < 16777216)
+		    {
+		        bytes[0] = (byte)0x83;
+		        bytes[1] = (byte)(length >> 16);
+		        bytes[2] = (byte)( ( length >> 8) & 0x00FF );
+		        bytes[3] = (byte)(length & 0x00FF);
+		    }
+		    else
+		    {
+		        bytes[0] = (byte)0x84;
+		        bytes[1] = (byte)(length >> 24);
+		        bytes[2] = (byte)( ( length >> 16) & 0x00FF );
+		        bytes[3] = (byte)( ( length >> 8) & 0x00FF );
+		        bytes[4] = (byte)(length & 0x00FF);
+		    }
+        }
+        else
+        {
+	        bytes[0] = (byte)0x84;
+	        bytes[1] = (byte)(length >> 24);
+	        bytes[2] = (byte)( ( length >> 16) & 0x00FF );
+	        bytes[3] = (byte)( ( length >> 8) & 0x00FF );
+	        bytes[4] = (byte)(length & 0x00FF);
+        }
+        
+        return bytes;
+    }
 }