RFR: 8266078: Reader.read(CharBuffer) advances Reader position for read-only Charbuffers

Roger Riggs rriggs at openjdk.java.net
Tue Apr 27 19:37:29 UTC 2021

On Tue, 27 Apr 2021 18:47:41 GMT, Brian Burkhalter <bpb at openjdk.org> wrote:

> Please consider this request to modify `Reader.read(CharBuffer)` to check whether the buffer is read-only before reading any characters from the character stream. This can happen now if the buffer is read-only. Character are first read thereby advancing the stream before an attempt is made to put them in the `CharBuffer` thus incorrectly advancing the stream position.

src/java.base/share/classes/java/io/Reader.java line 202:

> 200:             if (target.isReadOnly())
> 201:                 throw new ReadOnlyBufferException();
> 202:             int len = target.remaining();

It seems like the other branch of the if should also check for read-only and throw.
Or can the target not have an array if it is readonly.
It might return -1 for a readonly target if the source was used up. The always throw.

Moving the `if (target.isReadOnly())`  to the line 188: would be unambiguous.

test/jdk/java/io/Reader/ReadIntoReadOnlyBuffer.java line 52:

> 50:         try {
> 51:             r.read(b);
> 52:             throw new RuntimeException();

A helpful message would make it clearer what failed, when/if it fails.


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

More information about the core-libs-dev mailing list