code: public static StringBuilder encodeString(StringBuilder buf,
                                               String path,
                                               String encode) {
          if (buf == null) {
              for (int i = 0; i < path.length(); i++) {
                  char c = path.charAt(i);
                  if (c == '%' || encode.indexOf(c) >= 0) {
                      buf = new StringBuilder(path.length() << 1);
                      break;
                  }
              }
              if (buf == null)
                  return null;
          }
  
          for (int i = 0; i < path.length(); i++) {
              char c = path.charAt(i);
              if (c == '%' || encode.indexOf(c) >= 0) {
                  buf.append('%');
                  StringUtils.append(buf, (byte) (0xff & c), 16);
              } else
                  buf.append(c);
          }
  
          return buf;
      }
docs: Encode a URI path.
  
  @param path   The path the encode
  @param buf    StringBuilder to encode path into (or null)
  @param encode String of characters to encode. % is always encoded.
  @return The StringBuilder or null if no substitutions required.