Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.IOException;
      4 
      5 /**
      6  * DER BMPString object.
      7  */
      8 public class DERBMPString
      9     extends ASN1Object
     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         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     29     }
     30 
     31     /**
     32      * return a BMP String from a tagged object.
     33      *
     34      * @param obj the tagged object holding the object we want
     35      * @param explicit true if the object is meant to be explicitly
     36      *              tagged false otherwise.
     37      * @exception IllegalArgumentException if the tagged object cannot
     38      *              be converted.
     39      */
     40     public static DERBMPString getInstance(
     41         ASN1TaggedObject obj,
     42         boolean          explicit)
     43     {
     44         DERObject o = obj.getObject();
     45 
     46         if (explicit || o instanceof DERBMPString)
     47         {
     48             return getInstance(o);
     49         }
     50         else
     51         {
     52             return new DERBMPString(ASN1OctetString.getInstance(o).getOctets());
     53         }
     54     }
     55 
     56 
     57     /**
     58      * basic constructor - byte encoded string.
     59      */
     60     public DERBMPString(
     61         byte[]   string)
     62     {
     63         char[]  cs = new char[string.length / 2];
     64 
     65         for (int i = 0; i != cs.length; i++)
     66         {
     67             cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
     68         }
     69 
     70         this.string = new String(cs);
     71     }
     72 
     73     /**
     74      * basic constructor
     75      */
     76     public DERBMPString(
     77         String   string)
     78     {
     79         this.string = string;
     80     }
     81 
     82     public String getString()
     83     {
     84         return string;
     85     }
     86 
     87     public String toString()
     88     {
     89         return string;
     90     }
     91 
     92     public int hashCode()
     93     {
     94         return this.getString().hashCode();
     95     }
     96 
     97     protected boolean asn1Equals(
     98         DERObject  o)
     99     {
    100         if (!(o instanceof DERBMPString))
    101         {
    102             return false;
    103         }
    104 
    105         DERBMPString  s = (DERBMPString)o;
    106 
    107         return this.getString().equals(s.getString());
    108     }
    109 
    110     void encode(
    111         DEROutputStream  out)
    112         throws IOException
    113     {
    114         char[]  c = string.toCharArray();
    115         byte[]  b = new byte[c.length * 2];
    116 
    117         for (int i = 0; i != c.length; i++)
    118         {
    119             b[2 * i] = (byte)(c[i] >> 8);
    120             b[2 * i + 1] = (byte)c[i];
    121         }
    122 
    123         out.writeEncoded(BMP_STRING, b);
    124     }
    125 }
    126