On Thu, 27 Aug 2026 17:16:47 GMT, Alan Bateman <[email protected]> wrote:

>> src/java.base/share/classes/sun/nio/cs/StreamDecoder.java line 397:
>> 
>>> 395:         synchronized (lock) {
>>> 396:             ensureOpen();
>>> 397:             if (in != null && decoderFromCharset && !readCalled) {
>> 
>> As far as I can see, the `readBytes()` method, which is where the 
>> `readCalled` gets set, gets called only when holding the monitor on `lock`. 
>> Here too in `tryReadAllAsString()` we synchronize on `lock`, so the 
>> `volatile` on `readCalled` isn't necessary.
>
> The clearest place to set readCalled is as close as possible to where read is 
> called, so read and read0 after acquiring lock.

While at it, both `in` and `decoderFromCharset` are `final` fields. I think we 
should move the check for these fields out of the synchronized block in this 
method. Something like (this untested code):


if (in == null || !decoderFromCharset) {
    return null;
}
synchronized (lock) {
 ensureOpen();
 if (!readCalled) {
     return new String(in.readAllBytes(), cs);
 }
}
return null;

That would prevent the necessity of acquiring the monitor on `lock` for some 
cases.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/32264#discussion_r3874259316

Reply via email to