private byte [ ] maybeDecodeBase64 ( byte [ ] byteArray ) { try { String pem = new String ( byteArray ) ; // Remove required begin/end lines. pem = pem . substring ( BEGIN_CERT_LINE_LENGTH , pem . length ( ) - END_CERT_LINE_LENGTH ) ; return Base64 . getDecoder ( ) . decode ( pem ) ; } catch ( Exception e ) { // Not a valid PEM encoded certificate, return original array. return byteArray ; } }
Test whether array is a Base64 - encoded certificate . If so return the decoded content instead of the specified array .