|
23 | 23 |
|
24 | 24 | import java.lang.reflect.Constructor;
|
25 | 25 | import java.lang.reflect.Field;
|
26 |
| -import java.lang.reflect.Method; |
27 | 26 | import java.nio.ByteBuffer;
|
28 | 27 | import java.util.Arrays;
|
29 | 28 | import java.util.Comparator;
|
30 | 29 | import java.util.List;
|
31 | 30 |
|
| 31 | +import sun.misc.Cleaner; |
32 | 32 | import sun.misc.Unsafe;
|
33 | 33 | import sun.nio.ch.DirectBuffer;
|
34 | 34 |
|
@@ -92,18 +92,6 @@ 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 |
| - |
107 | 95 | static {
|
108 | 96 | ByteBuffer buf = ByteBuffer.allocateDirect(1);
|
109 | 97 | try {
|
@@ -166,13 +154,11 @@ final static public ByteBuffer getDirectBufferAt (long address, int size) {
|
166 | 154 | *
|
167 | 155 | * NOTE: If Cleaner is not accessible due to SecurityManager restrictions, reflection could be used to obtain the "clean"
|
168 | 156 | * method and then invoke it. */
|
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 |
| - } |
| 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; |
176 | 162 | }
|
177 | 163 | }
|
178 | 164 | }
|
0 commit comments