1 /**
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.geronimo.util.asn1;
20
21 import java.io.IOException;
22
23 /**
24 * DER BMPString object.
25 */
26 public class DERBMPString
27 extends DERObject
28 implements DERString
29 {
30 String string;
31
32 /**
33 * return a BMP String from the given object.
34 *
35 * @param obj the object we want converted.
36 * @exception IllegalArgumentException if the object cannot be converted.
37 */
38 public static DERBMPString getInstance(
39 Object obj)
40 {
41 if (obj == null || obj instanceof DERBMPString)
42 {
43 return (DERBMPString)obj;
44 }
45
46 if (obj instanceof ASN1OctetString)
47 {
48 return new DERBMPString(((ASN1OctetString)obj).getOctets());
49 }
50
51 if (obj instanceof ASN1TaggedObject)
52 {
53 return getInstance(((ASN1TaggedObject)obj).getObject());
54 }
55
56 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
57 }
58
59 /**
60 * return a BMP String from a tagged object.
61 *
62 * @param obj the tagged object holding the object we want
63 * @param explicit true if the object is meant to be explicitly
64 * tagged false otherwise.
65 * @exception IllegalArgumentException if the tagged object cannot
66 * be converted.
67 */
68 public static DERBMPString getInstance(
69 ASN1TaggedObject obj,
70 boolean explicit)
71 {
72 return getInstance(obj.getObject());
73 }
74
75
76 /**
77 * basic constructor - byte encoded string.
78 */
79 public DERBMPString(
80 byte[] string)
81 {
82 char[] cs = new char[string.length / 2];
83
84 for (int i = 0; i != cs.length; i++)
85 {
86 cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
87 }
88
89 this.string = new String(cs);
90 }
91
92 /**
93 * basic constructor
94 */
95 public DERBMPString(
96 String string)
97 {
98 this.string = string;
99 }
100
101 public String getString()
102 {
103 return string;
104 }
105
106 public int hashCode()
107 {
108 return this.getString().hashCode();
109 }
110
111 public boolean equals(
112 Object o)
113 {
114 if (!(o instanceof DERBMPString))
115 {
116 return false;
117 }
118
119 DERBMPString s = (DERBMPString)o;
120
121 return this.getString().equals(s.getString());
122 }
123
124 void encode(
125 DEROutputStream out)
126 throws IOException
127 {
128 char[] c = string.toCharArray();
129 byte[] b = new byte[c.length * 2];
130
131 for (int i = 0; i != c.length; i++)
132 {
133 b[2 * i] = (byte)(c[i] >> 8);
134 b[2 * i + 1] = (byte)c[i];
135 }
136
137 out.writeEncoded(BMP_STRING, b);
138 }
139 }