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/06/21 02:36:23 UTC
svn commit: r191597 -
/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ber/tlv/Length.java
Author: elecharny
Date: Mon Jun 20 17:36:23 2005
New Revision: 191597
URL: http://svn.apache.org/viewcvs?rev=191597&view=rev
Log:
Added a flag to be able to handle indefinite long form.
minor refactor, mainly Jalopyzation of the code.
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=191597&r1=191596&r2=191597&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 Jun 20 17:36:23 2005
@@ -20,7 +20,7 @@
/**
- * The Length part of a TLV.
+ * The Length part of a TLV. We are not dealing with indefinite length.
*
* @author <a href="mailto:dev@directory.apache.org">Apache
* Directory Project</a>
@@ -30,7 +30,7 @@
//~ Static fields/initializers -----------------------------------------------------------------
/** A mask to get the Length form */
- public static final transient int LENGTH_LONG_FORM = 0x0080;
+ public static final transient int LENGTH_LONG_FORM = 0x0080;
/** Value of the reserved extension */
public static final transient int LENGTH_EXTENSION_RESERVED = 0x7F;
@@ -40,10 +40,10 @@
//~ Instance fields ----------------------------------------------------------------------------
- /** The expected length of the following value */
+ /** The length of the following value */
private int length;
- /** The size of the Length part */
+ /** The size of the Length part. */
private int size;
/** If the Length is in a long form, this variable store the expected
@@ -53,6 +53,9 @@
/** Stores the number of bytes already read for a long Length form */
private int currentLength;
+ /** A flag used with definite forms length. */
+ private boolean definiteForm;
+
//~ Constructors -------------------------------------------------------------------------------
/**
@@ -80,6 +83,8 @@
}
/**
+ * Get the Value length
+ *
* @return Returns the length of the value part.
*/
public int getLength()
@@ -99,6 +104,8 @@
}
/**
+ * Get the current number of bytes read
+ *
* @return Returns the currentLength.
*/
public int getCurrentLength()
@@ -126,7 +133,9 @@
}
/**
- * @return Returns the expected Length of the Length.
+ * Get the expected length
+ *
+ * @return Returns the expected Length of the long form Length.
*/
public int getExpectedLength()
{
@@ -134,9 +143,9 @@
}
/**
- * Set the expected length
+ * Set the expected long form length
*
- * @param expectedLength The expectedLength to set.
+ * @param expectedLength The long form expected length to set.
*/
public void setExpectedLength( int expectedLength )
{
@@ -157,6 +166,8 @@
}
/**
+ * Get the size of the Length element
+ *
* @return Returns the size of the Length element.
*/
public int getSize()
@@ -182,7 +193,9 @@
{
StringBuffer sb = new StringBuffer();
- sb.append( "LENGTH[" ).append( length ).append( "](size=" ).append( size ).append(
+ sb.append( "LENGTH[" ).append( length ).append( "](" )
+ .append( definiteForm ? "definite)" : "indefinite)" ).append( "size=" ).append( size )
+ .append(
")" );
return sb.toString();
@@ -197,37 +210,39 @@
{
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)
+ public static int getNbBytes( int length )
{
- if (length >= 0)
+
+ 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;
- }
+
+ 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
{
@@ -240,52 +255,73 @@
* @param length The length to store in a byte array
* @return The byte array representing the length.
*/
- public static byte[] getBytes(int length)
+ public static byte[] getBytes( int length )
{
- byte[] bytes = new byte[getNbBytes(length)];
-
- if (length >= 0)
+
+ 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);
- }
+
+ 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);
+ 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;
+ }
+
+ /**
+ * Get the length's type
+ * @return Returns the definiteForm flag.
+ */
+ public boolean isDefiniteForm()
+ {
+ return definiteForm;
+ }
+
+ /**
+ * Set the length's form
+ *
+ * @param definiteForm The definiteForm flag to set.
+ */
+ public void setDefiniteForm( boolean definiteForm )
+ {
+ this.definiteForm = definiteForm;
}
}