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

(bf) Views of MappedByteBuffers are not MappedByteBuffers, and cannot be forced

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 17
    • core-libs
    • None
    • behavioral
    • minimal
    • Changing these methods to return a sub-class of what is currently returned should not cause any problems.
    • Java API
    • SE

      Summary

      The compact(), duplicate(), and slice() methods of java.nio.MappedByteBuffer are changed to covariant overrides returning a MappedByteBuffer instead of a ByteBuffer.

      Problem

      The compact(), duplicate(), and slice() methods of MappedByteBuffer return a ByteBuffer thereby precluding the ability to use these methods in chain invocations except as the terminal invocation.

      Solution

      Provide covariant overrides for the MappedByteBuffer methods compact(), duplicate(), and slice().

      Specification

      --- a/src/java.base/share/classes/java/nio/MappedByteBuffer.java
      +++ b/src/java.base/share/classes/java/nio/MappedByteBuffer.java
      @@ -205,7 +215,10 @@ public abstract class MappedByteBuffer
      
           /**
            * Forces any changes made to this buffer's content to be written to the
      -     * storage device containing the mapped file.
      +     * storage device containing the mapped file.  The region starts at index
      +     * zero in this buffer and is {@code capacity()} bytes.  An invocation of
      +     * this method behaves in exactly the same way as the invocation
      +     * {@link force(int,int) force(0,capacity())}.
            *
            * <p> If the file mapped into this buffer resides on a local storage
            * device then when this method returns it is guaranteed that all changes
      
      @@ -362,4 +375,41 @@ public abstract class MappedByteBuffer
               super.rewind();
               return this;
           }
      +
      +    /**
      +     * {@inheritDoc}
      +     *
      +     * <p> Reading bytes into physical memory by invoking {@code load()} on the
      +     * returned buffer, or writing bytes to the storage device by invoking
      +     * {@code force()} on the returned buffer, will only act on the sub-range
      +     * of this buffer that the returned buffer represents, namely
      +     * {@code [position(),limit())}.
      +     */
      +    @Override
      +    public abstract MappedByteBuffer slice();
      +
      +    /**
      +     * {@inheritDoc}
      +     *
      +     * <p> Reading bytes into physical memory by invoking {@code load()} on the
      +     * returned buffer, or writing bytes to the storage device by invoking
      +     * {@code force()} on the returned buffer, will only act on the sub-range
      +     * of this buffer that the returned buffer represents, namely
      +     * {@code [index,index+length)}, where {@code index} and {@code length} are
      +     * assumed to satisfy the preconditions.
      +     */
      +    @Override
      +    public abstract MappedByteBuffer slice(int index, int length);
      +
      +    /**
      +     * {@inheritDoc}
      +     */
      +    @Override
      +    public abstract MappedByteBuffer duplicate();
      +
      +    /**
      +     * {@inheritDoc}
      +     */
      +    @Override
      +    public abstract MappedByteBuffer compact();
       }

            bpb Brian Burkhalter
            nthompsosunw Nathanael Thompson (Inactive)
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: