KickJava   Java API By Example, From Geeks To Geeks.

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


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 NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }.
24  */

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

36     public static DERNumericString getInstance(
37         Object JavaDoc obj)
38     {
39         if (obj == null || obj instanceof DERNumericString)
40         {
41             return (DERNumericString)obj;
42         }
43
44         if (obj instanceof ASN1OctetString)
45         {
46             return new DERNumericString(((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 Numeric 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 DERNumericString getInstance(
67         ASN1TaggedObject obj,
68         boolean explicit)
69     {
70         return getInstance(obj.getObject());
71     }
72
73     /**
74      * basic constructor - with bytes.
75      */

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