// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.asn1.cms;

import org.bouncycastle.asn1.BERTaggedObject;
import org.bouncycastle.asn1.DLTaggedObject;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DLSequence;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.BERSequence;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Object;

public class ContentInfo extends ASN1Object implements CMSObjectIdentifiers
{
    private final ASN1ObjectIdentifier contentType;
    private final ASN1Encodable content;
    private final boolean isDefiniteLength;
    
    public static ContentInfo getInstance(final Object o) {
        if (o instanceof ContentInfo) {
            return (ContentInfo)o;
        }
        if (o != null) {
            return new ContentInfo(ASN1Sequence.getInstance(o));
        }
        return null;
    }
    
    public static ContentInfo getInstance(final ASN1TaggedObject asn1TaggedObject, final boolean b) {
        return new ContentInfo(ASN1Sequence.getInstance(asn1TaggedObject, b));
    }
    
    private ContentInfo(final ASN1Sequence asn1Sequence) {
        if (asn1Sequence.size() < 1 || asn1Sequence.size() > 2) {
            throw new IllegalArgumentException("Bad sequence size: " + asn1Sequence.size());
        }
        this.contentType = (ASN1ObjectIdentifier)asn1Sequence.getObjectAt(0);
        if (asn1Sequence.size() > 1) {
            final ASN1TaggedObject instance = ASN1TaggedObject.getInstance(asn1Sequence.getObjectAt(1), 128);
            if (!instance.isExplicit() || instance.getTagNo() != 0) {
                throw new IllegalArgumentException("Bad tag for 'content'");
            }
            this.content = instance.getExplicitBaseObject();
        }
        else {
            this.content = null;
        }
        this.isDefiniteLength = !(asn1Sequence instanceof BERSequence);
    }
    
    public ContentInfo(final ASN1ObjectIdentifier contentType, final ASN1Encodable content) {
        if (contentType == null) {
            throw new NullPointerException("'contentType' cannot be null");
        }
        this.contentType = contentType;
        if ((this.content = content) != null) {
            final ASN1Primitive asn1Primitive = content.toASN1Primitive();
            this.isDefiniteLength = (asn1Primitive instanceof DEROctetString || asn1Primitive instanceof DLSequence || asn1Primitive instanceof DERSequence);
        }
        else {
            this.isDefiniteLength = true;
        }
    }
    
    public ASN1ObjectIdentifier getContentType() {
        return this.contentType;
    }
    
    public ASN1Encodable getContent() {
        return this.content;
    }
    
    public boolean isDefiniteLength() {
        return this.isDefiniteLength;
    }
    
    @Override
    public ASN1Primitive toASN1Primitive() {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector(2);
        asn1EncodableVector.add(this.contentType);
        if (this.content != null) {
            if (this.isDefiniteLength) {
                asn1EncodableVector.add(new DLTaggedObject(0, this.content));
            }
            else {
                asn1EncodableVector.add(new BERTaggedObject(0, this.content));
            }
        }
        return this.isDefiniteLength ? new DLSequence(asn1EncodableVector) : new BERSequence(asn1EncodableVector);
    }
}
