PemToDerConverter { public static byte[] convert(final byte[] pemEncoded) { try (ByteArrayInputStream bais = new ByteArrayInputStream(pemEncoded); Reader reader = new InputStreamReader(bais); PemReader pemReader = new PemReader(reader)) { PemObject pemObject = pemReader.readPemObject(); if (pemObject == null) { throw new DSSException("Unable to read PEM Object"); } byte[] binaries = pemObject.getContent(); ByteArrayOutputStream os = new ByteArrayOutputStream(); os.write(binaries, 0, binaries.length); return os.toByteArray(); } catch (IOException e) { throw new DSSException("Unable to convert the CRL to DER", e); } } private  PemToDerConverter(); static byte[] convert(final byte[] pemEncoded);  }
@Test public void testException() { Exception exception = assertThrows(DSSException.class, () -> { PemToDerConverter.convert(new byte[] {}); }); assertEquals("Unable to read PEM Object", exception.getMessage()); }
@Test public void pemFile() throws IOException { try (InputStream is = AbstractTestCRLUtils.class.getResourceAsStream("/belgium2.pem.crl")) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Streams.pipeAll(is, baos); byte[] converted = PemToDerConverter.convert(baos.toByteArray()); assertTrue(converted != null && converted.length > 0); } }