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