FileDocCategorySizeDatePackage
ObjectIdentifier.javaAPI DocAndroid 1.5 API12134Wed May 06 22:41:06 BST 2009org.apache.harmony.security.asn1

ObjectIdentifier

public final class ObjectIdentifier extends Object
Instance of this class represents ObjectIdentifier (OID). According to X.690: OID is represented as a sequence of subidentifier. Each subidentifier is represented as non negative integer value. There are at least 2 subidentifiers in the sequence. Valid values for first subidentifier are 0, 1 and 2. If the first subidentifier has 0 or 1 value the second subidentifier must be less then 40.
see
ASN.1

Fields Summary
private final int[]
oid
private String
soid
Constructors Summary
public ObjectIdentifier(int[] oid)
Creates ObjectIdentifier(OID) from array of integers.

param
oid - array of integers
throws
IllegalArgumentException - if oid is invalid or null

        validate(oid);
        this.oid = oid;
    
public ObjectIdentifier(String strOid)
Creates ObjectIdentifier(OID) from string representation.

param
strOid - oid string
throws
IllegalArgumentException - if oid string is invalid or null

        this.oid = toIntArray(strOid);
        this.soid = strOid;
    
Methods Summary
public booleanequals(java.lang.Object o)
Compares object with OID for equality.

return
true if object is ObjectIdentifier and it has the same representation as array of integers, otherwise false

        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        return Arrays.equals(oid, ((ObjectIdentifier) o).oid);
    
public int[]getOid()
Returns array of integers.

return
array of integers

        return oid;
    
public inthashCode()

see
java.lang.Object#hashCode()

        // FIXME change me to Arrays.hashCode(int[])
        int intHash = 0;
        for (int i = 0; i < oid.length && i < 4; i++) {
            intHash += oid[i] << (8 * i); //TODO what about to find better one?
        }
        return intHash & 0x7FFFFFFF; // only positive
    
public static booleanisOID(java.lang.String str)
Returns whether the given string is a valid ObjectIdentifier (OID) representation. String representation is defined as for {@link #toIntArray}.

param
oidString - string representation of OID
return
true if oidString has valid syntax or false if not

        return toIntArray(str, false) != null;
    
private static int[]toIntArray(java.lang.String str, boolean shouldThrow)
Gets ObjectIdentifier (OID) from string representation. String representation is defined by the following syntax: OID = subidentifier 1*("." subidentifier) subidentifier = 1*(digit)

param
oidString - string representation of OID
return
- oid as array of integers or null if the oid string is invalid or null and shouldThrow is false
throws
IllegalArgumentException - if oid string is invalid or null and shouldThrow is true

        if (str == null) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(
                    Messages.getString("security.9D")); //$NON-NLS-1$
        }

        int length = str.length();
        if (length == 0) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(Messages.getString("security.9E")); //$NON-NLS-1$
        }

        int count = 1; // number of subidentifiers
        boolean wasDot = true; // indicates whether char before was dot or not.
        char c; // current char
        for (int i = 0; i < length; i++) {
            c = str.charAt(i);
            if (c == '.") {
                if (wasDot) {
                    if (! shouldThrow) {
                        return null;
                    }
                    throw new IllegalArgumentException(Messages.getString("security.9E")); //$NON-NLS-1$
                }
                wasDot = true;
                count++;
            } else if (c >= '0" && c <= '9") {
                wasDot = false;
            } else {
                if (! shouldThrow) {
                    return null;
                }
                throw new IllegalArgumentException(Messages.getString("security.9E")); //$NON-NLS-1$
            }
        }

        if (wasDot) {
            // the last char is dot
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(Messages.getString("security.9E")); //$NON-NLS-1$
        }

        if (count < 2) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(
                    Messages.getString("security.99")); //$NON-NLS-1$
        }

        int[] oid = new int[count];
        for (int i = 0, j = 0; i < length; i++) {
            c = str.charAt(i);
            if (c == '.") {
                j++;
            } else {
                oid[j] = oid[j] * 10 + c - 48; // '0' = 48
            }
        }

        if (oid[0] > 2) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(
                    Messages.getString("security.9A")); //$NON-NLS-1$
        } else if (oid[0] != 2 && oid[1] > 39) {
            if (! shouldThrow) {
                return null;
            }
            throw new IllegalArgumentException(
                    Messages.getString("security.9B")); //$NON-NLS-1$
        }

        return oid;
    
public static int[]toIntArray(java.lang.String str)
Gets ObjectIdentifier (OID) from string representation. String representation is defined by the following syntax: OID = subidentifier 1*("." subidentifier) subidentifier = 1*(digit)

param
oidString - string representation of OID
return
- oid as array of integers
throws
IllegalArgumentException - if oid string is invalid or null

        return toIntArray(str, true);
    
public java.lang.StringtoString()

see
java.lang.Object#toString()

        if (soid == null) {
            soid = toString(oid);
        }
        return soid;
    
public static java.lang.StringtoString(int[] oid)
Returns string representation of OID. Note: it is supposed that passed array of integers contains valid OID value, so no checks are performed.

param
oid - oid as array of integers
return
oid string representation

        StringBuffer sb = new StringBuffer(3 * oid.length);

        for (int i = 0; i < oid.length - 1; ++i) {
            sb.append(oid[i]);
            sb.append('.");
        }
        sb.append(oid[oid.length - 1]);
        return sb.toString();
    
public static voidvalidate(int[] oid)
Validates ObjectIdentifier (OID).

param
oid - oid as array of integers
throws
IllegalArgumentException - if oid is invalid or null


        if (oid == null) {
            throw new IllegalArgumentException(Messages.getString("security.98")); //$NON-NLS-1$
        }

        if (oid.length < 2) {
            throw new IllegalArgumentException(
                    Messages.getString("security.99")); //$NON-NLS-1$
        }

        if (oid[0] > 2) {
            throw new IllegalArgumentException(
                    Messages.getString("security.9A")); //$NON-NLS-1$
        } else if (oid[0] != 2 && oid[1] > 39) {
            throw new IllegalArgumentException(
                    Messages.getString("security.9B")); //$NON-NLS-1$
        }

        for (int i = 0; i < oid.length; i++) {
            if (oid[i] < 0) {
                throw new IllegalArgumentException(
                        Messages.getString("security.9C")); //$NON-NLS-1$
            }
        }