Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
6539707: (fc) MappedByteBuffer.force() method throws an IOException i…
…n a very simple test

Reviewed-by: alanb
  • Loading branch information
Brian Burkhalter committed Mar 15, 2021
1 parent 189289d commit 46d78f0d249e199b335d880630f0ea235fd1e184
@@ -26,6 +26,7 @@
package java.nio;

import java.io.FileDescriptor;
import java.io.UncheckedIOException;
import java.lang.ref.Reference;
import java.util.Objects;

@@ -221,6 +222,10 @@ public final MappedByteBuffer load() {
* 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() {
@@ -272,6 +277,10 @@ public final MappedByteBuffer force() {
* 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
@@ -28,6 +28,8 @@
import jdk.internal.misc.Unsafe;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.UncheckedIOException;

/* package */ class MappedMemoryUtils {

@@ -94,7 +96,11 @@ static void force(FileDescriptor fd, long address, boolean isSync, long index, l
} else {
// force writeback via file descriptor
long offset = mappingOffset(address, index);
force0(fd, mappingAddress(address, offset, index), mappingLength(offset, length));
try {
force0(fd, mappingAddress(address, offset, index), mappingLength(offset, length));
} catch (IOException cause) {
throw new UncheckedIOException(cause);
}
}
}

@@ -103,7 +109,7 @@ static void force(FileDescriptor fd, long address, boolean isSync, long index, l
private static native boolean isLoaded0(long address, long length, int pageCount);
private static native void load0(long address, long length);
private static native void unload0(long address, long length);
private static native void force0(FileDescriptor fd, long address, long length);
private static native void force0(FileDescriptor fd, long address, long length) throws IOException;

// utility methods

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -76,7 +76,7 @@ Java_java_nio_MappedMemoryUtils_force0(JNIEnv *env, jobject obj, jobject fdo,
if ((result != 0) || (GetLastError() != ERROR_LOCK_VIOLATION))
break;
retry++;
} while (retry < 3);
} while (retry < 5);

/**
* FlushViewOfFile only initiates the writing of dirty pages to disk
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,6 +27,7 @@

import jdk.internal.foreign.MappedMemorySegmentImpl;

import java.io.UncheckedIOException;
import java.nio.MappedByteBuffer;
import java.util.Objects;

@@ -149,6 +150,7 @@ public static void unload(MemorySegment segment) {
* 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) {
toMappedSegment(segment).force();
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/* @test
* @bug 6539707
* @summary Test behavior of force() with respect to throwing exceptions
* @run main ForceException
*/

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UncheckedIOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class ForceException {
public static void main(String[] args) throws IOException {
int blockSize = 2048 * 1024;
int numberOfBlocks = 200;
int fileLength = numberOfBlocks * blockSize;

File file = new File(System.getProperty("test.src", "."), "test.dat");
file.deleteOnExit();
try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
raf.setLength(fileLength);

int pos = (numberOfBlocks - 1) * blockSize;
int size = (int)Math.min(blockSize, fileLength - pos);
MappedByteBuffer mbb =
raf.getChannel().map(FileChannel.MapMode.READ_WRITE, pos, size);

System.out.printf("Write region 0x%s..0x%s%n",
Long.toHexString(pos), Long.toHexString(size));
for (int k = 0; k < mbb.limit(); k++) {
mbb.put(k, (byte)65);
}

// Catch and process UncheckedIOException; other Throwables fail
try {
System.out.println("Force");
mbb.force();
} catch (UncheckedIOException legal) {
System.out.printf("Caught legal exception %s%n", legal);
IOException cause = legal.getCause(); // can't be null
// Throw the cause if flush failed (should be only on Windows)
if (cause.getMessage().startsWith("Flush failed")) {
throw cause;
}
}

System.out.println("OK");
}
}
}

0 comments on commit 46d78f0

Please sign in to comment.