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