private static String replace(String string, boolean replaceSlash) {
        if (Strings.isNullOrEmpty(string)) {
            return string;
        }
        StringBuffer buffer = new StringBuffer(string.length());
        Matcher matcher = TOKEN_REPLACE_PATTERN.matcher(string);
        while (matcher.find()) {
            String replacement = matcher.group(0);

            if ("+".equals(replacement)) {
                replacement = "%20";
            } else if ("*".equals(replacement)) {
                replacement = "%2A";
            } else if ("%7E".equals(replacement)) {
                replacement = "~";
            } else if (replaceSlash && "%2F".equals(replacement)) {
                replacement = "/";
            }

            matcher.appendReplacement(buffer, replacement);
        }

        matcher.appendTail(buffer);
        return buffer.toString();
    }
private static String replace ( String string , boolean replaceSlash ) { if ( Strings . isNullOrEmpty ( string ) ) { return string ; } StringBuffer buffer = new StringBuffer ( string . length ( ) ) ; Matcher matcher = TOKEN_REPLACE_PATTERN . matcher ( string ) ; while ( matcher . find ( ) ) { String replacement = matcher . group ( 0 ) ; if ( "+" . equals ( replacement ) ) { replacement = "%20" ; } else if ( "*" . equals ( replacement ) ) { replacement = "%2A" ; } else if ( "%7E" . equals ( replacement ) ) { replacement = "~" ; } else if ( replaceSlash && "%2F" . equals ( replacement ) ) { replacement = "/" ; } matcher . appendReplacement ( buffer , replacement ) ; } matcher . appendTail ( buffer ) ; return buffer . toString ( ) ; }
Replaces the special characters in the URLEncoded string with the replacement values defined by the spec.

Partially copied from https://github.com/aws/aws-sdk-java: com.amazonaws.util.SdkHttpUtils (2017-05-19)

@param string the string to replace characters in
@param replaceSlash whether the encoded '/' should be replaced
@return the string after replacements