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