Unsafe9R { public static void invokeCleaner(ByteBuffer directBuffer) { if (!directBuffer.isDirect()) throw new IllegalArgumentException("buffer is non-direct"); if (useJava9) Java9.invokeCleaner(directBuffer); else { Java6.invokeCleaner(directBuffer); } }  static void invokeCleaner(ByteBuffer directBuffer);  }
@Test(expected = IllegalArgumentException.class) public void releaseHeapBuffer() { Unsafe9R.invokeCleaner(ByteBuffer.allocate(1)); }
@Test public void releaseDirectBuffer() { Unsafe9R.invokeCleaner(ByteBuffer.allocateDirect(1)); }
@Test public void unmapMemoryMappedFile() throws IOException { File tmpFile = new File("Unsafe9RTest.tmp"); assertTrue("pre-create", !tmpFile.exists() || tmpFile.delete()); final FileChannel channel = new RandomAccessFile(tmpFile, "rw").getChannel(); assertTrue("post-create", tmpFile.exists()); final ByteBuffer buffer = channel .map(FileChannel.MapMode.READ_WRITE, 0, 1024) .load(); if (PlatformInfo.isWindows()) { assertFalse("pre-close", tmpFile.delete()); channel.close(); assertFalse("post-close", tmpFile.delete()); } else { channel.close(); } Unsafe9R.invokeCleaner(buffer); assertTrue("post-free", tmpFile.delete()); }