Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8275183

(ch) ReadableByteChannel returned by Channels.newChannel(InputStream) throws ReadOnlyBufferException

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 18
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      Callers could be checking for ReadOnlyBufferException and not IllegalArgumentException.
      Show
      Callers could be checking for ReadOnlyBufferException and not IllegalArgumentException.
    • Java API
    • Implementation

      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();
          +  

            bpb Brian Burkhalter
            bpb Brian Burkhalter
            Alan Bateman, Lance Andersen
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: