1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5
8 public class DERBMPString
9 extends DERObject
10 implements DERString
11 {
12 String string;
13
14
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
50 public static DERBMPString getInstance(
51 ASN1TaggedObject obj,
52 boolean explicit)
53 {
54 return getInstance(obj.getObject());
55 }
56
57
58
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
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 DERPrintableString s = (DERPrintableString)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