KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
23  * DER T61String (also the teletex string)
24  */

25 public class DERT61String
26     extends DERObject
27     implements DERString
28 {
29     String JavaDoc string;
30
31     /**
32      * return a T61 string from the passed in object.
33      *
34      * @exception IllegalArgumentException if the object cannot be converted.
35      */

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

66     public static DERT61String getInstance(
67         ASN1TaggedObject obj,
68         boolean explicit)
69     {
70         return getInstance(obj.getObject());
71     }
72
73     /**
74      * basic constructor - with bytes.
75      */

76     public DERT61String(
77         byte[] string)
78     {
79         char[] cs = new char[string.length];
80
81         for (int i = 0; i != cs.length; i++)
82         {
83             cs[i] = (char)(string[i] & 0xff);
84         }
85
86         this.string = new String JavaDoc(cs);
87     }
88
89     /**
90      * basic constructor - with string.
91      */

92     public DERT61String(
93         String JavaDoc string)
94     {
95         this.string = string;
96     }
97
98     public String JavaDoc getString()
99     {
100         return string;
101     }
102
103     void encode(
104         DEROutputStream out)
105         throws IOException JavaDoc
106     {
107         out.writeEncoded(T61_STRING, this.getOctets());
108     }
109
110     public byte[] getOctets()
111     {
112         char[] cs = string.toCharArray();
113         byte[] bs = new byte[cs.length];
114
115         for (int i = 0; i != cs.length; i++)
116         {
117             bs[i] = (byte)cs[i];
118         }
119
120         return bs;
121     }
122
123     public boolean equals(
124         Object JavaDoc o)
125     {
126         if ((o == null) || !(o instanceof DERT61String))
127         {
128             return false;
129         }
130
131         return this.getString().equals(((DERT61String)o).getString());
132     }
133
134     public int hashCode()
135     {
136         return this.getString().hashCode();
137     }
138 }
139
Popular Tags