001package org.apache.commons.ssl.org.bouncycastle.asn1;
002
003import java.io.ByteArrayOutputStream;
004import java.io.IOException;
005
006// import org.bouncycastle.util.Encodable;
007
008/**
009 * Base class for defining an ASN.1 object.
010 */
011public abstract class ASN1Object
012    implements ASN1Encodable // , Encodable
013{
014    /**
015     * Return the default BER or DER encoding for this object.
016     *
017     * @return BER/DER byte encoded object.
018     * @throws java.io.IOException on encoding error.
019     */
020    public byte[] getEncoded()
021        throws IOException
022    {
023        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
024        ASN1OutputStream      aOut = new ASN1OutputStream(bOut);
025
026        aOut.writeObject(this);
027
028        return bOut.toByteArray();
029    }
030
031    /**
032     * Return either the default for "BER" or a DER encoding if "DER" is specified.
033     *
034     * @param encoding name of encoding to use.
035     * @return byte encoded object.
036     * @throws IOException on encoding error.
037     */
038    public byte[] getEncoded(
039        String encoding)
040        throws IOException
041    {
042        if (encoding.equals(ASN1Encoding.DER))
043        {
044            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
045            DEROutputStream         dOut = new DEROutputStream(bOut);
046
047            dOut.writeObject(this);
048
049            return bOut.toByteArray();
050        }
051        else if (encoding.equals(ASN1Encoding.DL))
052        {
053            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
054            DLOutputStream          dOut = new DLOutputStream(bOut);
055
056            dOut.writeObject(this);
057
058            return bOut.toByteArray();
059        }
060
061        return this.getEncoded();
062    }
063
064    public int hashCode()
065    {
066        return this.toASN1Primitive().hashCode();
067    }
068
069    public boolean equals(
070        Object  o)
071    {
072        if (this == o)
073        {
074            return true;
075        }
076
077        if (!(o instanceof ASN1Encodable))
078        {
079            return false;
080        }
081
082        ASN1Encodable other = (ASN1Encodable)o;
083
084        return this.toASN1Primitive().equals(other.toASN1Primitive());
085    }
086
087    /**
088     * @deprecated use toASN1Primitive()
089     * @return the underlying primitive type.
090     */
091    public ASN1Primitive toASN1Object()
092    {
093        return this.toASN1Primitive();
094    }
095
096    /**
097     * Return true if obj is a byte array and represents an object with the given tag value.
098     *
099     * @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}