private byte[] decodePEM(InputStream inStream,byte[] boundary_suffix) throws IOException {
  int ch;
  for (int i=1; i < PEM_BEGIN.length; ++i) {
    if (PEM_BEGIN[i] != (ch=inStream.read())) {
      throw new IOException("Incorrect PEM encoding: '-----BEGIN" + ((boundary_suffix == null) ? "" : new String(boundary_suffix)) + "' is expected as opening delimiter boundary.");
    }
  }
  if (boundary_suffix == null) {
    while ((ch=inStream.read()) != '\n') {
      if (ch == -1) {
        throw new IOException("Incorrect PEM encoding: EOF before content");
      }
    }
  }
 else {
    for (int i=0; i < boundary_suffix.length; i++) {
      if (boundary_suffix[i] != inStream.read()) {
        throw new IOException("Incorrect PEM encoding: '-----BEGIN" + new String(boundary_suffix) + "' is expected as opening delimiter boundary.");
      }
    }
    if ((ch=inStream.read()) == '\r') {
      ch=inStream.read();
    }
    if (ch != '\n') {
      throw new IOException("Incorrect PEM encoding: newline expected after " + "opening delimiter boundary");
    }
  }
  int size=1024;
  byte[] buff=new byte[size];
  int index=0;
  while ((ch=inStream.read()) != '-') {
    if (ch == -1) {
      throw new IOException("Incorrect Base64 encoding: EOF without closing delimiter");
    }
    buff[index++]=(byte)ch;
    if (index == size) {
      byte[] newbuff=new byte[size + 1024];
      System.arraycopy(buff,0,newbuff,0,size);
      buff=newbuff;
      size+=1024;
    }
  }
  if (buff[index - 1] != '\n') {
    throw new IOException("Incorrect Base64 encoding: newline expected before " + "closing boundary delimiter");
  }
  for (int i=1; i < PEM_END.length; ++i) {
    if (PEM_END[i] != inStream.read()) {
      throw badEnd(boundary_suffix);
    }
  }
  if (boundary_suffix == null) {
    while (((ch=inStream.read()) != -1) && (ch != '\n') && (ch != '\r')) {
    }
  }
 else {
    for (int i=0; i < boundary_suffix.length; i++) {
      if (boundary_suffix[i] != inStream.read()) {
        throw badEnd(boundary_suffix);
      }
    }
  }
  inStream.mark(1);
  while (((ch=inStream.read()) != -1) && (ch == '\n' || ch == '\r')) {
    inStream.mark(1);
  }
  inStream.reset();
  buff=Base64.decode(buff,index);
  if (buff == null) {
    throw new IOException("Incorrect Base64 encoding");
  }
  return buff;
}

private byte [ ] decodePEM ( InputStream inStream , byte [ ] boundary_suffix ) throws IOException { int ch ; for ( int i = 1 ; i < PEM_BEGIN . length ; ++ i ) { if ( PEM_BEGIN [ i ] != ( ch = inStream . read ( ) ) ) { throw new IOException ( "Incorrect PEM encoding: '-----BEGIN" + ( ( boundary_suffix == null ) ? "" : new String ( boundary_suffix ) ) + "' is expected as opening delimiter boundary." ) ; } } if ( boundary_suffix == null ) { while ( ( ch = inStream . read ( ) ) != '\n' ) { if ( ch == - 1 ) { throw new IOException ( "Incorrect PEM encoding: EOF before content" ) ; } } } else { for ( int i = 0 ; i < boundary_suffix . length ; i ++ ) { if ( boundary_suffix [ i ] != inStream . read ( ) ) { throw new IOException ( "Incorrect PEM encoding: '-----BEGIN" + new String ( boundary_suffix ) + "' is expected as opening delimiter boundary." ) ; } } if ( ( ch = inStream . read ( ) ) == '\r' ) { ch = inStream . read ( ) ; } if ( ch != '\n' ) { throw new IOException ( "Incorrect PEM encoding: newline expected after " + "opening delimiter boundary" ) ; } } int size = 1024 ; byte [ ] buff = new byte [ size ] ; int index = 0 ; while ( ( ch = inStream . read ( ) ) != '-' ) { if ( ch == - 1 ) { throw new IOException ( "Incorrect Base64 encoding: EOF without closing delimiter" ) ; } buff [ index ++ ] = ( byte ) ch ; if ( index == size ) { byte [ ] newbuff = new byte [ size + 1024 ] ; System . arraycopy ( buff , 0 , newbuff , 0 , size ) ; buff = newbuff ; size += 1024 ; } } if ( buff [ index - 1 ] != '\n' ) { throw new IOException ( "Incorrect Base64 encoding: newline expected before " + "closing boundary delimiter" ) ; } for ( int i = 1 ; i < PEM_END . length ; ++ i ) { if ( PEM_END [ i ] != inStream . read ( ) ) { throw badEnd ( boundary_suffix ) ; } } if ( boundary_suffix == null ) { while ( ( ( ch = inStream . read ( ) ) != - 1 ) && ( ch != '\n' ) && ( ch != '\r' ) ) { } } else { for ( int i = 0 ; i < boundary_suffix . length ; i ++ ) { if ( boundary_suffix [ i ] != inStream . read ( ) ) { throw badEnd ( boundary_suffix ) ; } } } inStream . mark ( 1 ) ; while ( ( ( ch = inStream . read ( ) ) != - 1 ) && ( ch == '\n' || ch == '\r' ) ) { inStream . mark ( 1 ) ; } inStream . reset ( ) ; buff = Base64 . decode ( buff , index ) ; if ( buff == null ) { throw new IOException ( "Incorrect Base64 encoding" ) ; } return buff ; }
method retrieves the pem encoded data from the stream and returns its decoded representation . method checks correctness of pem boundaries . it supposes that the first ' - ' of the opening boundary has already been read from the stream . so first of all it checks that the leading bytes are equal to " - - - - - begin " boundary prefix . than if boundary_suffix is not null , it checks that next bytes equal to boundary_suffix + new line char [ s ] ( [ cr ] lf ) . if boundary_suffix parameter is null , method supposes free suffix format and skips any bytes until the new line . < br > after the opening boundary has been read and checked , the method read base64 encoded data until closing pem boundary is not reached . < br > than it checks closing boundary - it should start with new line + " - - - - - end " + boundary_suffix . if boundary_suffix is null , any characters are skipped until the new line . < br > after this any trailing new line characters are skipped from the stream , base64 encoding is decoded and returned .
