RFR: 8311216: DataURI can lose information in some charset environments [v2]

John Hendrikx jhendrikx at openjdk.org
Sat Oct 28 18:13:43 UTC 2023


On Sat, 28 Oct 2023 18:07:46 GMT, Michael Strauß <mstrauss at openjdk.org> wrote:

>> modules/javafx.graphics/src/main/java/com/sun/javafx/util/DataURI.java line 210:
>> 
>>> 208: 
>>> 209:     private static byte[] decodePercentEncoding(String input) {
>>> 210:         try (var output = new ByteArrayOutputStream(input.length())) {
>> 
>> If deemed important to optimize this a bit:
>> 
>> This could allocate an array that's a factor 3 too large, which depending on how these URI's are used can become quite big (although for large amounts of data, base64 is recommended).
>> 
>> I noticed that in the URLDecoder they limit the size of the buffer `new StringBuilder(numChars > 500 ? numChars / 2 : numChars);` -- perhaps you could do something similar (`new ByteArrayOutputStream(input.length() > 500 ? input.length() / 2 : input.length())`)
>> 
>> Another alternative would be to calculate the exact size first (by counting `%`s) avoiding the need to reallocate the `byte[]` -- performance is (IMHO) likely to be better:
>> 
>> - ByteArrayOutputStream method
>>   - allocate output array: zeroes n*3 bytes
>>   - decode: reads whole string once (n to n*3 reads)
>>   - decode: writes n bytes
>>   - reallocate: reads whole output once (n)
>>   - reallocate: writes n bytes
>>   - garbage created: n*3 bytes
>>   - max memory use: n*3 + n bytes
>> 
>> Total read/writes: 4n + 5n
>> 
>> - Count method
>>   - count: reads whole string once (n to n*3 reads)
>>   - allocate output array: zeroes n bytes
>>   - decode: reads whole string again (n to n*3 reads)
>>   - decode: writes n bytes
>>   - garbage created: none
>>   - max memory use: n bytes
>> 
>> Total read/writes: 6n + 2n
>
> Solved by calculating the exact size first.

Thanks, I see you went this route, you could eliminate the `ByteArrayOutputStream` now, perhaps have the signature of `decodePercentEncodingToStream` changed to:

    private static byte[] decodePercentEncodingToStream(String input, int size)

-------------

PR Review Comment: https://git.openjdk.org/jfx/pull/1165#discussion_r1375302003


More information about the openjfx-dev mailing list