KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DERUTF8String


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * DER UTF8String object.
25  */

26 public class DERUTF8String
27     extends DERObject
28     implements DERString
29 {
30     String JavaDoc string;
31
32     /**
33      * return an UTF8 string from the passed in object.
34      *
35      * @exception IllegalArgumentException if the object cannot be converted.
36      */

37     public static DERUTF8String getInstance(
38         Object JavaDoc obj)
39     {
40         if (obj == null || obj instanceof DERUTF8String)
41         {
42             return (DERUTF8String)obj;
43         }
44
45         if (obj instanceof ASN1OctetString)
46         {
47             return new DERUTF8String(((ASN1OctetString)obj).getOctets());
48         }
49
50         if (obj instanceof ASN1TaggedObject)
51         {
52             return getInstance(((ASN1TaggedObject)obj).getObject());
53         }
54
55         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: " + obj.getClass().getName());
56     }
57
58     /**
59      * return an UTF8 String from a tagged object.
60      *
61      * @param obj the tagged object holding the object we want
62      * @param explicit true if the object is meant to be explicitly
63      * tagged false otherwise.
64      * @exception IllegalArgumentException if the tagged object cannot
65      * be converted.
66      */

67     public static DERUTF8String getInstance(
68         ASN1TaggedObject obj,
69         boolean explicit)
70     {
71         return getInstance(obj.getObject());
72     }
73
74     /**
75      * basic constructor - byte encoded string.
76      */

77     DERUTF8String(
78         byte[] string)
79     {
80         int i = 0;
81         int length = 0;
82
83         while (i < string.length)
84         {
85             length++;
86             if ((string[i] & 0xe0) == 0xe0)
87             {
88                 i += 3;
89             }
90             else if ((string[i] & 0xc0) == 0xc0)
91             {
92                 i += 2;
93             }
94             else
95             {
96                 i += 1;
97             }
98         }
99
100         char[] cs = new char[length];
101
102         i = 0;
103         length = 0;
104
105         while (i < string.length)
106         {
107             char ch;
108
109             if ((string[i] & 0xe0) == 0xe0)
110             {
111                 ch = (char)(((string[i] & 0x1f) << 12)
112                       | ((string[i + 1] & 0x3f) << 6) | (string[i + 2] & 0x3f));
113                 i += 3;
114             }
115             else if ((string[i] & 0xc0) == 0xc0)
116             {
117                 ch = (char)(((string[i] & 0x3f) << 6) | (string[i + 1] & 0x3f));
118                 i += 2;
119             }
120             else
121             {
122                 ch = (char)(string[i] & 0xff);
123                 i += 1;
124             }
125
126             cs[length++] = ch;
127         }
128
129         this.string = new String JavaDoc(cs);
130     }
131
132     /**
133      * basic constructor
134      */

135     public DERUTF8String(
136         String JavaDoc string)
137     {
138         this.string = string;
139     }
140
141     public String JavaDoc getString()
142     {
143         return string;
144     }
145
146     public int hashCode()
147     {
148         return this.getString().hashCode();
149     }
150
151     public boolean equals(
152         Object JavaDoc o)
153     {
154         if (!(o instanceof DERUTF8String))
155         {
156             return false;
157         }
158
159         DERUTF8String s = (DERUTF8String)o;
160
161         return this.getString().equals(s.getString());
162     }
163
164     void encode(
165         DEROutputStream out)
166         throws IOException JavaDoc
167     {
168         char[] c = string.toCharArray();
169         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
170
171         for (int i = 0; i != c.length; i++)
172         {
173             char ch = c[i];
174
175             if (ch < 0x0080)
176             {
177                 bOut.write(ch);
178             }
179             else if (ch < 0x0800)
180             {
181                 bOut.write(0xc0 | (ch >> 6));
182                 bOut.write(0x80 | (ch & 0x3f));
183             }
184             else
185             {
186                 bOut.write(0xe0 | (ch >> 12));
187                 bOut.write(0x80 | ((ch >> 6) & 0x3F));
188                 bOut.write(0x80 | (ch & 0x3F));
189             }
190         }
191
192         out.writeEncoded(UTF8_STRING, bOut.toByteArray());
193     }
194 }
195
Popular Tags