KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERObjectIdentifier


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.io.OutputStream;
6
7 public class DERObjectIdentifier
8     extends DERObject
9 {
10     String identifier;
11
12     /**
13      * return an OID from the passed in object
14      *
15      * @exception IllegalArgumentException if the object cannot be converted.
16      */

17     public static DERObjectIdentifier getInstance(
18         Object obj)
19     {
20         if (obj == null || obj instanceof DERObjectIdentifier)
21         {
22             return (DERObjectIdentifier)obj;
23         }
24
25         if (obj instanceof ASN1OctetString)
26         {
27             return new DERObjectIdentifier(((ASN1OctetString)obj).getOctets());
28         }
29
30         if (obj instanceof ASN1TaggedObject)
31         {
32             return getInstance(((ASN1TaggedObject)obj).getObject());
33         }
34
35         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
36     }
37
38     /**
39      * return an Object Identifier from a tagged object.
40      *
41      * @param obj the tagged object holding the object we want
42      * @param explicit true if the object is meant to be explicitly
43      * tagged false otherwise.
44      * @exception IllegalArgumentException if the tagged object cannot
45      * be converted.
46      */

47     public static DERObjectIdentifier getInstance(
48         ASN1TaggedObject obj,
49         boolean explicit)
50     {
51         return getInstance(obj.getObject());
52     }
53     
54
55     DERObjectIdentifier(
56         byte[] bytes)
57     {
58         StringBuffer objId = new StringBuffer();
59         int value = 0;
60         boolean first = true;
61
62         for (int i = 0; i != bytes.length; i++)
63         {
64             int b = bytes[i] & 0xff;
65
66             value = value * 128 + (b & 0x7f);
67             if ((b & 0x80) == 0) // end of number reached
68
{
69                 if (first)
70                 {
71                     switch (value / 40)
72                     {
73                     case 0:
74                         objId.append('0');
75                         break;
76                     case 1:
77                         objId.append('1');
78                         value -= 40;
79                         break;
80                     default:
81                         objId.append('2');
82                         value -= 80;
83                     }
84                     first = false;
85                 }
86
87                 objId.append('.');
88                 objId.append(Integer.toString(value));
89                 value = 0;
90             }
91         }
92
93         this.identifier = objId.toString();
94     }
95
96     public DERObjectIdentifier(
97         String identifier)
98     {
99         this.identifier = identifier;
100     }
101
102     public String getId()
103     {
104         return identifier;
105     }
106
107     private void writeField(
108         OutputStream out,
109         int fieldValue)
110         throws IOException
111     {
112         if (fieldValue >= (1 << 7))
113         {
114             if (fieldValue >= (1 << 14))
115             {
116                 if (fieldValue >= (1 << 21))
117                 {
118                     if (fieldValue >= (1 << 28))
119                     {
120                         out.write((fieldValue >> 28) | 0x80);
121                     }
122                     out.write((fieldValue >> 21) | 0x80);
123                 }
124                 out.write((fieldValue >> 14) | 0x80);
125             }
126             out.write((fieldValue >> 7) | 0x80);
127         }
128         out.write(fieldValue & 0x7f);
129     }
130
131     void encode(
132         DEROutputStream out)
133         throws IOException
134     {
135         OIDTokenizer tok = new OIDTokenizer(identifier);
136         ByteArrayOutputStream bOut = new ByteArrayOutputStream();
137         DEROutputStream dOut = new DEROutputStream(bOut);
138
139         writeField(bOut,
140                     Integer.parseInt(tok.nextToken()) * 40
141                     + Integer.parseInt(tok.nextToken()));
142
143         while (tok.hasMoreTokens())
144         {
145             writeField(bOut, Integer.parseInt(tok.nextToken()));
146         }
147
148         dOut.close();
149
150         byte[] bytes = bOut.toByteArray();
151
152         out.writeEncoded(OBJECT_IDENTIFIER, bytes);
153     }
154
155     public int hashCode()
156     {
157         return identifier.hashCode();
158     }
159
160     public boolean equals(
161         Object o)
162     {
163         if ((o == null) || !(o instanceof DERObjectIdentifier))
164         {
165             return false;
166         }
167
168         return identifier.equals(((DERObjectIdentifier)o).identifier);
169     }
170 }
171
Popular Tags