forked from CinemaMod/java-cef
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPointer.java
More file actions
85 lines (70 loc) · 2.88 KB
/
DataPointer.java
File metadata and controls
85 lines (70 loc) · 2.88 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
package org.cef.misc;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.ByteBuffer;
public class DataPointer {
private final long address;
private ByteBuffer dataBuffer;
boolean initialized = false;
int alignment = 3;
public DataPointer(long address) {
this.address = address;
}
public DataPointer forCapacity(int capacity) {
try {
dataBuffer = (ByteBuffer) memByteBuffer.invoke(address, capacity);
initialized = true;
return this;
} catch (Throwable err) {
throw new RuntimeException("Failed to invoke memByteBuffer?", err);
}
}
public DataPointer withAlignment(int alignment) {
this.alignment = alignment;
return this;
}
public long getAddress() {
return address;
}
public DataPointer getData(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return new DataPointer(dataBuffer.getLong(offset << alignment));
}
public long getLong(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.getLong(offset << alignment);
}
public int getInt(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.getInt(offset << alignment);
}
public short getShort(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.getShort(offset << alignment);
}
public byte getByte(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.get(offset << alignment);
}
public double getDouble(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.getDouble(offset << alignment);
}
public float getFloat(int offset) {
if (!initialized) throw new RuntimeException("DataPoint#forCapacity must be called before the data can be accessed.");
return dataBuffer.getFloat(offset << alignment);
}
// TODO: ideally we'd just directly depend on lwjgl, since we require it for GLFW anyway
private static final MethodHandle memByteBuffer;
static {
MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
Class<?> clz = Class.forName("org.lwjgl.system.MemoryUtil", false, lookup.lookupClass().getClassLoader());
memByteBuffer = lookup.findStatic(clz, "memByteBuffer", MethodType.methodType(ByteBuffer.class, new Class[]{Long.TYPE, Integer.TYPE}));
} catch (Throwable err) {
System.err.println("Could not find LWJGL MemoryUtil's memByteBuffer method.\nAre you using LWJGL 3.x?");
throw new RuntimeException(err);
}
}
}