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 PrintableString object.
25 */
26 public class DERPrintableString
27 extends DERObject
28 implements DERString
29 {
30 String string;
31
32 /**
33 * return a printable string from the passed in object.
34 *
35 * @exception IllegalArgumentException if the object cannot be converted.
36 */
37 public static DERPrintableString getInstance(
38 Object obj)
39 {
40 if (obj == null || obj instanceof DERPrintableString)
41 {
42 return (DERPrintableString)obj;
43 }
44
45 if (obj instanceof ASN1OctetString)
46 {
47 return new DERPrintableString(((ASN1OctetString)obj).getOctets());
48 }
49
50 if (obj instanceof ASN1TaggedObject)
51 {
52 return getInstance(((ASN1TaggedObject)obj).getObject());
53 }
54
55 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
56 }
57
58 /**
59 * return a Printable 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 DERPrintableString getInstance(
68 ASN1TaggedObject obj,
69 boolean explicit)
70 {
71 return getInstance(obj.getObject());
72 }
73
74 /**
75 * basic constructor - byte encoded string.
76 */
77 public DERPrintableString(
78 byte[] string)
79 {
80 char[] cs = new char[string.length];
81
82 for (int i = 0; i != cs.length; i++)
83 {
84 cs[i] = (char)(string[i] & 0xff);
85 }
86
87 this.string = new String(cs);
88 }
89
90 /**
91 * basic constructor
92 */
93 public DERPrintableString(
94 String string)
95 {
96 this.string = string;
97 }
98
99 public String getString()
100 {
101 return string;
102 }
103
104 public byte[] getOctets()
105 {
106 char[] cs = string.toCharArray();
107 byte[] bs = new byte[cs.length];
108
109 for (int i = 0; i != cs.length; i++)
110 {
111 bs[i] = (byte)cs[i];
112 }
113
114 return bs;
115 }
116
117 void encode(
118 DEROutputStream out)
119 throws IOException
120 {
121 out.writeEncoded(PRINTABLE_STRING, this.getOctets());
122 }
123
124 public int hashCode()
125 {
126 return this.getString().hashCode();
127 }
128
129 public boolean equals(
130 Object o)
131 {
132 if (!(o instanceof DERPrintableString))
133 {
134 return false;
135 }
136
137 DERPrintableString s = (DERPrintableString)o;
138
139 return this.getString().equals(s.getString());
140 }
141 }