KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21
22 /**
23  * DER BMPString object.
24  */

25 public class DERBMPString
26     extends DERObject
27     implements DERString
28 {
29     String JavaDoc string;
30
31     /**
32      * return a BMP String from the given object.
33      *
34      * @param obj the object we want converted.
35      * @exception IllegalArgumentException if the object cannot be converted.
36      */

37     public static DERBMPString getInstance(
38         Object JavaDoc obj)
39     {
40         if (obj == null || obj instanceof DERBMPString)
41         {
42             return (DERBMPString)obj;
43         }
44
45         if (obj instanceof ASN1OctetString)
46         {
47             return new DERBMPString(((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 a BMP 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 DERBMPString getInstance(
68         ASN1TaggedObject obj,
69         boolean explicit)
70     {
71         return getInstance(obj.getObject());
72     }
73
74
75     /**
76      * basic constructor - byte encoded string.
77      */

78     public DERBMPString(
79         byte[] string)
80     {
81         char[] cs = new char[string.length / 2];
82
83         for (int i = 0; i != cs.length; i++)
84         {
85             cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
86         }
87
88         this.string = new String JavaDoc(cs);
89     }
90
91     /**
92      * basic constructor
93      */

94     public DERBMPString(
95         String JavaDoc string)
96     {
97         this.string = string;
98     }
99
100     public String JavaDoc getString()
101     {
102         return string;
103     }
104
105     public int hashCode()
106     {
107         return this.getString().hashCode();
108     }
109
110     public boolean equals(
111         Object JavaDoc o)
112     {
113         if (!(o instanceof DERBMPString))
114         {
115             return false;
116         }
117
118         DERBMPString s = (DERBMPString)o;
119
120         return this.getString().equals(s.getString());
121     }
122
123     void encode(
124         DEROutputStream out)
125         throws IOException JavaDoc
126     {
127         char[] c = string.toCharArray();
128         byte[] b = new byte[c.length * 2];
129
130         for (int i = 0; i != c.length; i++)
131         {
132             b[2 * i] = (byte)(c[i] >> 8);
133             b[2 * i + 1] = (byte)c[i];
134         }
135
136         out.writeEncoded(BMP_STRING, b);
137     }
138 }
139
Popular Tags