DERT61String.java revision 44021512997b337e6079e46fd4230ce979c20b6f
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        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
31    }
32
33    /**
34     * return an T61 String from a tagged object.
35     *
36     * @param obj the tagged object holding the object we want
37     * @param explicit true if the object is meant to be explicitly
38     *              tagged false otherwise.
39     * @exception IllegalArgumentException if the tagged object cannot
40     *               be converted.
41     */
42    public static DERT61String getInstance(
43        ASN1TaggedObject obj,
44        boolean          explicit)
45    {
46        ASN1Primitive o = obj.getObject();
47
48        if (explicit || o instanceof DERT61String)
49        {
50            return getInstance(o);
51        }
52        else
53        {
54            return new DERT61String(ASN1OctetString.getInstance(o).getOctets());
55        }
56    }
57
58    /**
59     * basic constructor - with bytes.
60     */
61    DERT61String(
62        byte[]   string)
63    {
64        this.string = string;
65    }
66
67    /**
68     * basic constructor - with string.
69     */
70    public DERT61String(
71        String   string)
72    {
73        // BEGIN android-changed
74        this.string = Strings.toUTF8ByteArray(string);
75        // END android-changed
76    }
77
78    public String getString()
79    {
80        // BEGIN android-changed
81        return Strings.fromUTF8ByteArray(string);
82        // END android-changed
83    }
84
85    public String toString()
86    {
87        return getString();
88    }
89
90    boolean isConstructed()
91    {
92        return false;
93    }
94
95    int encodedLength()
96    {
97        return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
98    }
99
100    void encode(
101        ASN1OutputStream out)
102        throws IOException
103    {
104        out.writeEncoded(BERTags.T61_STRING, string);
105    }
106
107    public byte[] getOctets()
108    {
109        return Arrays.clone(string);
110    }
111
112    boolean asn1Equals(
113        ASN1Primitive o)
114    {
115        if (!(o instanceof DERT61String))
116        {
117            return false;
118        }
119
120        return Arrays.areEqual(string, ((DERT61String)o).string);
121    }
122
123    public int hashCode()
124    {
125        return Arrays.hashCode(string);
126    }
127}
128