001package org.apache.commons.ssl.org.bouncycastle.asn1;
002
003import java.io.IOException;
004
005/**
006 * A NULL object - use DERNull.INSTANCE for populating structures.
007 */
008public abstract class ASN1Null
009    extends ASN1Primitive
010{
011    /**
012     * Return an instance of ASN.1 NULL from the passed in object.
013     * <p>
014     * Accepted inputs:
015     * <ul>
016     * <li> null &rarr; null
017     * <li> {@link ASN1Null} object
018     * <li> a byte[] containing ASN.1 NULL object
019     * </ul>
020     * </p>
021     *
022     * @param o object to be converted.
023     * @return an instance of ASN1Null, or null.
024     * @exception IllegalArgumentException if the object cannot be converted.
025     */
026    public static ASN1Null getInstance(Object o)
027    {
028        if (o instanceof ASN1Null)
029        {
030            return (ASN1Null)o;
031        }
032
033        if (o != null)
034        {
035            try
036            {
037                return ASN1Null.getInstance(ASN1Primitive.fromByteArray((byte[])o));
038            }
039            catch (IOException e)
040            {
041                throw new IllegalArgumentException("failed to construct NULL from byte[]: " + e.getMessage());
042            }
043            catch (ClassCastException e)
044            {
045                throw new IllegalArgumentException("unknown object in getInstance(): " + o.getClass().getName());
046            }
047        }
048
049        return null;
050    }
051
052    public int hashCode()
053    {
054        return -1;
055    }
056
057    boolean asn1Equals(
058        ASN1Primitive o)
059    {
060        if (!(o instanceof ASN1Null))
061        {
062            return false;
063        }
064        
065        return true;
066    }
067
068    abstract void encode(ASN1OutputStream out)
069        throws IOException;
070
071    public String toString()
072    {
073         return "NULL";
074    }
075}