code: public static X509Certificate readCertificate(BufferedReader reader)
              throws IOException, GeneralSecurityException {
          String line;
          StringBuffer buff = new StringBuffer();
          boolean isCert = false;
          boolean isKey = false;
          boolean notNull = false;
          while ((line = reader.readLine()) != null) {
              // Skip key info, if any
              if (line.indexOf("BEGIN RSA PRIVATE KEY") != -1 ||
                   line.indexOf("BEGIN PRIVATE KEY") != -1) {
                  isKey = true;
                  continue;
              } else if (isKey && (line.indexOf("END RSA PRIVATE KEY") != -1 ||
                                   line.indexOf("END PRIVATE KEY") != -1)) {
                  isKey = false;
                  continue;
              } else if (isKey)
                  continue;
  
              notNull = true;
              if (line.indexOf("BEGIN CERTIFICATE") != -1) {
                  isCert = true;
              } else if (isCert && line.indexOf("END CERTIFICATE") != -1) {
                  byte[] data = Base64.decode(buff.toString().getBytes());
                  return loadCertificate(new ByteArrayInputStream(data));
              } else if (isCert) {
                  buff.append(line);
              }
          }
          if (notNull && !isCert) {
              throw new GeneralSecurityException(
                      "Certificate needs to start with "
                              + " BEGIN CERTIFICATE");
          }
          return null;
      }
docs: Loads a X.509 certificate from the specified reader. The certificate
  contents must start with "BEGIN CERTIFICATE" line and end with "END
  CERTIFICATE" line, and be in PEM/Base64 format.
  <p>
  This function does not close the input stream.
  
  @param reader the stream from which load the certificate.
  @return the loaded certificate or null if there was no certificate in the
  stream or the stream is closed.
  @throws IOException              if I/O error occurs
  @throws GeneralSecurityException if security problems occurs.