DERGeneralString.java revision e1142c149e244797ce73b0e7fad40816e447a817
1b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.compackage org.bouncycastle.asn1;
2b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
3b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comimport java.io.IOException;
4b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
5b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comimport org.bouncycastle.util.Arrays;
6b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.comimport org.bouncycastle.util.Strings;
7b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
8b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.compublic class DERGeneralString
9b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    extends ASN1Primitive
108e13a159f3a54f761ab80b16377015e5a9077411commit-bot@chromium.org    implements ASN1String
11b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com{
12e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com    private byte[] string;
13e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com
14e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com    public static DERGeneralString getInstance(
15e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com        Object obj)
16e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com    {
17d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com        if (obj == null || obj instanceof DERGeneralString)
18b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        {
19b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            return (DERGeneralString) obj;
20b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
21b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
22b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        if (obj instanceof byte[])
23b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        {
24b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            try
25b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            {
26b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                return (DERGeneralString)fromByteArray((byte[])obj);
27b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            }
28b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com            catch (Exception e)
29d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com            {
30b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
31e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com            }
32b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        }
33e4ae0bc5caeed52e07c7e5939fa4ac38d9e408abbungeman@google.com
34b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com        throw new IllegalArgumentException("illegal object in getInstance: "
35b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com                + obj.getClass().getName());
36b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com    }
37b29c883fb46ac6099440d82ac57b86d25386daedbungeman@google.com
38    public static DERGeneralString getInstance(
39        ASN1TaggedObject obj,
40        boolean explicit)
41    {
42        ASN1Primitive o = obj.getObject();
43
44        if (explicit || o instanceof DERGeneralString)
45        {
46            return getInstance(o);
47        }
48        else
49        {
50            return new DERGeneralString(((ASN1OctetString)o).getOctets());
51        }
52    }
53
54    DERGeneralString(byte[] string)
55    {
56        this.string = string;
57    }
58
59    public DERGeneralString(String string)
60    {
61        this.string = Strings.toByteArray(string);
62    }
63
64    public String getString()
65    {
66        return Strings.fromByteArray(string);
67    }
68
69    public String toString()
70    {
71        return getString();
72    }
73
74    public byte[] getOctets()
75    {
76        return Arrays.clone(string);
77    }
78
79    boolean isConstructed()
80    {
81        return false;
82    }
83
84    int encodedLength()
85    {
86        return 1 + StreamUtil.calculateBodyLength(string.length) + string.length;
87    }
88
89    void encode(ASN1OutputStream out)
90        throws IOException
91    {
92        out.writeEncoded(BERTags.GENERAL_STRING, string);
93    }
94
95    public int hashCode()
96    {
97        return Arrays.hashCode(string);
98    }
99
100    boolean asn1Equals(ASN1Primitive o)
101    {
102        if (!(o instanceof DERGeneralString))
103        {
104            return false;
105        }
106        DERGeneralString s = (DERGeneralString)o;
107
108        return Arrays.areEqual(string, s.string);
109    }
110}
111