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