RFR: 8371718: (sc) Channels.new{Input, Output}Stream can allocate unbounded memory for a socket channel [v3]

Brian Burkhalter bpb at openjdk.org
Tue Dec 9 16:57:12 UTC 2025


On Tue, 9 Dec 2025 16:29:06 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

>> src/java.base/share/classes/sun/nio/ch/ChannelOutputStream.java line 76:
>> 
>>> 74:             if (n <= 0)
>>> 75:                 throw new RuntimeException("no bytes written");
>>> 76:             bb.limit(limit);
>> 
>> Setting the limit twice per iteration is a bit confusing, maybe simplify this loop to:
>> 
>>         int pos = bb.position();
>>         int rem = bb.limit() - pos;
>>         while (rem > 0) {
>>             bb.limit(pos + Math.min(MAX_BUFFER_SIZE, rem));
>>             int n = channelWrite(bb);
>>             if (n <= 0)
>>                 throw new RuntimeException("no bytes written");
>>             pos += n;
>>             rem -= n;
>>         }
>> 
>> In passing, the pre-existing throwing of RuntimeException should be re-examined at some point.
>
>> Setting the limit twice per iteration is a bit confusing
> 
> I actually made this change locally but decided against it. I'll take a second look.

> In passing, the pre-existing throwing of RuntimeException should be re-examined at some point.

For this case, FileOutputStream throws an IOException with message "Write error" and IOUtil.convertReturnVal throws an IOException with message "Read/write failed".

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

PR Review Comment: https://git.openjdk.org/jdk/pull/28705#discussion_r2603499279


More information about the nio-dev mailing list