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