code: public static String encodeIllegalCharacters(String uri) {
          StringBuilder result = new StringBuilder();
          int length = uri.length();
          for (int i = 0; i < length; i++) {
              char character = uri.charAt(i);
              if (character == '%') {
                  // Encode invalid escape sequences
                  if (i >= length - 2 || !isHex(uri.charAt(i + 1)) || !isHex(uri.charAt(i + 2))) {
                      result.append("%25");
                  } else {
                      result.append('%');
                  }
              } else {
                  int j = (int) character;
                  if (j >= CONVERSION_TABLE_SIZE || j < 0) {
                      result.append(encode(character));
                  } else {
                      result.append(CONVERSION_TABLE[j]);
                  }
              }
          }
          return result.toString();
      }
docs: Fixes common mistakes in URI by replacing all illegal characters by their encoded value.
  
  @param uri
  the URI to fix
  @return the fixed URI