On Tue, 27 Apr 2021 19:31:40 GMT, Roger Riggs <rri...@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. It can't have an array if it is read-only. Returns: true if, and only if, this buffer is backed by an array and is not read-only https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/nio/CharBuffer.html#hasArray() > 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. Agreed. ------------- PR: https://git.openjdk.java.net/jdk/pull/3725