KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERUTF8String


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 /**
26  * DER UTF8String object.
27  */

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

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

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

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

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