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