private static byte[] decodePem(InputStream keyInputStream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(keyInputStream));
    try {
      String line;
      while ((line = reader.readLine()) != null) {
        if (line.contains("-----BEGIN ")) {
          return readBytes(reader, line.trim().replace("BEGIN", "END"));
        }
      }
      throw new IOException("PEM is invalid: no begin marker");
    } finally {
      reader.close();
    }
  }
private static byte [ ] decodePem ( InputStream keyInputStream ) throws IOException { BufferedReader reader = new BufferedReader ( new InputStreamReader ( keyInputStream ) ) ; try { String line ; while ( ( line = reader . readLine ( ) ) != null ) { if ( line . contains ( "-----BEGIN " ) ) { return readBytes ( reader , line . trim ( ) . replace ( "BEGIN" , "END" ) ) ; } } throw new IOException ( "PEM is invalid: no begin marker" ) ; } finally { reader . close ( ) ; } }
All credits to belong to them.