code: public static String urlEncode(String url, Pattern unsafe, Charset charset) {
          StringBuffer sb = new StringBuffer(url.length());
          Matcher matcher = unsafe.matcher(url);
          while (matcher.find()) {
              String str = matcher.group(0);
              byte[] bytes = str.getBytes(charset);
              StringBuilder escaped = new StringBuilder(str.length() * 3);
  
              for (byte aByte : bytes) {
                  escaped.append('%');
                  char ch = Character.forDigit((aByte >> 4) & 0xF, 16);
                  escaped.append(ch);
                  ch = Character.forDigit(aByte & 0xF, 16);
                  escaped.append(ch);
              }
  
              matcher.appendReplacement(sb, Matcher.quoteReplacement(escaped.toString().toLowerCase()));
          }
  
          matcher.appendTail(sb);
          return sb.toString();
      }
docs: Replaces the unsafe characters in url with url-encoded values.
  This is based on {@link java.net.URLEncoder#encode(String, String)}
  @param url The url to encode
  @param unsafe Regex pattern of unsafe caracters
  @param charset
  @return An encoded url string