drcrallen commented on a change in pull request #7466: Java 9 compatible 
ByteBuffer unmap operation
URL: https://github.com/apache/incubator-druid/pull/7466#discussion_r275119388
 
 

 ##########
 File path: 
core/src/main/java/org/apache/druid/java/util/common/ByteBufferUtils.java
 ##########
 @@ -19,16 +19,124 @@
 
 package org.apache.druid.java.util.common;
 
-import sun.misc.Cleaner;
-import sun.nio.ch.DirectBuffer;
+import org.apache.druid.utils.JvmUtils;
 
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
 
 /**
  */
 public class ByteBufferUtils
 {
+  // the following MethodHandle lookup code is adapted from Apache Kafka
+  // 
https://github.com/apache/kafka/blob/e554dc518eaaa0747899e708160275f95c4e525f/clients/src/main/java/org/apache/kafka/common/utils/MappedByteBuffers.java
+
+  // null if unmap is not supported
+  private static final MethodHandle UNMAP;
+
+  // null if unmap is supported
+  private static final RuntimeException UNMAP_NOT_SUPPORTED_EXCEPTION;
+
+  static {
+    Object unmap = null;
+    RuntimeException exception = null;
+    try {
+      unmap = lookupUnmapMethodHandle();
+    }
+    catch (RuntimeException e) {
+      exception = e;
+    }
+    if (unmap != null) {
+      UNMAP = (MethodHandle) unmap;
+      UNMAP_NOT_SUPPORTED_EXCEPTION = null;
+    } else {
+      UNMAP = null;
+      UNMAP_NOT_SUPPORTED_EXCEPTION = exception;
+    }
+  }
+
+  private static void clean(ByteBuffer buffer)
+  {
+    if (!buffer.isDirect()) {
+      throw new IllegalArgumentException("Unmapping only works with direct 
buffers");
+    }
+    if (UNMAP == null) {
+      throw UNMAP_NOT_SUPPORTED_EXCEPTION;
 
 Review comment:
   Can this be a wrapped exception so the stack trace to here can be recorded?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to