2002/03/21 01:19:32
[org.ibex.core.git] / src / org / bouncycastle / asn1 / DERBMPString.java
1 package org.bouncycastle.asn1;
2
3 import java.io.*;
4
5 /**
6  * DER BMPString object.
7  */
8 public class DERBMPString
9     extends DERObject
10     implements DERString
11 {
12     String  string;
13
14     /**
15      * basic constructor - byte encoded string.
16      */
17     public DERBMPString(
18         byte[]   string)
19     {
20             try
21             {
22             this.string = new String(string, "UnicodeBig");
23             }
24             catch (UnsupportedEncodingException e)
25             {
26             throw new RuntimeException(e.toString());
27             }
28     }
29
30     /**
31      * basic constructor
32      */
33     public DERBMPString(
34         String   string)
35     {
36         this.string = string;
37     }
38
39     public String getString()
40     {
41         return string;
42     }
43
44     void encode(
45         DEROutputStream  out)
46         throws IOException
47     {
48         char[]  c = string.toCharArray();
49         byte[]  b = new byte[c.length * 2];
50
51         for (int i = 0; i != c.length; i++)
52         {
53             b[2 * i] = (byte)((c[i] & 0xff00) >> 8);
54             b[2 * i + 1] = (byte)c[i];
55         }
56
57         out.writeEncoded(BMP_STRING, b);
58     }
59 }