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/01 01:12:10 UTC
svn commit: r165430 -
/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
Author: elecharny
Date: Sat Apr 30 16:12:09 2005
New Revision: 165430
URL: http://svn.apache.org/viewcvs?rev=165430&view=rev
Log:
Lot of improvment and correction in the OID primitive class. The test case has helped debugging it !
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java?rev=165430&r1=165429&r2=165430&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/primitives/OID.java Sat Apr 30 16:12:09 2005
@@ -19,6 +19,7 @@
import org.apache.asn1.DecoderException;
import org.apache.asn1.util.pools.PoolObject;
+
/**
* This class implement an OID (Object Identifier).
*
@@ -48,10 +49,15 @@
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-public class OID extends PoolObject {
+public class OID extends PoolObject
+{
+ //~ Instance fields ----------------------------------------------------------------------------
+
/** The OID as a array of int */
private int[] oidValues;
+ //~ Constructors -------------------------------------------------------------------------------
+
/**
* Creates a new OID object.
*/
@@ -62,92 +68,112 @@
}
/**
+ * Frees the object
+ */
+ public void free()
+ {
+ if ( oidValues != null )
+ {
+ for ( int i = 0; i < oidValues.length ; i ++ )
+ {
+ oidValues[i] = 0;
+ }
+ }
+ }
+
+ //~ Methods ------------------------------------------------------------------------------------
+
+ /**
* Set the OID. It will be translated from a byte array to a string.
* @param oid
*/
- public void setOID(byte[] oid) throws DecoderException
+ public void setOID( byte[] oid ) throws DecoderException
{
+
if ( oid == null )
{
- throw new DecoderException("Null OID");
+ throw new DecoderException( "Null OID" );
}
-
- if ( oid == null | oid.length < 1 )
+
+ if ( ( oid == null ) | ( oid.length < 1 ) )
{
- throw new DecoderException("Invalid OID : " + oid);
+ throw new DecoderException( "Invalid OID : " + oid );
}
-
+
// First, we have to calculate the number of int to allocate
int nbValues = 2;
-
- int pos = 1;
+
+ int pos = 1;
while ( pos < oid.length )
{
+
if ( oid[pos] >= 0 )
{
nbValues++;
}
-
+
pos++;
}
-
+
oidValues = new int[nbValues];
-
- nbValues = 0;
- pos = 0;
+
+ nbValues = 0;
+ pos = 0;
+
int accumulator = 0;
-
+
if ( oid[0] < 40 )
{
oidValues[nbValues++] = 0;
- oidValues[nbValues++] = oid[pos++] ; // itu-t
- }
- else if ( oid[0] < 80 )
+ oidValues[nbValues++] = oid[pos++]; // itu-t
+ }
+ else if ( oid[0] < 80 )
{
oidValues[nbValues++] = 1;
- oidValues[nbValues++] = oid[pos++] - 40 ; // iso
+ oidValues[nbValues++] = oid[pos++] - 40; // iso
}
- else
+ else
{
oidValues[nbValues++] = 2;
-
+
while ( pos < oid.length )
{
+
if ( oid[pos] >= 0 )
{
- oidValues[nbValues++] = (( accumulator << 7 ) + oid[pos]) - 80;
- accumulator = 0;
+ oidValues[nbValues++] = ( ( accumulator << 7 ) + oid[pos] ) - 80;
+ accumulator = 0;
pos++;
break;
}
else
{
- accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
+ accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
}
-
+
pos++;
}
}
while ( pos < oid.length )
{
-
+
if ( oid[pos] >= 0 )
{
- oidValues[nbValues++] = ( accumulator << 7 ) + oid[pos];
- accumulator = 0;
+ oidValues[nbValues++] = ( accumulator << 7 ) + oid[pos];
+ accumulator = 0;
}
else
{
- accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
+ accumulator = ( accumulator << 7 ) + ( oid[pos] & 0x007F );
}
-
+
pos++;
}
}
-
+
/**
* Get an array of int from the OID
* @return An array of int representing the OID
@@ -163,15 +189,17 @@
*/
public String getOID()
{
+
StringBuffer sb = new StringBuffer();
- sb.append(oidValues[0]);
+ sb.append( oidValues[0] );
+
int pos = 1;
- for ( int i = 1 ; i < oidValues.length ; i++ )
+ for ( int i = 1; i < oidValues.length; i++ )
{
- sb.append('.').append(oidValues[i]);
+ sb.append( '.' ).append( oidValues[i] );
}
-
+
return sb.toString();
}
}