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