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

package org.bouncycastle.cms;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Encodable;

public class PKCS7TypedStream extends CMSTypedStream
{
    private final ASN1Encodable content;
    
    public PKCS7TypedStream(final ASN1ObjectIdentifier asn1ObjectIdentifier, final ASN1Encodable content) throws IOException {
        super(asn1ObjectIdentifier);
        this.content = content;
    }
    
    public ASN1Encodable getContent() {
        return this.content;
    }
    
    @Override
    public InputStream getContentStream() {
        try {
            return this.getContentStream(this.content);
        }
        catch (final IOException ex) {
            throw new CMSRuntimeException("unable to convert content to stream: " + ex.getMessage(), ex);
        }
    }
    
    @Override
    public void drain() throws IOException {
        this.content.toASN1Primitive();
    }
    
    private InputStream getContentStream(final ASN1Encodable asn1Encodable) throws IOException {
        final byte[] encoded = asn1Encodable.toASN1Primitive().getEncoded("DER");
        int offset = 0;
        if ((encoded[offset++] & 0x1F) == 0x1F) {
            while ((encoded[offset++] & 0x80) != 0x0) {}
        }
        final byte b = encoded[offset++];
        if ((b & 0x80) != 0x0) {
            offset += (b & 0x7F);
        }
        return new ByteArrayInputStream(encoded, offset, encoded.length - offset);
    }
}
