RFR: 8316681: Rewrite URLEncoder.encode to use small reusable buffers [v2]
Claes Redestad
redestad at openjdk.org
Thu Sep 21 18:51:58 UTC 2023
On Thu, 21 Sep 2023 16:16:42 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:
>> Claes Redestad has updated the pull request incrementally with one additional commit since the last revision:
>>
>> Minor refactoring, drop unnecessary double cast
>
> src/java.base/share/classes/java/net/URLEncoder.java line 245:
>
>> 243: CharsetEncoder ce = charset.newEncoder();
>> 244: CharBuffer cb = CharBuffer.allocate(ENCODING_CHUNK_SIZE);
>> 245: ByteBuffer bb = ByteBuffer.allocate((int)(ENCODING_CHUNK_SIZE * (double)ce.maxBytesPerChar()));
>
> The cast to (double) is curious
It's copied from `String::scale`:
private static int scale(int len, float expansionFactor) {
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when len is larger than 2**24.
return (int)(len * (double)expansionFactor);
}
... though admittedly this caveat doesn't apply since `len` in our case is a small constant value.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/15865#discussion_r1333461724
More information about the net-dev
mailing list