1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.util.internal;
17
18 import java.lang.reflect.Method;
19 import java.nio.ByteBuffer;
20
21
22
23
24 public final class ByteBufferUtil {
25 private static final boolean CLEAN_SUPPORTED;
26 private static final Method directBufferCleaner;
27 private static final Method directBufferCleanerClean;
28
29 static {
30 Method directBufferCleanerX = null;
31 Method directBufferCleanerCleanX = null;
32 boolean v;
33 try {
34 directBufferCleanerX = Class.forName("java.nio.DirectByteBuffer").getMethod("cleaner");
35 directBufferCleanerX.setAccessible(true);
36 directBufferCleanerCleanX = Class.forName("sun.misc.Cleaner").getMethod("clean");
37 directBufferCleanerCleanX.setAccessible(true);
38 v = true;
39 } catch (Exception e) {
40 v = false;
41 }
42 CLEAN_SUPPORTED = v;
43 directBufferCleaner = directBufferCleanerX;
44 directBufferCleanerClean = directBufferCleanerCleanX;
45 }
46
47
48
49
50 public static void destroy(ByteBuffer buffer) {
51 if (CLEAN_SUPPORTED && buffer.isDirect()) {
52 try {
53 Object cleaner = directBufferCleaner.invoke(buffer);
54 directBufferCleanerClean.invoke(cleaner);
55 } catch (Exception e) {
56
57 }
58 }
59 }
60
61 private ByteBufferUtil() {
62
63 }
64 }