code: public static String encodeString(String string, Charset charset) {
          if (charset == null)
              charset = ENCODING;
          byte[] bytes = null;
          bytes = string.getBytes(charset);
  
          int len = bytes.length;
          byte[] encoded = new byte[bytes.length * 3];
          int n = 0;
          boolean noEncode = true;
  
          for (int i = 0; i < len; i++) {
              byte b = bytes[i];
  
              if (b == ' ') {
                  noEncode = false;
                  encoded[n++] = (byte) '+';
              } else if (b >= 'a' && b <= 'z' ||
                      b >= 'A' && b <= 'Z' ||
                      b >= '0' && b <= '9') {
                  encoded[n++] = b;
              } else {
                  noEncode = false;
                  encoded[n++] = (byte) '%';
                  byte nibble = (byte) ((b & 0xf0) >> 4);
                  if (nibble >= 10)
                      encoded[n++] = (byte) ('A' + nibble - 10);
                  else
                      encoded[n++] = (byte) ('0' + nibble);
                  nibble = (byte) (b & 0xf);
                  if (nibble >= 10)
                      encoded[n++] = (byte) ('A' + nibble - 10);
                  else
                      encoded[n++] = (byte) ('0' + nibble);
              }
          }
  
          if (noEncode)
              return string;
  
          return new String(encoded, 0, n, charset);
      }
docs: Perform URL encoding.
  
  @param string  the string to encode
  @param charset the charset to use for encoding
  @return encoded string.