RFR: 8304745: Lazily initialize byte[] in java.io.BufferedInputStream

Sergey Tsypanov stsypanov at openjdk.org
Thu Mar 23 19:17:39 UTC 2023


On Thu, 23 Mar 2023 14:51:40 GMT, Sergey Tsypanov <stsypanov at openjdk.org> wrote:

>> src/java.base/share/classes/java/io/BufferedInputStream.java line 211:
>> 
>>> 209:             throw new IllegalArgumentException("Buffer size <= 0");
>>> 210:         }
>>> 211:         buf = new byte[0];
>> 
>> Two recommendations:
>> 1. This `new byte[0]` can be safely shared, so I recommend you to put it in a static final field to avoid reallocation
>> 2. The subclass compatibility can be retained by:
>> Suggestion:
>> 
>>         buf = (getClass() == BufferedInputStream.class) ? new byte[0] : new byte[size];
>
> Good point, done!

Btw, maybe it'd be better to rename `getBufIfOpen()` into e.g. `getBuffer()` to make it less confusing?

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13150#discussion_r1146344036


More information about the core-libs-dev mailing list