code: public static String escape(String string, char escape, boolean isPath)
     {
        try
        {
           BitSet validChars = isPath ? URISaveEx : URISave;
           byte[] bytes = string.getBytes("utf-8");
           StringBuilder out = new StringBuilder(bytes.length);
           for (int i = 0; i < bytes.length; i++)
           {
              int c = bytes[i] & 0xff;
              if (validChars.get(c) && c != escape)
              {
                 out.append((char)c);
              }
              else
              {
                 out.append(escape);
                 out.append(hexTable[(c >> 4) & 0x0f]);
                 out.append(hexTable[(c) & 0x0f]);
              }
           }
           return out.toString();
        }
        catch (UnsupportedEncodingException e)
        {
           throw new InternalError(e.toString());
        }
     }
docs: Does an URL encoding of the <code>string</code> using the <code>escape</code> character. The
  characters that don't need encoding are those defined 'unreserved' in section 2.3 of the 'URI
  generic syntax' RFC 2396, but without the escape character. If <code>isPath</code> is
  <code>true</code>, additionally the slash '/' is ignored, too.
  
  @param string
  the string to encode.
  @param escape
  the escape character.
  @param isPath
  if <code>true</code>, the string is treated as path
  @return the escaped string
  @throws NullPointerException
  if <code>string</code> is <code>null</code>.