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.ByteArrayOutputStream;
22 import java.io.IOException;
23
24 /**
25 * DER UniversalString object.
26 */
27 public class DERUniversalString
28 extends DERObject
29 implements DERString
30 {
31 private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
32 private byte[] string;
33
34 /**
35 * return a Universal String from the passed in object.
36 *
37 * @exception IllegalArgumentException if the object cannot be converted.
38 */
39 public static DERUniversalString getInstance(
40 Object obj)
41 {
42 if (obj == null || obj instanceof DERUniversalString)
43 {
44 return (DERUniversalString)obj;
45 }
46
47 if (obj instanceof ASN1OctetString)
48 {
49 return new DERUniversalString(((ASN1OctetString)obj).getOctets());
50 }
51
52 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
53 }
54
55 /**
56 * return a Universal String from a tagged object.
57 *
58 * @param obj the tagged object holding the object we want
59 * @param explicit true if the object is meant to be explicitly
60 * tagged false otherwise.
61 * @exception IllegalArgumentException if the tagged object cannot
62 * be converted.
63 */
64 public static DERUniversalString getInstance(
65 ASN1TaggedObject obj,
66 boolean explicit)
67 {
68 return getInstance(obj.getObject());
69 }
70
71 /**
72 * basic constructor - byte encoded string.
73 */
74 public DERUniversalString(
75 byte[] string)
76 {
77 this.string = string;
78 }
79
80 public String getString()
81 {
82 StringBuffer buf = new StringBuffer("#");
83 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
84 ASN1OutputStream aOut = new ASN1OutputStream(bOut);
85
86 try
87 {
88 aOut.writeObject(this);
89 }
90 catch (IOException e)
91 {
92 throw new RuntimeException("internal error encoding BitString");
93 }
94
95 byte[] string = bOut.toByteArray();
96
97 for (int i = 0; i != string.length; i++)
98 {
99 buf.append(table[(string[i] >>> 4) % 0xf]);
100 buf.append(table[string[i] & 0xf]);
101 }
102
103 return buf.toString();
104 }
105
106 public byte[] getOctets()
107 {
108 return string;
109 }
110
111 void encode(
112 DEROutputStream out)
113 throws IOException
114 {
115 out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
116 }
117
118 public boolean equals(
119 Object o)
120 {
121 if ((o == null) || !(o instanceof DERUniversalString))
122 {
123 return false;
124 }
125
126 return this.getString().equals(((DERUniversalString)o).getString());
127 }
128
129 public int hashCode()
130 {
131 return this.getString().hashCode();
132 }
133 }