1package org.bouncycastle.asn1;
2
3import java.io.ByteArrayOutputStream;
4import java.io.IOException;
5
6import org.bouncycastle.util.Encodable;
7
8/**
9 * Base class for defining an ASN.1 object.
10 */
11public abstract class ASN1Object
12    implements ASN1Encodable, Encodable
13{
14    /**
15     * Return the default BER or DER encoding for this object.
16     *
17     * @return BER/DER byte encoded object.
18     * @throws java.io.IOException on encoding error.
19     */
20    public byte[] getEncoded()
21        throws IOException
22    {
23        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
24        ASN1OutputStream      aOut = new ASN1OutputStream(bOut);
25
26        aOut.writeObject(this);
27
28        return bOut.toByteArray();
29    }
30
31    /**
32     * Return either the default for "BER" or a DER encoding if "DER" is specified.
33     *
34     * @param encoding name of encoding to use.
35     * @return byte encoded object.
36     * @throws IOException on encoding error.
37     */
38    public byte[] getEncoded(
39        String encoding)
40        throws IOException
41    {
42        if (encoding.equals(ASN1Encoding.DER))
43        {
44            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
45            DEROutputStream         dOut = new DEROutputStream(bOut);
46
47            dOut.writeObject(this);
48
49            return bOut.toByteArray();
50        }
51        else if (encoding.equals(ASN1Encoding.DL))
52        {
53            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
54            DLOutputStream          dOut = new DLOutputStream(bOut);
55
56            dOut.writeObject(this);
57
58            return bOut.toByteArray();
59        }
60
61        return this.getEncoded();
62    }
63
64    public int hashCode()
65    {
66        return this.toASN1Primitive().hashCode();
67    }
68
69    public boolean equals(
70        Object  o)
71    {
72        if (this == o)
73        {
74            return true;
75        }
76
77        if (!(o instanceof ASN1Encodable))
78        {
79            return false;
80        }
81
82        ASN1Encodable other = (ASN1Encodable)o;
83
84        return this.toASN1Primitive().equals(other.toASN1Primitive());
85    }
86
87    /**
88     * @deprecated use toASN1Primitive()
89     * @return the underlying primitive type.
90     */
91    public ASN1Primitive toASN1Object()
92    {
93        return this.toASN1Primitive();
94    }
95
96    /**
97     * Return true if obj is a byte array and represents an object with the given tag value.
98     *
99     * @param obj object of interest.
100     * @param tagValue tag value to check for.
101     * @return  true if obj is a byte encoding starting with the given tag value, false otherwise.
102     */
103    protected static boolean hasEncodedTagValue(Object obj, int tagValue)
104    {
105        return (obj instanceof byte[]) && ((byte[])obj)[0] == tagValue;
106    }
107
108    /**
109     * Method providing a primitive representation of this object suitable for encoding.
110     * @return a primitive representation of this object.
111     */
112    public abstract ASN1Primitive toASN1Primitive();
113}
114