001package org.apache.commons.ssl.org.bouncycastle.asn1;
002
003import java.io.IOException;
004
005/**
006 * Base class for ASN.1 primitive objects. These are the actual objects used to generate byte encodings.
007 */
008public abstract class ASN1Primitive
009    extends ASN1Object
010{
011    ASN1Primitive()
012    {
013
014    }
015
016    /**
017     * Create a base ASN.1 object from a byte stream.
018     *
019     * @param data the byte stream to parse.
020     * @return the base ASN.1 object represented by the byte stream.
021     * @exception IOException if there is a problem parsing the data.
022     */
023    public static ASN1Primitive fromByteArray(byte[] data)
024        throws IOException
025    {
026        ASN1InputStream aIn = new ASN1InputStream(data);
027
028        try
029        {
030            return aIn.readObject();
031        }
032        catch (ClassCastException e)
033        {
034            throw new IOException("cannot recognise object in stream");
035        }
036    }
037
038    public final boolean equals(Object o)
039    {
040        if (this == o)
041        {
042            return true;
043        }
044
045        return (o instanceof ASN1Encodable) && asn1Equals(((ASN1Encodable)o).toASN1Primitive());
046    }
047
048    public ASN1Primitive toASN1Primitive()
049    {
050        return this;
051    }
052
053    /**
054     * Return the current object as one which encodes using Distinguished Encoding Rules.
055     *
056     * @return a DER version of this.
057     */
058    ASN1Primitive toDERObject()
059    {
060        return this;
061    }
062
063    /**
064     * Return the current object as one which encodes using Definite Length encoding.
065     *
066     * @return a DL version of this.
067     */
068    ASN1Primitive toDLObject()
069    {
070        return this;
071    }
072
073    public abstract int hashCode();
074
075    abstract boolean isConstructed();
076
077    abstract int encodedLength() throws IOException;
078
079    abstract void encode(ASN1OutputStream out) throws IOException;
080
081    abstract boolean asn1Equals(ASN1Primitive o);
082}