On Wed, 4 Jan 2023 16:35:57 GMT, Alan Bateman <al...@openjdk.org> wrote:

>> src/java.base/share/classes/java/io/StringWriter.java line 244:
>> 
>>> 242: 
>>> 243:     private static int checkSize(int initialSize) {
>>> 244:         if (initialSize < 0) {
>> 
>> Similar checks exist e.g. in `ByteArrayOutputStream` and `CharWrite`, so 
>> could we reuse it somehow across java.io?
>
> I assume this is done so that "lock" is only set once during construction 
> (StringWriter is a bit unusual in that it uses the SB as the lock object). A 
> downside of the change is that it introduces casts. Another is that the 
> expression to super is a bit complicated and forces the reader to locate 
> checkSize at the end of the file to find the check/exception specified by the 
> constructor. So two minds on this part, maybe it should be left unchanged.

I think the casts are worth it to set `lock` only once during construction, but 
would be inclined to leave out the addition of `checkSize`.

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

PR: https://git.openjdk.org/jdk/pull/11848

Reply via email to