KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.lowagie.bc.asn1;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6 /**
7  * DER UTF8String object.
8  */

9 public class DERUTF8String
10     extends DERObject
11     implements DERString
12 {
13     String string;
14
15     /**
16      * return an UTF8 string from the passed in object.
17      *
18      * @exception IllegalArgumentException if the object cannot be converted.
19      */

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

50     public static DERUTF8String getInstance(
51         ASN1TaggedObject obj,
52         boolean explicit)
53     {
54         return getInstance(obj.getObject());
55     }
56
57     /**
58      * basic constructor - byte encoded string.
59      */

60     DERUTF8String(
61         byte[] string)
62     {
63         int i = 0;
64         int length = 0;
65
66         while (i < string.length)
67         {
68             length++;
69             if ((string[i] & 0xe0) == 0xe0)
70             {
71                 i += 3;
72             }
73             else if ((string[i] & 0xc0) == 0xc0)
74             {
75                 i += 2;
76             }
77             else
78             {
79                 i += 1;
80             }
81         }
82
83         char[] cs = new char[length];
84
85         i = 0;
86         length = 0;
87
88         while (i < string.length)
89         {
90             char ch;
91
92             if ((string[i] & 0xe0) == 0xe0)
93             {
94                 ch = (char)(((string[i] & 0x1f) << 12)
95                       | ((string[i + 1] & 0x3f) << 6) | (string[i + 2] & 0x3f));
96                 i += 3;
97             }
98             else if ((string[i] & 0xc0) == 0xc0)
99             {
100                 ch = (char)(((string[i] & 0x3f) << 6) | (string[i + 1] & 0x3f));
101                 i += 2;
102             }
103             else
104             {
105                 ch = (char)(string[i] & 0xff);
106                 i += 1;
107             }
108
109             cs[length++] = ch;
110         }
111
112         this.string = new String(cs);
113     }
114
115     /**
116      * basic constructor
117      */

118     public DERUTF8String(
119         String string)
120     {
121         this.string = string;
122     }
123
124     public String getString()
125     {
126         return string;
127     }
128
129     public int hashCode()
130     {
131         return this.getString().hashCode();
132     }
133
134     public boolean equals(
135         Object o)
136     {
137         if (!(o instanceof DERUTF8String))
138         {
139             return false;
140         }
141
142         DERUTF8String s = (DERUTF8String)o;
143
144         return this.getString().equals(s.getString());
145     }
146
147     void encode(
148         DEROutputStream out)
149         throws IOException
150     {
151         char[] c = string.toCharArray();
152         ByteArrayOutputStream bOut = new ByteArrayOutputStream();
153
154         for (int i = 0; i != c.length; i++)
155         {
156             char ch = c[i];
157
158             if (ch < 0x0080)
159             {
160                 bOut.write(ch);
161             }
162             else if (ch < 0x0800)
163             {
164                 bOut.write(0xc0 | (ch >> 6));
165                 bOut.write(0x80 | (ch & 0x3f));
166             }
167             else
168             {
169                 bOut.write(0xe0 | (ch >> 12));
170                 bOut.write(0x80 | ((ch >> 6) & 0x3F));
171                 bOut.write(0x80 | (ch & 0x3F));
172             }
173         }
174
175         out.writeEncoded(UTF8_STRING, bOut.toByteArray());
176     }
177 }
178
Popular Tags