Summary
Change the method java.nio.channels.ReadableByteBuffer.read(ByteBuffer)
that is returned from java.nio.channels.Channels.newChannel(InputStream)
to throw IllegalArgumentException
if the ByteBuffer
parameter is read-only.
Problem
The read(ByteBuffer)
method of ReadableByteChannel
is specified to throw an IllegalArgumentException
if the buffer parameter is read-only. Despite this, the ReadableByteChannel
returned by Channels.newChannel(InputStream)
when the stream parameter is not a FileInputStream throws a ReadOnlyBufferException
in this case.
The described behavior is contrary to the specification. All other implementations of ReadableByteChannel
examined in the JDK throw the specified IllegalArgumentException
. Also, the ReadableByteChannel
returned by Channels.newChannel(InputStream)
has different behavior in this respect depending on whether the stream parameter is a FileInputStream
. If it is, then its ReadableByteChannel
read
method throws the specified IllegalArgumentException
if the buffer parameter is read-only, but ReadOnlyBufferException
otherwise.
Solution
Change java.nio.channels.Channels$ReadableByteChannelImpl.read(ByteBuffer)
to throw an IllegalArgumentException
instead of a ReadOnlyBufferException
when the buffer is read-only.
Specification
No specification change, this is just aligning the implementation with the specification.
--- a/src/java.base/share/classes/java/nio/channels/Channels.java
+++ b/src/java.base/share/classes/java/nio/channels/Channels.java
@@ -293,10 +293,13 @@ public final class Channels {
@Override
public int read(ByteBuffer dst) throws IOException {
if (!isOpen()) {
throw new ClosedChannelException();
}
+ if (dst.isReadOnly()) {
+ throw new IllegalArgumentException();
+
- csr of
-
JDK-8275149 (ch) ReadableByteChannel returned by Channels.newChannel(InputStream) throws ReadOnlyBufferException
-
- Closed
-