[ 
https://issues.apache.org/jira/browse/CXF-6189?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14271583#comment-14271583
 ] 

Lucas Pouzac commented on CXF-6189:
-----------------------------------

The best solution for decodeUrl seems to be 
urlDecodeAsApacheCodecWithByteBufferAndCharBuffer as below.

It's a same solution that Apache Commons Codec 1.10 except I replace 
ByteArrayOuptutStream by ByteBuffer and CharBuffer.  

{code:java}
/**
 * Radix used in encoding and decoding.
 */
static final int RADIX = 16;

protected static final byte ESCAPE_CHAR = '%';

public static String urlDecode(String value) {
    return urlDecode(value, "UTF-8");
}

public static String urlDecode(String value, String enc) {
    if (value == null) {
        return null;
    }
    return 
Charset.forName(enc).decode(urlDecode(CharBuffer.wrap(value))).toString();
}

public static final ByteBuffer urlDecode(CharBuffer in) {

    ByteBuffer buffer = ByteBuffer.allocate(in.capacity());
    while (in.hasRemaining()) {
        final int b = in.get();
        if (b == '+') {
            buffer.put((byte) ' ');
        } else if (b == ESCAPE_CHAR) {
            try {
                final int u = digit16((byte) in.get());
                final int l = digit16((byte) in.get());
                buffer.put((byte) ((u << 4) + l));
            } catch (final ArrayIndexOutOfBoundsException e) {
                throw new RuntimeException("Invalid URL encoding: ", e);
            }
        } else {
            buffer.put((byte) b);
        }
    }
    buffer.flip();
    return buffer;
}

public static int digit16(final byte b) {
    final int i = Character.digit((char) b, RADIX);
    if (i == -1) {
        throw new RuntimeException("Invalid URL encoding: not a valid digit 
(radix " + RADIX + "): " + b);
    }
    return i;
}

{code} 

It would still avoid the call if the characters '%' and '+' are not present.

Regards

> Improve memory usage of UrlUtils
> --------------------------------
>
>                 Key: CXF-6189
>                 URL: https://issues.apache.org/jira/browse/CXF-6189
>             Project: CXF
>          Issue Type: Bug
>          Components: Core
>    Affects Versions: 3.0.3
>            Reporter: Lucas Pouzac
>              Labels: performance
>         Attachments: jmh.tar.gz, screenshot-1.png
>
>
> When I run load test, I find that the management of encoding parameters of 
> the urls is consumer memory.
> I do not know if it is possible to optimize this part.
> Throughput of load test : 400 query/s
> ~80% GET query with 6 parameters
> ~20% POST query with 6 parameters GET and 1 payload



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to