Encode a string for use in the URL

@param toEncode
@return
1709
private static String encoder(final String toEncode) {
        try {
            return URLEncoder.encode(toEncode, URL_ENCODING);
        } catch (UnsupportedEncodingException ex) {
            LOG.warn("Failed to encode: {}", ex.getMessage(), ex);
            return "";
        }
    }
927566
public Object encode(Object o) {
        if(o instanceof String) {
            // create a new copy of the String instance            
            return new String((String)o);
        } else if(o instanceof CharSequence) {
            // convert CharSequence to String so that we have a new instance
            return String.valueOf(o);
        } else {
            return o;
        }
    }
219646
public static String decode(String encoded) {
    try {
      return URLDecoder.decode(encoded, ENCODING_FOR_URL);
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException(
          "Unable to decode URL entry via " + ENCODING_FOR_URL + ". This should not happen", e);
    }
  }
463128
public final CachedData encode(Object o) {
    byte[] b = null;
    int flags = 0;
    if (o instanceof String) {
      b = encodeString((String) o);
    } else if (o instanceof Long) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeLong((Long) o);
      }
      flags |= SPECIAL_LONG;
    } else if (o instanceof Integer) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeInt((Integer) o);
      }
      flags |= SPECIAL_INT;
    } else if (o instanceof Boolean) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeBoolean((Boolean) o);
      }
      flags |= SPECIAL_BOOLEAN;
    } else if (o instanceof Date) {
      b = this.transcoderUtils.encodeLong(((Date) o).getTime());
      flags |= SPECIAL_DATE;
    } else if (o instanceof Byte) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeByte((Byte) o);
      }
      flags |= SPECIAL_BYTE;
    } else if (o instanceof Float) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeInt(Float.floatToRawIntBits((Float) o));
      }
      flags |= SPECIAL_FLOAT;
    } else if (o instanceof Double) {
      if (this.primitiveAsString) {
        b = encodeString(o.toString());
      } else {
        b = this.transcoderUtils.encodeLong(Double.doubleToRawLongBits((Double) o));
      }
      flags |= SPECIAL_DOUBLE;
    } else if (o instanceof byte[]) {
      b = (byte[]) o;
      flags |= SPECIAL_BYTEARRAY;
    } else {
      b = serialize(o);
      flags |= SERIALIZED;
    }
    assert b != null;
    if (this.primitiveAsString) {
      // It is not be SERIALIZED,so change it to string type
      if ((flags & SERIALIZED) == 0) {
        flags = 0;
      }
    }
1031088
public static String encode(String value) {
        if (value == null) {
            return value;
        }
        try {
            value = URLEncoder.encode(value, Constants.UTF_8);
        } catch (UnsupportedEncodingException e) {
            // Do nothing - UTF-8 should always be supported
        }
        return value;
    }
89521
public synchronized String encode(String charset, boolean equalsForNullValue)
    {
        if (charset==null)
            charset=StringUtil.__ISO_8859_1;
        
        StringBuffer result = new StringBuffer(128);
        synchronized(result)
        {
            Iterator iter = entrySet().iterator();
            while(iter.hasNext())
            {
                Map.Entry entry = (Map.Entry)iter.next();
                
                String key = entry.getKey().toString();
                Object list = entry.getValue();
                int s=LazyList.size(list);
                
                if (s==0)
                {
                    result.append(encodeString(key,charset));
                    if(equalsForNullValue)
                        result.append('=');
                }
                else
                {
                    for (int i=0;i<s;i++)
                    {
                        if (i>0)
                            result.append('&');
                        Object val=LazyList.get(list,i);
                        result.append(encodeString(key,charset));

                        if (val!=null)
                        {
                            String str=val.toString();
                            if (str.length()>0)
                            {
                                result.append('=');
                                result.append(encodeString(str,charset));
                            }
                            else if (equalsForNullValue)
                                result.append('=');
                        }
                        else if (equalsForNullValue)
                            result.append('=');
                    }
                }
                if (iter.hasNext())
                    result.append('&');
            }
            return result.toString();
        }
    }
1363113
public static String encode(byte[] rawData) {
        StringBuilder buffer = new StringBuilder(4 * rawData.length / 3);

        int pos = 0;
        int iterations = rawData.length / 3;
        for (int i = 0; i < iterations; i++) {
            int value = ((rawData[pos++] & 0xFF) << 16) |
                    ((rawData[pos++] & 0xFF) << 8) | (rawData[pos++] & 0xFF);

            buffer.append(BASE64_ALPHABET[(value >>> 18) & 0x3F]);
            buffer.append(BASE64_ALPHABET[(value >>> 12) & 0x3F]);
            buffer.append(BASE64_ALPHABET[(value >>> 6) & 0x3F]);
            buffer.append(BASE64_ALPHABET[value & 0x3F]);
        }

        switch (rawData.length % 3) {
            case 1:
                buffer.append(BASE64_ALPHABET[(rawData[pos] >>> 2) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(rawData[pos] << 4) & 0x3F]);
                buffer.append("==");
                break;
            case 2:
                int value = ((rawData[pos++] & 0xFF) << 8) | (rawData[pos] & 0xFF);
                buffer.append(BASE64_ALPHABET[(value >>> 10) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(value >>> 4) & 0x3F]);
                buffer.append(BASE64_ALPHABET[(value << 2) & 0x3F]);
                buffer.append("=");
                break;
        }

        return buffer.toString();
    }
841723
public static String encode(final String s) {
        /*
         * try { return URLEncoder.encode(s, ENCODING_CHARSET); } catch (UnsupportedEncodingException use) {
         * log.error("Error ocurred encoding URI [" + s + "]", use); }
         */

        /*
         * Lotus Notes does not correctly strip angle brackets from cid uris. From RFC2392: A "cid" URL is converted to
         * the corresponding Content-ID message header [MIME] by removing the "cid:" prefix, converting the % encoded
         * character to their equivalent US-ASCII characters, and enclosing the remaining parts with an angle bracket
         * pair, "<" and ">". For example, "cid:foo4%25foo1@bar.net" corresponds to Content-ID: <foo4%25foo1@bar.net>
         * Reversing the process and converting URL special characters to their % encodings produces the original cid. A
         * "mid" URL is converted to a Message-ID or Message-ID/Content-ID pair in a similar fashion.
         */
        if (CompatibilityHints.isHintEnabled(CompatibilityHints.KEY_NOTES_COMPATIBILITY)
        		&& CID_PATTERN.matcher(s).matches()) {
        	
            return NOTES_CID_REPLACEMENT_PATTERN.matcher(s).replaceAll("");
        }
        return s;
    }
68133
private String encode(String value) {
    return this.encode.isEncodingRequired() ? UriUtils.encode(value, this.charset) : value;
  }
618524
public static String urlEncode(String part) {
		try {
			return URLEncoder.encode(part, Charsets.UTF_8_NAME);
		} catch (UnsupportedEncodingException ignored) { // NOSONAR
			// this exception is only for detecting and handling invalid inputs
			return null;
		}
	}
892483
public static String encodeQueryString(String queryString) {
        if (StringUtils.isBlank(queryString)) {
            return queryString;
        }
        return new QueryString(queryString).toString();
    }
1367562
public static String urlEncode(String s) {
		if (s == null) {
			return "";
		}
		String encoded = s;
		try {
			encoded = URLEncoder.encode(s, Config.DEFAULT_ENCODING);
		} catch (UnsupportedEncodingException ex) {
			logger.error(null, ex);
		}
		return encoded;
	}
971206
private static String privateBase64Encoder(String toEncode, int flags) {
		byte[] data = null;
		try {
			data = toEncode.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		if (flags == -1) {
			flags = Base64.DEFAULT;
		}

		return Base64.encodeToString(data, flags);
	}
863282
public static String encode(String string) {
        int n = string.length();
        char character;
        StringBuffer buffer = new StringBuffer();
        // loop over all the characters of the String.
        for (int i = 0; i < n; i++) {
            character = string.charAt(i);
            // the Htmlcode of these characters are added to a StringBuffer one by one
            if (character < 256) {
                buffer.append(htmlCode[character]);
            }
            else {
                // Improvement posted by Joachim Eyrich
                buffer.append("&#").append((int)character).append(';');
            }
        }
        return buffer.toString();
    }
839633
public static String encodeUrl(String stringToEncode) {
        try {
            return URLEncoder.encode(stringToEncode, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            throw new RuntimeException(e1);
        }
    }
1133188
public static String urlquote(String text) {
        try {
            return URLEncoder.encode(text, "UTF-8").replace("+", "%20");
        } catch (UnsupportedEncodingException ex) {
            throw new RuntimeException(ex);
        }
    }
1363587
public static String percentEncode(String source) throws AuthException {
    try {
      return URLEncoder.encode(source, "UTF-8")
          .replace("+", "%20")
          .replace("*", "%2A")
          .replace("%7E", "~");
    } catch (UnsupportedEncodingException ex) {
      throw new AuthException("cannot encode value '" + source + "'", ex);
    }
  }