protected CharSequence encodeUriQuery(CharSequence in, boolean encodePercent) {
    //Note that I can't simply use URI.java to encode because it will escape pre-existing escaped things.
    StringBuilder outBuf = null;
    Formatter formatter = null;
    for(int i = 0; i < in.length(); i++) {
      char c = in.charAt(i);
      boolean escape = true;
      if (c < 128) {
        if (asciiQueryChars.get(c) && !(encodePercent && c == '%')) {
          escape = false;
        }
      } else if (!Character.isISOControl(c) && !Character.isSpaceChar(c)) {//not-ascii
        escape = false;
      }
      if (!escape) {
        if (outBuf != null)
          outBuf.append(c);
      } else {
        //escape
        if (outBuf == null) {
          outBuf = new StringBuilder(in.length() + 5*3);
          outBuf.append(in,0,i);
          formatter = new Formatter(outBuf);
        }
        //leading %, 0 padded, width 2, capital hex
        formatter.format("%%%02X",(int)c);//TODO
      }
    }
    return outBuf != null ? outBuf : in;
  }
Encodes characters in the query or fragment part of the URI.

<p>Unfortunately, an incoming URI sometimes has characters disallowed by the spec.  HttpClient
insists that the outgoing proxied request has a valid URI because it uses Java's {@link URI}.
To be more forgiving, we must escape the problematic characters.  See the URI class for the
spec.

@param in example: name=value&amp;foo=bar#fragment
@param encodePercent determine whether percent characters need to be encoded
src/main/java/org/mitre/dsmiley/httpproxy/ProxyServlet.java