parse a certificate file into ArrayList of certificates - Java Security

Java examples for Security:Certificate

Description

parse a certificate file into ArrayList of certificates

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

import java.util.ArrayList;

public class Main {
    public static void main(String[] argv) throws Exception {
        File f = new File("Main.java");
        System.out.println(readCertificate(f));
    }//from  ww  w .  ja  va2  s .com

    /**
     * parse a certificate file into ArrayList of certificates
     */
    public static ArrayList<Certificate> readCertificate(File f)
            throws CertificateException {
        ArrayList<Certificate> certs = new ArrayList<Certificate>();
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        BufferedInputStream in;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            while (in.available() > 0) {
                Certificate cert = cf.generateCertificate(in);
                certs.add(cert);
            }
            in.close();
            return certs;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials