catch (IOException e)
if (crls.size() > 0)
    public static OutputStream replaceCertificatesAndCRLs(        InputStream   original,        CertStore     certsAndCrls,        OutputStream  out)        throws CMSException, IOException    {        ASN1StreamParser in = new ASN1StreamParser(original, CMSUtils.getMaximumMemory());        ContentInfoParser contentInfo = new ContentInfoParser((ASN1SequenceParser)in.readObject());        SignedDataParser signedData = SignedDataParser.getInstance(contentInfo.getContent(DERTags.SEQUENCE));        BERSequenceGenerator sGen = new BERSequenceGenerator(out);        sGen.addObject(CMSObjectIdentifiers.signedData);        BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);        // version number        sigGen.addObject(signedData.getVersion());        // digests        sigGen.getRawOutputStream().write(signedData.getDigestAlgorithms().getDERObject().getEncoded());        // encap content info        ContentInfoParser encapContentInfo = signedData.getEncapContentInfo();        BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());        eiGen.addObject(encapContentInfo.getContentType());        ASN1OctetStringParser octs = (ASN1OctetStringParser)encapContentInfo.getContent(DERTags.OCTET_STRING);        if (octs != null)        {            BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, true);            byte[]                  inBuffer = new byte[4096];            byte[]                  outBuffer = new byte[4096];            InputStream             inOctets = octs.getOctetStream();            OutputStream            outOctets = octGen.getOctetOutputStream(outBuffer);            int len;            while ((len = inOctets.read(inBuffer, 0, inBuffer.length)) >= 0)            {                outOctets.write(inBuffer, 0, len);            }            outOctets.close();        }        eiGen.close();        //        // skip existing certs and CRLs        //        ASN1SetParser set = signedData.getCertificates();        if (set != null)        {            set.getDERObject();        }        set = signedData.getCrls();        if (set != null)        {            set.getDERObject();        }        //        // replace the certs and crls in the SignedData object        //        ASN1EncodableVector v = new ASN1EncodableVector();        try        {            Iterator  it = certsAndCrls.getCertificates(null).iterator();            while (it.hasNext())            {                X509Certificate c = (X509Certificate)it.next();                v.add(makeObj(c.getEncoded()));            }        }        catch (CertStoreException e)        {            throw new CMSException("error getting certs from certStore", e);        }        catch (IOException e)        {            throw new CMSException("error processing certs", e);        }        catch (CertificateEncodingException e)        {            throw new CMSException("error encoding certs", e);        }        if (v.size() > 0)        {            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 0, new DERSet(v)).getEncoded());        }        v = new ASN1EncodableVector();        try        {            Iterator    it = certsAndCrls.getCRLs(null).iterator();            while (it.hasNext())            {                X509CRL c = (X509CRL)it.next();                v.add(makeObj(c.getEncoded()));            }        }        catch (CertStoreException e)        {            throw new CMSException("error getting crls from certStore", e);        }        catch (IOException e)        {            throw new CMSException("error processing crls", e);        }        catch (CRLException e)        {            throw new CMSException("error encoding crls", e);        }        if (v.size() > 0)        {            sigGen.getRawOutputStream().write(new DERTaggedObject(false, 1, new DERSet(v)).getEncoded());        }        sigGen.getRawOutputStream().write(signedData.getSignerInfos().getDERObject().getEncoded());        sigGen.close();        sGen.close();        return out;    }