Home | History | Annotate | Download | only in asn1
      1 package org.bouncycastle.asn1;
      2 
      3 import java.io.IOException;
      4 
      5 import org.bouncycastle.util.Arrays;
      6 import org.bouncycastle.util.Strings;
      7 
      8 /**
      9  * DER VisibleString object encoding ISO 646 (ASCII) character code points 32 to 126.
     10  * <p>
     11  * Explicit character set escape sequences are not allowed.
     12  * </p>
     13  */
     14 public class DERVisibleString
     15     extends ASN1Primitive
     16     implements ASN1String
     17 {
     18     private final byte[]  string;
     19 
     20     /**
     21      * Return a Visible String from the passed in object.
     22      *
     23      * @param obj a DERVisibleString or an object that can be converted into one.
     24      * @exception IllegalArgumentException if the object cannot be converted.
     25      * @return a DERVisibleString instance, or null
     26      */
     27     public static DERVisibleString getInstance(
     28         Object  obj)
     29     {
     30         if (obj == null || obj instanceof DERVisibleString)
     31         {
     32             return (DERVisibleString)obj;
     33         }
     34 
     35         if (obj instanceof byte[])
     36         {
     37             try
     38             {
     39                 return (DERVisibleString)fromByteArray((byte[])obj);
     40             }
     41             catch (Exception e)
     42             {
     43                 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
     44             }
     45         }
     46 
     47         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     48     }
     49 
     50     /**
     51      * Return a Visible String from a tagged object.
     52      *
     53      * @param obj the tagged object holding the object we want
     54      * @param explicit true if the object is meant to be explicitly
     55      *              tagged false otherwise.
     56      * @exception IllegalArgumentException if the tagged object cannot
     57      *               be converted.
     58      * @return a DERVisibleString instance, or null
     59      */
     60     public static DERVisibleString getInstance(
     61         ASN1TaggedObject obj,
     62         boolean          explicit)
     63     {
     64         ASN1Primitive o = obj.getObject();
     65 
     66         if (explicit || o instanceof DERVisibleString)
     67         {
     68             return getInstance(o);
     69         }
     70         else
     71         {
     72             return new DERVisibleString(ASN1OctetString.getInstance(o).getOctets());
     73         }
     74     }
     75 
     76     /*
     77      * Basic constructor - byte encoded string.
     78      */
     79     DERVisibleString(
     80         byte[]   string)
     81     {
     82         this.string = string;
     83     }
     84 
     85     /**
     86      * Basic constructor
     87      *
     88      * @param string the string to be carried in the VisibleString object,
     89      */
     90     public DERVisibleString(
     91         String   string)
     92     {
     93         this.string = Strings.toByteArray(string);
     94     }
     95 
     96     public String getString()
     97     {
     98         return Strings.fromByteArray(string);
     99     }
    100 
    101     public String toString()
    102     {
    103         return getString();
    104     }
    105 
    106     public byte[] getOctets()
    107     {
    108         return Arrays.clone(string);
    109     }
    110 
    111     boolean isConstructed()
    112     {
    113         return false;
    114     }
    115 
    116     int encodedLength()
    117     {
    118         return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
    119     }
    120 
    121     void encode(
    122         ASN1OutputStream out)
    123         throws IOException
    124     {
    125         out.writeEncoded(BERTags.VISIBLE_STRING, this.string);
    126     }
    127 
    128     boolean asn1Equals(
    129         ASN1Primitive o)
    130     {
    131         if (!(o instanceof DERVisibleString))
    132         {
    133             return false;
    134         }
    135 
    136         return Arrays.areEqual(string, ((DERVisibleString)o).string);
    137     }
    138 
    139     public int hashCode()
    140     {
    141         return Arrays.hashCode(string);
    142     }
    143 }
    144