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 T61String (also the teletex string), try not to use this if you don't need to. The standard support the encoding for
     10  * this has been withdrawn.
     11  */
     12 public class DERT61String
     13     extends ASN1Primitive
     14     implements ASN1String
     15 {
     16     private byte[] string;
     17 
     18     /**
     19      * return a T61 string from the passed in object.
     20      *
     21      * @param obj a DERT61String or an object that can be converted into one.
     22      * @exception IllegalArgumentException if the object cannot be converted.
     23      * @return a DERT61String instance, or null
     24      */
     25     public static DERT61String getInstance(
     26         Object  obj)
     27     {
     28         if (obj == null || obj instanceof DERT61String)
     29         {
     30             return (DERT61String)obj;
     31         }
     32 
     33         if (obj instanceof byte[])
     34         {
     35             try
     36             {
     37                 return (DERT61String)fromByteArray((byte[])obj);
     38             }
     39             catch (Exception e)
     40             {
     41                 throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
     42             }
     43         }
     44 
     45         throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
     46     }
     47 
     48     /**
     49      * return an T61 String from a tagged object.
     50      *
     51      * @param obj the tagged object holding the object we want
     52      * @param explicit true if the object is meant to be explicitly
     53      *              tagged false otherwise.
     54      * @exception IllegalArgumentException if the tagged object cannot
     55      *               be converted.
     56      * @return a DERT61String instance, or null
     57      */
     58     public static DERT61String getInstance(
     59         ASN1TaggedObject obj,
     60         boolean          explicit)
     61     {
     62         ASN1Primitive o = obj.getObject();
     63 
     64         if (explicit || o instanceof DERT61String)
     65         {
     66             return getInstance(o);
     67         }
     68         else
     69         {
     70             return new DERT61String(ASN1OctetString.getInstance(o).getOctets());
     71         }
     72     }
     73 
     74     /**
     75      * basic constructor - string encoded as a sequence of bytes.
     76      *
     77      * @param string the byte encoding of the string to be wrapped.
     78      */
     79     public DERT61String(
     80         byte[]   string)
     81     {
     82         this.string = Arrays.clone(string);
     83     }
     84 
     85     /**
     86      * basic constructor - with string 8 bit assumed.
     87      *
     88      * @param string the string to be wrapped.
     89      */
     90     public DERT61String(
     91         String   string)
     92     {
     93         this.string = Strings.toByteArray(string);
     94     }
     95 
     96     /**
     97      * Decode the encoded string and return it, 8 bit encoding assumed.
     98      * @return the decoded String
     99      */
    100     public String getString()
    101     {
    102         return Strings.fromByteArray(string);
    103     }
    104 
    105     public String toString()
    106     {
    107         return getString();
    108     }
    109 
    110     boolean isConstructed()
    111     {
    112         return false;
    113     }
    114 
    115     int encodedLength()
    116     {
    117         return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
    118     }
    119 
    120     void encode(
    121         ASN1OutputStream out)
    122         throws IOException
    123     {
    124         out.writeEncoded(BERTags.T61_STRING, string);
    125     }
    126 
    127     /**
    128      * Return the encoded string as a byte array.
    129      * @return the actual bytes making up the encoded body of the T61 string.
    130      */
    131     public byte[] getOctets()
    132     {
    133         return Arrays.clone(string);
    134     }
    135 
    136     boolean asn1Equals(
    137         ASN1Primitive o)
    138     {
    139         if (!(o instanceof DERT61String))
    140         {
    141             return false;
    142         }
    143 
    144         return Arrays.areEqual(string, ((DERT61String)o).string);
    145     }
    146 
    147     public int hashCode()
    148     {
    149         return Arrays.hashCode(string);
    150     }
    151 }
    152