forked from isopov/msgpack-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectBufferAccess.java
More file actions
158 lines (143 loc) · 6.05 KB
/
Copy pathDirectBufferAccess.java
File metadata and controls
158 lines (143 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.core.buffer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
/**
* Wraps the difference of access methods to DirectBuffers between Android and others.
*/
class DirectBufferAccess
{
private DirectBufferAccess()
{}
enum DirectBufferConstructorType
{
ARGS_LONG_INT_REF,
ARGS_LONG_INT,
ARGS_INT_INT,
ARGS_MB_INT_INT
}
static Method mGetAddress;
static Method mCleaner;
static Method mClean;
// TODO We should use MethodHandle for efficiency, but it is not available in JDK6
static Constructor byteBufferConstructor;
static Class<?> directByteBufferClass;
static DirectBufferConstructorType directBufferConstructorType;
static Method memoryBlockWrapFromJni;
static {
try {
// Find the hidden constructor for DirectByteBuffer
directByteBufferClass = ClassLoader.getSystemClassLoader().loadClass("java.nio.DirectByteBuffer");
Constructor directByteBufferConstructor = null;
DirectBufferConstructorType constructorType = null;
Method mbWrap = null;
try {
// TODO We should use MethodHandle for Java7, which can avoid the cost of boxing with JIT optimization
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class, Object.class);
constructorType = DirectBufferConstructorType.ARGS_LONG_INT_REF;
}
catch (NoSuchMethodException e0) {
try {
// https://android.googlesource.com/platform/libcore/+/master/luni/src/main/java/java/nio/DirectByteBuffer.java
// DirectByteBuffer(long address, int capacity)
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(long.class, int.class);
constructorType = DirectBufferConstructorType.ARGS_LONG_INT;
}
catch (NoSuchMethodException e1) {
try {
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(int.class, int.class);
constructorType = DirectBufferConstructorType.ARGS_INT_INT;
}
catch (NoSuchMethodException e2) {
Class<?> aClass = Class.forName("java.nio.MemoryBlock");
mbWrap = aClass.getDeclaredMethod("wrapFromJni", int.class, long.class);
mbWrap.setAccessible(true);
directByteBufferConstructor = directByteBufferClass.getDeclaredConstructor(aClass, int.class, int.class);
constructorType = DirectBufferConstructorType.ARGS_MB_INT_INT;
}
}
}
byteBufferConstructor = directByteBufferConstructor;
directBufferConstructorType = constructorType;
memoryBlockWrapFromJni = mbWrap;
if (byteBufferConstructor == null) {
throw new RuntimeException("Constructor of DirectByteBuffer is not found");
}
byteBufferConstructor.setAccessible(true);
mGetAddress = directByteBufferClass.getDeclaredMethod("address");
mGetAddress.setAccessible(true);
mCleaner = directByteBufferClass.getDeclaredMethod("cleaner");
mCleaner.setAccessible(true);
mClean = mCleaner.getReturnType().getDeclaredMethod("clean");
mClean.setAccessible(true);
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
static long getAddress(Object base)
{
try {
return (Long) mGetAddress.invoke(base);
}
catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
static void clean(Object base)
{
try {
Object cleaner = mCleaner.invoke(base);
mClean.invoke(cleaner);
}
catch (Throwable e) {
throw new RuntimeException(e);
}
}
static boolean isDirectByteBufferInstance(Object s)
{
return directByteBufferClass.isInstance(s);
}
static ByteBuffer newByteBuffer(long address, int index, int length, ByteBuffer reference)
{
try {
switch (directBufferConstructorType) {
case ARGS_LONG_INT_REF:
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length, reference);
case ARGS_LONG_INT:
return (ByteBuffer) byteBufferConstructor.newInstance(address + index, length);
case ARGS_INT_INT:
return (ByteBuffer) byteBufferConstructor.newInstance((int) address + index, length);
case ARGS_MB_INT_INT:
return (ByteBuffer) byteBufferConstructor.newInstance(
memoryBlockWrapFromJni.invoke(null, address + index, length),
length, 0);
default:
throw new IllegalStateException("Unexpected value");
}
}
catch (Throwable e) {
// Convert checked exception to unchecked exception
throw new RuntimeException(e);
}
}
}