|
23 | 23 |
|
24 | 24 | import java.lang.reflect.Constructor;
|
25 | 25 | import java.lang.reflect.Field;
|
| 26 | +import java.lang.reflect.Method; |
26 | 27 | import java.nio.ByteBuffer;
|
27 | 28 | import java.util.Arrays;
|
28 | 29 | import java.util.Comparator;
|
29 | 30 | import java.util.List;
|
30 | 31 |
|
31 |
| -import sun.misc.Cleaner; |
32 | 32 | import sun.misc.Unsafe;
|
33 | 33 | import sun.nio.ch.DirectBuffer;
|
34 | 34 |
|
@@ -92,6 +92,18 @@ public class UnsafeUtil {
|
92 | 92 | _unsafe = tmpUnsafe;
|
93 | 93 | }
|
94 | 94 |
|
| 95 | + static private Method cleanerMethod, cleanMethod; |
| 96 | + static { |
| 97 | + try { |
| 98 | + cleanerMethod = DirectBuffer.class.getMethod("cleaner"); |
| 99 | + cleanerMethod.setAccessible(true); |
| 100 | + cleanMethod = cleanerMethod.getReturnType().getMethod("clean"); |
| 101 | + } catch (Exception ex) { |
| 102 | + if (DEBUG) debug("kryo", "No direct ByteBuffer clean method is available.", ex); |
| 103 | + cleanerMethod = null; |
| 104 | + } |
| 105 | + } |
| 106 | + |
95 | 107 | static {
|
96 | 108 | ByteBuffer buf = ByteBuffer.allocateDirect(1);
|
97 | 109 | try {
|
@@ -154,11 +166,13 @@ final static public ByteBuffer getDirectBufferAt (long address, int size) {
|
154 | 166 | *
|
155 | 167 | * NOTE: If Cleaner is not accessible due to SecurityManager restrictions, reflection could be used to obtain the "clean"
|
156 | 168 | * method and then invoke it. */
|
157 |
| - static public void releaseBuffer (ByteBuffer niobuffer) { |
158 |
| - if (niobuffer != null && niobuffer.isDirect()) { |
159 |
| - Object cleaner = ((DirectBuffer)niobuffer).cleaner(); |
160 |
| - if (cleaner != null) ((Cleaner)cleaner).clean(); |
161 |
| - niobuffer = null; |
| 169 | + static public void releaseBuffer (ByteBuffer buffer) { |
| 170 | + if (!(buffer instanceof DirectBuffer)) return; |
| 171 | + if (cleanerMethod != null) { |
| 172 | + try { |
| 173 | + cleanMethod.invoke(cleanerMethod.invoke(buffer)); |
| 174 | + } catch (Throwable ignored) { |
| 175 | + } |
162 | 176 | }
|
163 | 177 | }
|
164 | 178 | }
|
0 commit comments