KickJava   Java API By Example, From Geeks To Geeks.

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


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 public class DERGeneralString
23     extends DERObject implements DERString
24 {
25     private String JavaDoc string;
26
27     public static DERGeneralString getInstance(
28         Object JavaDoc obj)
29     {
30         if (obj == null || obj instanceof DERGeneralString)
31         {
32             return (DERGeneralString) obj;
33         }
34         if (obj instanceof ASN1OctetString)
35         {
36             return new DERGeneralString(((ASN1OctetString) obj).getOctets());
37         }
38         if (obj instanceof ASN1TaggedObject)
39         {
40             return getInstance(((ASN1TaggedObject) obj).getObject());
41         }
42         throw new IllegalArgumentException JavaDoc("illegal object in getInstance: "
43                 + obj.getClass().getName());
44     }
45
46     public static DERGeneralString getInstance(
47         ASN1TaggedObject obj,
48         boolean explicit)
49     {
50         return getInstance(obj.getObject());
51     }
52
53     public DERGeneralString(byte[] string)
54     {
55         char[] cs = new char[string.length];
56         for (int i = 0; i != cs.length; i++)
57         {
58             cs[i] = (char)(string[i] & 0xff);
59         }
60         this.string = new String JavaDoc(cs);
61     }
62
63     public DERGeneralString(String JavaDoc string)
64     {
65         this.string = string;
66     }
67
68     public String JavaDoc getString()
69     {
70         return string;
71     }
72
73     public byte[] getOctets()
74     {
75         char[] cs = string.toCharArray();
76         byte[] bs = new byte[cs.length];
77         for (int i = 0; i != cs.length; i++)
78         {
79             bs[i] = (byte) cs[i];
80         }
81         return bs;
82     }
83
84     void encode(DEROutputStream out)
85         throws IOException JavaDoc
86     {
87         out.writeEncoded(GENERAL_STRING, this.getOctets());
88     }
89
90     public int hashCode()
91     {
92         return this.getString().hashCode();
93     }
94
95     public boolean equals(Object JavaDoc o)
96     {
97         if (!(o instanceof DERGeneralString))
98         {
99             return false;
100         }
101         DERGeneralString s = (DERGeneralString) o;
102         return this.getString().equals(s.getString());
103     }
104 }
105
Popular Tags