try { if (encoding.equalsIgnoreCase( "PkiPath" )) { ASN1InputStream derInStream = new ASN1InputStream(inStream); DERObject derObject = derInStream.readObject(); if (!(derObject instanceof ASN1Sequence)) { throw new CertificateException("input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath" ); } Enumeration e = ((ASN1Sequence)derObject).getObjects(); InputStream certInStream; ByteArrayOutputStream outStream; DEROutputStream derOutStream; certificates = new ArrayList(); CertificateFactory certFactory= CertificateFactory.getInstance( "X.509", "BC" ); while ( e.hasMoreElements() ) { outStream = new ByteArrayOutputStream(); derOutStream = new DEROutputStream(outStream);  derOutStream.writeObject(e.nextElement()); derOutStream.close();  certInStream = new ByteArrayInputStream(outStream.toByteArray()); certificates.add(0,certFactory.generateCertificate(certInStream)); } } else if (encoding.equalsIgnoreCase("PKCS7") || encoding.equalsIgnoreCase("PEM")) { inStream = new BufferedInputStream(inStream); certificates = new ArrayList(); CertificateFactory certFactory= CertificateFactory.getInstance( "X.509", "BC" ); Certificate cert = null; while ((cert = certFactory.generateCertificate(inStream)) != null) { certificates.add(cert); } } else { throw new CertificateException( "unsupported encoding: " + encoding); } } catch (IOException ex) { throw new CertificateException( "IOException throw while decoding CertPath:\n" + ex.toString() ); } catch (NoSuchProviderException ex ) { throw new CertificateException( "BouncyCastle provider not found while trying to get a CertificateFactory:\n" + ex.toString() ); }  this.certificates = sortCerts(certificates);
this.certificates = sortCerts(new ArrayList(certificates));
    PKIXCertPath(        InputStream inStream,        String encoding)        throws CertificateException    {        super("X.509");        try {            if (encoding.equalsIgnoreCase( "PkiPath" ))            {                ASN1InputStream derInStream = new ASN1InputStream(inStream);                DERObject derObject = derInStream.readObject();                if (!(derObject instanceof ASN1Sequence))                {                    throw new CertificateException("input stream does not contain a ASN1 SEQUENCE while reading PkiPath encoded data to load CertPath" );                }                Enumeration e = ((ASN1Sequence)derObject).getObjects();                InputStream certInStream;                ByteArrayOutputStream outStream;                DEROutputStream derOutStream;                certificates = new ArrayList();                CertificateFactory certFactory= CertificateFactory.getInstance( "X.509", "BC" );                while ( e.hasMoreElements() ) {                    outStream = new ByteArrayOutputStream();                    derOutStream = new DEROutputStream(outStream);                            derOutStream.writeObject(e.nextElement());                    derOutStream.close();                        certInStream = new ByteArrayInputStream(outStream.toByteArray());                    certificates.add(0,certFactory.generateCertificate(certInStream));                }            }            else if (encoding.equalsIgnoreCase("PKCS7") || encoding.equalsIgnoreCase("PEM"))            {                inStream = new BufferedInputStream(inStream);                certificates = new ArrayList();                CertificateFactory certFactory= CertificateFactory.getInstance( "X.509", "BC" );                Certificate cert = null;                while ((cert = certFactory.generateCertificate(inStream)) != null)                {                    certificates.add(cert);                }            }            else            {                throw new CertificateException( "unsupported encoding: " + encoding);            }        }        catch (IOException ex)         {            throw new CertificateException( "IOException throw while decoding CertPath:\n" + ex.toString() );         }        catch (NoSuchProviderException ex )         {            throw new CertificateException( "BouncyCastle provider not found while trying to get a CertificateFactory:\n" + ex.toString() );         }                this.certificates = sortCerts(certificates);    }