KickJava   Java API By Example, From Geeks To Geeks.

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


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 BMPString object.
26  */

27 public class DERBMPString
28     extends DERObject
29     implements DERString
30 {
31     String JavaDoc string;
32
33     /**
34      * return a BMP String from the given object.
35      *
36      * @param obj the object we want converted.
37      * @exception IllegalArgumentException if the object cannot be converted.
38      */

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

69     public static DERBMPString getInstance(
70         ASN1TaggedObject obj,
71         boolean explicit)
72     {
73         return getInstance(obj.getObject());
74     }
75
76
77     /**
78      * basic constructor - byte encoded string.
79      */

80     public DERBMPString(
81         byte[] string)
82     {
83         char[] cs = new char[string.length / 2];
84
85         for (int i = 0; i != cs.length; i++)
86         {
87             cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
88         }
89
90         this.string = new String JavaDoc(cs);
91     }
92
93     /**
94      * basic constructor
95      */

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