Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.ByteArrayOutputStream;
      4 import java.io.IOException;
      5 
      6 /**
      7  * DER UniversalString object.
      8  */
      9 public class DERUniversalString
     10     extends ASN1Object
     11     implements DERString
     12 {
     13     private static final char[]  table = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
     14     private byte[] string;
     15 
     16     /**
     17      * return a Universal String from the passed in object.
     18      *
     19      * @exception IllegalArgumentException if the object cannot be converted.
     20      */
     21     public static DERUniversalString getInstance(
     22         Object  obj)
     23     {
     24         if (obj == null || obj instanceof DERUniversalString)
     25         {
     26             return (DERUniversalString)obj;
     27         }
     28 
     29         if (obj instanceof ASN1OctetString)
     30         {
     31             return new DERUniversalString(((ASN1OctetString)obj).getOctets());
     32         }
     33 
     34         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     35     }
     36 
     37     /**
     38      * return a Universal String from a tagged object.
     39      *
     40      * @param obj the tagged object holding the object we want
     41      * @param explicit true if the object is meant to be explicitly
     42      *              tagged false otherwise.
     43      * @exception IllegalArgumentException if the tagged object cannot
     44      *               be converted.
     45      */
     46     public static DERUniversalString getInstance(
     47         ASN1TaggedObject obj,
     48         boolean          explicit)
     49     {
     50         return getInstance(obj.getObject());
     51     }
     52 
     53     /**
     54      * basic constructor - byte encoded string.
     55      */
     56     public DERUniversalString(
     57         byte[]   string)
     58     {
     59         this.string = string;
     60     }
     61 
     62     public String getString()
     63     {
     64         StringBuffer    buf = new StringBuffer("#");
     65         ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
     66         ASN1OutputStream            aOut = new ASN1OutputStream(bOut);
     67 
     68         try
     69         {
     70             aOut.writeObject(this);
     71         }
     72         catch (IOException e)
     73         {
     74            throw new RuntimeException("internal error encoding BitString");
     75         }
     76 
     77         byte[]    string = bOut.toByteArray();
     78 
     79         for (int i = 0; i != string.length; i++)
     80         {
     81             buf.append(table[(string[i] >>> 4) & 0xf]);
     82             buf.append(table[string[i] & 0xf]);
     83         }
     84 
     85         return buf.toString();
     86     }
     87 
     88     public String toString()
     89     {
     90         return getString();
     91     }
     92 
     93     public byte[] getOctets()
     94     {
     95         return string;
     96     }
     97 
     98     void encode(
     99         DEROutputStream  out)
    100         throws IOException
    101     {
    102         out.writeEncoded(UNIVERSAL_STRING, this.getOctets());
    103     }
    104 
    105     boolean asn1Equals(
    106         DERObject  o)
    107     {
    108         if (!(o instanceof DERUniversalString))
    109         {
    110             return false;
    111         }
    112 
    113         return this.getString().equals(((DERUniversalString)o).getString());
    114     }
    115 
    116     public int hashCode()
    117     {
    118         return this.getString().hashCode();
    119     }
    120 }
    121