KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > DERNumericString


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  * DER NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }.
26  */

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

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

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

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

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