NativeIO { public static boolean isAvailable() { return NativeCodeLoader.isNativeCodeLoaded() && nativeLoaded; }  static boolean isAvailable(); static String getOwner(FileDescriptor fd); static FileInputStream getShareDeleteFileInputStream(File f); static FileInputStream getShareDeleteFileInputStream(File f, long seekOffset); static FileOutputStream getCreateForWriteFileOutputStream(File f, int permissions); static void renameTo(File src, File dst); static void link(File src, File dst); static void copyFileUnbuffered(File src, File dst);  }
@Test(timeout=10000) public void testMlock() throws Exception { assumeTrue(NativeIO.isAvailable()); final File TEST_FILE = new File(new File( System.getProperty("test.build.data","build/test/data")), "testMlockFile"); final int BUF_LEN = 12289; byte buf[] = new byte[BUF_LEN]; int bufSum = 0; for (int i = 0; i < buf.length; i++) { buf[i] = (byte)(i % 60); bufSum += buf[i]; } FileOutputStream fos = new FileOutputStream(TEST_FILE); try { fos.write(buf); fos.getChannel().force(true); } finally { fos.close(); } FileInputStream fis = null; FileChannel channel = null; try { fis = new FileInputStream(TEST_FILE); channel = fis.getChannel(); long fileSize = channel.size(); MappedByteBuffer mapbuf = channel.map(MapMode.READ_ONLY, 0, fileSize); NativeIO.POSIX.mlock(mapbuf, fileSize); int sum = 0; for (int i=0; i<fileSize; i++) { sum += mapbuf.get(i); } assertEquals("Expected sums to be equal", bufSum, sum); NativeIO.POSIX.munmap(mapbuf); } finally { if (channel != null) { channel.close(); } if (fis != null) { fis.close(); } } }