5
\$\begingroup\$

This is working code for a URI template (RFC 6570) implementation; when the character to render is not within a specific character set, it is needed to grab the UTF-8 representation of that character and encode each byte as a percent-encoded sequence.

The question is whether something more efficient than this exists?

private static String encodeChar(final char c)
{
    final String tmp = new String(new char[] { c });
    final byte[] bytes = tmp.getBytes(Charset.forName("UTF-8"));
    final StringBuilder sb = new StringBuilder();
    for (final byte b: bytes)
        sb.append('%').append(UnsignedBytes.toString(b, 16));
    return sb.toString();
}

Note: UnsignedBytes.toString() is from Guava.

\$\endgroup\$
  • \$\begingroup\$ What kind of performance do you need? Is this just a general question? Did you try essentially the same thing on the whole string and see if it was faster? \$\endgroup\$ – Quentin Pradet May 24 '13 at 12:32
  • \$\begingroup\$ Doing it on a whole string is a no-no. Only the characters out of a specified character set need to be percent-encoded this way. What I am asking is whether a better way to achieve such an encoding of a single character exists. For reference, see RFC 6570, section 1.5. \$\endgroup\$ – fge May 24 '13 at 21:09
  • \$\begingroup\$ Of course, on a whole string it would skip most characters - I don't know how easy it would be with getBytes. (You didn't answer my first two questions.) \$\endgroup\$ – Quentin Pradet May 26 '13 at 11:33
  • \$\begingroup\$ It is not a question of performance; the problem is that I think the code is a little clumsy as it stands. In this sense it is more of a general question. The main encoding function uses a CharMatcher to determine whether the current character in a string should be percent encoded or not. \$\endgroup\$ – fge May 26 '13 at 11:38
1
\$\begingroup\$

Documentation here

public static void main(final String[] args) {
        try {
            System.out.println(URLEncoder.encode("a?>éàùA-Z",
                    System.getProperty("sun.jnu.encoding")));
        } catch (final UnsupportedEncodingException e) {
            System.out.println("BIG PB");
            return; 
        }
    }

Output :

a%3F%3E%E9%E0%F9A-Z

You have the same for URLEDecoder

EDIT

For URI
Found in src of iMeMex Dataspace ManagementSystem

public static URI encode(String uriStr) throws URISyntaxException {
    if (uriStr != null) {
        // poor man's encoding
        uriStr = uriStr.replaceAll(" ", "%20");
    }
    return new URI(uriStr);
}

JavaDoc here

But URI from Java does not change other %VALUES.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ This does not fit the bill. For instance, it replaces spaces with '+', and the encoded character list differs from what URI Template requires. \$\endgroup\$ – fge May 24 '13 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.