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

MappedByteBuffer.force() throws IndexOutOfBoundsException

XMLWordPrintable

    • b25
    • 15
    • b27
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "15-ea" 2020-09-15
      OpenJDK Runtime Environment (build 15-ea+26-1287)
      OpenJDK 64-Bit Server VM (build 15-ea+26-1287, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      When a MappedByteBuffer limit is less than the capacity, calling force() throws an IndexOutOfBoundsException.

      REGRESSION : Last worked in version 14.0.1

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Map a file, write something to it, set a limit lower than the buffer capacity, and then call force().

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result is no exception.
      ACTUAL -
      An exception is thrown:

      Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 1000) out of bounds for length 4
              at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
              at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82)
              at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343)
              at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411)
              at java.base/java.nio.MappedByteBuffer.force(MappedByteBuffer.java:283)
              at java.base/java.nio.MappedByteBuffer.force(MappedByteBuffer.java:230)
              at test.MappedFileBug.main(MappedFileBug.java:15)

      ---------- BEGIN SOURCE ----------
      package test;

      import java.io.File;
      import java.nio.MappedByteBuffer;
      import java.nio.channels.FileChannel;
      import static java.nio.file.StandardOpenOption.*;

      public class MappedFileBug {
          public static void main(String[] args) throws Exception {
              var file = File.createTempFile("test", null);
              var channel = FileChannel.open(file.toPath(), READ, WRITE);
              MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1000);
              buffer.putInt(1234);
              buffer.limit(4);
              buffer.force();
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Set the limit to the capacity, call force, and then set the limit back. This might cause more data to be forced than desired.

      FREQUENCY : always


            bpb Brian Burkhalter
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: