KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 /**
24  * DER UniversalString object.
25  */

26 public class DERUniversalString
27     extends DERObject
28     implements DERString
29 {
30     private static final char[] table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
31     private byte[] string;
32
33     /**
34      * return a Universal String from the passed in object.
35      *
36      * @exception IllegalArgumentException if the object cannot be converted.
37      */

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

63     public static DERUniversalString getInstance(
64         ASN1TaggedObject obj,
65         boolean explicit)
66     {
67         return getInstance(obj.getObject());
68     }
69
70     /**
71      * basic constructor - byte encoded string.
72      */

73     public DERUniversalString(
74         byte[] string)
75     {
76         this.string = string;
77     }
78
79     public String JavaDoc getString()
80     {
81         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("#");
82         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
83         ASN1OutputStream aOut = new ASN1OutputStream(bOut);
84
85         try
86         {
87             aOut.writeObject(this);
88         }
89         catch (IOException JavaDoc e)
90         {
91            throw new RuntimeException JavaDoc("internal error encoding BitString");
92         }
93
94         byte[] string = bOut.toByteArray();
95
96         for (int i = 0; i != string.length; i++)
97         {
98             buf.append(table[(string[i] >>> 4) % 0xf]);
99             buf.append(table[string[i] & 0xf]);
100         }
101
102         return buf.toString();
103     }
104
105     public byte[] getOctets()
106     {
107         return string;
108     }
109
110     void encode(
111         DEROutputStream out)
112         throws IOException JavaDoc
113     {
114         out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
115     }
116
117     public boolean equals(
118         Object JavaDoc o)
119     {
120         if ((o == null) || !(o instanceof DERUniversalString))
121         {
122             return false;
123         }
124
125         return this.getString().equals(((DERUniversalString)o).getString());
126     }
127
128     public int hashCode()
129     {
130         return this.getString().hashCode();
131     }
132 }
133
Popular Tags