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

package org.bouncycastle.tsp.cms;

import org.bouncycastle.asn1.ASN1IA5String;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.cms.TimeStampedData;
import org.bouncycastle.asn1.cms.Evidence;
import org.bouncycastle.asn1.cms.TimeStampTokenEvidence;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.DERIA5String;
import org.bouncycastle.asn1.cms.TimeStampAndCRL;
import org.bouncycastle.asn1.BEROctetString;
import java.io.IOException;
import java.io.OutputStream;
import org.bouncycastle.util.io.Streams;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import org.bouncycastle.cms.CMSException;
import java.io.InputStream;
import org.bouncycastle.tsp.TimeStampToken;

public class CMSTimeStampedDataGenerator extends CMSTimeStampedGenerator
{
    public CMSTimeStampedData generate(final TimeStampToken timeStampToken) throws CMSException {
        return this.generate(timeStampToken, (InputStream)null);
    }
    
    public CMSTimeStampedData generate(final TimeStampToken timeStampToken, final byte[] buf) throws CMSException {
        return this.generate(timeStampToken, new ByteArrayInputStream(buf));
    }
    
    public CMSTimeStampedData generate(final TimeStampToken timeStampToken, final InputStream inputStream) throws CMSException {
        ASN1OctetString asn1OctetString = null;
        if (inputStream != null) {
            final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                Streams.pipeAll(inputStream, byteArrayOutputStream);
            }
            catch (final IOException ex) {
                throw new CMSException("exception encapsulating content: " + ex.getMessage(), ex);
            }
            if (byteArrayOutputStream.size() != 0) {
                asn1OctetString = new BEROctetString(byteArrayOutputStream.toByteArray());
            }
        }
        final TimeStampAndCRL timeStampAndCRL = new TimeStampAndCRL(timeStampToken.toCMSSignedData().toASN1Structure());
        ASN1IA5String asn1IA5String = null;
        if (this.dataUri != null) {
            asn1IA5String = new DERIA5String(this.dataUri.toString());
        }
        return new CMSTimeStampedData(new ContentInfo(CMSObjectIdentifiers.timestampedData, new TimeStampedData(asn1IA5String, this.metaData, asn1OctetString, new Evidence(new TimeStampTokenEvidence(timeStampAndCRL)))));
    }
}
