Reading Base64-Encoded Certificates
The following example reads a file with Base64-encoded certificates,
which are each bounded at the beginning by
-----BEGIN CERTIFICATE-----
and
at the end by -----END CERTIFICATE-----
We convert the
FileInputStream
(which does not
support mark
and reset
) to a ByteArrayInputStream
(which
supports those methods), so that each call to generateCertificate
consumes
only one certificate, and the read position of the input stream is
positioned to the next certificate in the file: FileInputStream fis = new FileInputStream(filename);
DataInputStream dis = new DataInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
byte[] bytes = new byte[dis.available()];
dis.readFully(bytes);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
while (bais.available() > 0) {
Certificate cert = cf.generateCertificate(bais);
System.out.println(cert.toString());
}