public String base64UrlEncode(byte[] data){
  String encodedData=new String(Base64.encode(data,Base64.URL_SAFE));
  encodedData=encodedData.replace("=","");
  encodedData=encodedData.replace("\n","");
  return encodedData;
}

public String base64UrlEncode ( byte [ ] data ) { String encodedData = new String ( Base64 . encode ( data , Base64 . URL_SAFE ) ) ; encodedData = encodedData . replace ( "=" , "" ) ; encodedData = encodedData . replace ( "\n" , "" ) ; return encodedData ; }
encodes a string with base64url encoding it also removes characters which must be removed according to the jose spec : http : / / tools . ietf . org / html / draft - ietf - jose - json - web - signature - 29 # appendix - c
