public static String urlEncode(final String text) throws Exception {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();

        for (int b : toUTF8Bytes(text)) {
            if (b < 0) {
                b = 256 + b;
            }

            if (UNRESERVED_CHARS.get(b)) {
                buffer.write(b);
            } else {
                buffer.write(URL_ESCAPE_CHAR);
                char hex1 = Character.toUpperCase(Character.forDigit(
                        (b >> 4) & 0xF, 16));
                char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF,
                        16));
                buffer.write(hex1);
                buffer.write(hex2);
            }
        }

        return new String(buffer.toByteArray());
    }