public static String encodePartiallyEncoded(String encoded, boolean query) {
        if (encoded.length() == 0) {
            return encoded;
        }
        Matcher m = ENCODE_PATTERN.matcher(encoded);

        if (!m.find()) {
            return query ? HttpUtils.queryEncode(encoded) : HttpUtils.pathEncode(encoded);
        }

        int length = encoded.length();
        StringBuilder sb = new StringBuilder(length + 8);
        int i = 0;
        do {
            String before = encoded.substring(i, m.start());
            sb.append(query ? HttpUtils.queryEncode(before) : HttpUtils.pathEncode(before));
            sb.append(m.group());
            i = m.end();
        } while (m.find());
        String tail = encoded.substring(i, length);
        sb.append(query ? HttpUtils.queryEncode(tail) : HttpUtils.pathEncode(tail));
        return sb.toString();
    }
public static String encodePartiallyEncoded ( String encoded , boolean query ) { if ( encoded . length ( ) == 0 ) { return encoded ; } Matcher m = ENCODE_PATTERN . matcher ( encoded ) ; if ( ! m . find ( ) ) { return query ? HttpUtils . queryEncode ( encoded ) : HttpUtils . pathEncode ( encoded ) ; } int length = encoded . length ( ) ; StringBuilder sb = new StringBuilder ( length + 8 ) ; int i = 0 ; do { String before = encoded . substring ( i , m . start ( ) ) ; sb . append ( query ? HttpUtils . queryEncode ( before ) : HttpUtils . pathEncode ( before ) ) ; sb . append ( m . group ( ) ) ; i = m . end ( ) ; } while ( m . find ( ) ) ; String tail = encoded . substring ( i , length ) ; sb . append ( query ? HttpUtils . queryEncode ( tail ) : HttpUtils . pathEncode ( tail ) ) ; return sb . toString ( ) ; }
Encodes partially encoded string. Encode all values but those matching pattern
"percent char followed by two hexadecimal digits".

@param encoded fully or partially encoded string.
@return fully encoded string