KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > bc > asn1 > DERBMPString


1 package com.lowagie.bc.asn1;
2
3 import java.io.IOException;
4
5 /**
6  * DER BMPString object.
7  */

8 public class DERBMPString
9     extends DERObject
10     implements DERString
11 {
12     String string;
13
14     /**
15      * return a BMP String from the given object.
16      *
17      * @param obj the object we want converted.
18      * @exception IllegalArgumentException if the object cannot be converted.
19      */

20     public static DERBMPString getInstance(
21         Object obj)
22     {
23         if (obj == null || obj instanceof DERBMPString)
24         {
25             return (DERBMPString)obj;
26         }
27
28         if (obj instanceof ASN1OctetString)
29         {
30             return new DERBMPString(((ASN1OctetString)obj).getOctets());
31         }
32
33         if (obj instanceof ASN1TaggedObject)
34         {
35             return getInstance(((ASN1TaggedObject)obj).getObject());
36         }
37
38         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
39     }
40
41     /**
42      * return a BMP String from a tagged object.
43      *
44      * @param obj the tagged object holding the object we want
45      * @param explicit true if the object is meant to be explicitly
46      * tagged false otherwise.
47      * @exception IllegalArgumentException if the tagged object cannot
48      * be converted.
49      */

50     public static DERBMPString getInstance(
51         ASN1TaggedObject obj,
52         boolean explicit)
53     {
54         return getInstance(obj.getObject());
55     }
56     
57
58     /**
59      * basic constructor - byte encoded string.
60      */

61     public DERBMPString(
62         byte[] string)
63     {
64         char[] cs = new char[string.length / 2];
65
66         for (int i = 0; i != cs.length; i++)
67         {
68             cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
69         }
70
71         this.string = new String(cs);
72     }
73
74     /**
75      * basic constructor
76      */

77     public DERBMPString(
78         String string)
79     {
80         this.string = string;
81     }
82
83     public String getString()
84     {
85         return string;
86     }
87
88     public int hashCode()
89     {
90         return this.getString().hashCode();
91     }
92
93     public boolean equals(
94         Object o)
95     {
96         if (!(o instanceof DERBMPString))
97         {
98             return false;
99         }
100
101         DERBMPString s = (DERBMPString)o;
102
103         return this.getString().equals(s.getString());
104     }
105
106     void encode(
107         DEROutputStream out)
108         throws IOException
109     {
110         char[] c = string.toCharArray();
111         byte[] b = new byte[c.length * 2];
112
113         for (int i = 0; i != c.length; i++)
114         {
115             b[2 * i] = (byte)(c[i] >> 8);
116             b[2 * i + 1] = (byte)c[i];
117         }
118
119         out.writeEncoded(BMP_STRING, b);
120     }
121 }
122
Popular Tags