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