Summary
Specify that the java.nio.MappedByteBuffer
methods force()
and force(int,int)
and the jdk.incubator.foreign.MappedMemorySegments
method force()
throw an UncheckedIOException
.
Problem
The methods force()
and force(int,int)
of java.nio.MappedByteBuffer
and force()
of jdk.incubator.foreign.MappedMemorySegments
can throw an IOException
from the native layer but this is unspecified behavior.
Solution
Catch the IOException
and rethrow it as an UncheckedIOException
.
Specification
--- a/src/java.base/share/classes/java/nio/MappedByteBuffer.java
+++ b/src/java.base/share/classes/java/nio/MappedByteBuffer.java
@@ -219,10 +220,14 @@ public abstract class MappedByteBuffer
* invoking this method may have no effect. In particular, the
* method has no effect for buffers mapped in read-only or private
* mapping modes. This method may or may not have an effect for
* implementation-specific mapping modes. </p>
*
+ * @throws UncheckedIOException
+ * If an I/O error occurs writing the buffer's content to the
+ * storage device containing the mapped file
+ *
* @return This buffer
*/
public final MappedByteBuffer force() {
@@ -270,10 +275,14 @@ public abstract class MappedByteBuffer
*
* @throws IndexOutOfBoundsException
* if the preconditions on the index and length do not
* hold.
*
+ * @throws UncheckedIOException
+ * If an I/O error occurs writing the buffer's content to the
+ * storage device containing the mapped file
+ *
* @return This buffer
*
* @since 13
*/
public final MappedByteBuffer force(int index, int length) {
--- a/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MappedMemorySegments.java
+++ b/src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/MappedMemorySegments.java
@@ -147,10 +148,11 @@ public final class MappedMemorySegments {
*
* @throws IllegalStateException if the given segment is not alive, or if the given segment is confined
* and this method is called from a thread other than the segment's owner thread.
* @throws UnsupportedOperationException if the given segment is not a mapped memory segment, e.g. if
* {@code segment.isMapped() == false}.
+ * @throws UncheckedIOException if there is an I/O error writing the contents of the segment to the associated storage device
*/
public static void force(MemorySegment segment) {
- csr of
-
JDK-6539707 (fc) MappedByteBuffer.force() method throws an IOException in a very simple test
-
- Resolved
-