RFR: 4926314: Optimize Reader.read(CharBuffer) [v4]

Philippe Marschall github.com+471021+marschall at openjdk.java.net
Tue Jan 19 07:25:51 UTC 2021


On Mon, 18 Jan 2021 07:47:30 GMT, Peter Levart <plevart at openjdk.org> wrote:

>> Philippe Marschall has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Add unit tests
>>   
>>   - add unit test for Reader#read(CharBuffer)
>>   - add unit test for InputStreamReader#reader(CharBuffer)
>>   - test with both on-heap and off-heap buffers
>
> src/java.base/share/classes/java/io/Reader.java line 207:
> 
>> 205:                         target.put(cbuf, 0, n);
>> 206:                         nread += n;
>> 207:                         remaining -= n;
> 
> Wouldn't there be a possibility for target.put(cbuf, 0, n) to throw BufferOverflowException ?
> For example:
> - there's room (remaining) for TRANSFER_BUFFER_SIZE + 1 characters in target
> - cbuff is sized to TRANSFER_BUFFER_SIZE
> - 1st iteration of do loop transfers TRANSFER_BUFFER_SIZE charasters (remaining == 1)
> - 2nd iteration reads > 1 (up to TRANSFER_BUFFER_SIZE) characters
> - target.put throws BufferOverflowException
> 
> You have to limit the amount read in each iteration to be Math.min(remaining, cbuf.length)

You're correct. I need to expand the unit tests.

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

PR: https://git.openjdk.java.net/jdk/pull/1915


More information about the core-libs-dev mailing list