0
\$\begingroup\$

I have got an array of javax.security.cert.X509Certificate and need to convert it into an array of java.security.cert.X509Certificate.

My thoughts when I implemented the following code was that I must keep the order of the certificate thus I used a LinkedList. In the first step I am converting all of them into the new data type and appending it to the LinkedList. Afterwards I create temporary array of a size which equals the amount of certificates in LinkedList. Iterating over the LinkedList allows me to put each single certificate into the temp array and finally I can set my my private java.security.cert.X509Certificate array field. I have got the feeling that it is unnecessarily verbose and not efficient and could be shorter.

private X509Certificate[] x509Certificates;

private void setX509Certificates(javax.security.cert.X509Certificate[] certs)
{
  LinkedList<X509Certificate> x509CertificateLinkedList = new LinkedList<>();

  try
  {
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

    for (javax.security.cert.X509Certificate cert : certs)
    {
      try
      {
        ByteArrayInputStream bais = new ByteArrayInputStream(cert.getEncoded());
        X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(bais);
        x509CertificateLinkedList.add(x509Certificate);
      }
      catch (CertificateEncodingException e)
      {
        e.printStackTrace();
      }
    }

    X509Certificate[] tempX509Certificates = new X509Certificate[x509CertificateLinkedList.size()];

    for (int i = 0; i < x509CertificateLinkedList.toArray().length; i++)
    {
      if (x509CertificateLinkedList.get(i) != null)
      {
        tempX509Certificates[i] = x509CertificateLinkedList.get(i);
      }
    }

    this.x509Certificates = tempX509Certificates;
  }
  catch (CertificateException e)
  {
    e.printStackTrace();
  }
}
\$\endgroup\$
0
1
\$\begingroup\$
private X509Certificate[] x509Certificates;

private void setX509Certificates(javax.security.cert.X509Certificate[] certs) throws CertificateException {
    CertificateFactory certificateFactory = certificateFactory = CertificateFactory.getInstance("X.509");
    List<X509Certificate> x509List = new ArrayList<>();

    for (javax.security.cert.X509Certificate cert : certs) {
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(cert.getEncoded());
            X509Certificate x509Certificate = (X509Certificate) certificateFactory.generateCertificate(bais);
            if (x509Certificate != null) {
                x509List.add(x509Certificate);
            }
        } catch (CertificateEncodingException e) {
            e.printStackTrace(System.err);
        }
    }
    this.x509Certificates = x509List.toArray(new X509Certificate[]{});
}

Linked List which you have used for temporary purpose. This could be achieved in a single for loop. Since there will not be any duplicate certificate it is safe to use ArrayList for collecting the certifcates.

I am not sure your exception handling strategy. So I am throwing the CertificateException you are free to change it based on your exception strategy.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.