/
JByteBuffer.h
100 lines (82 loc) · 3.52 KB
/
JByteBuffer.h
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
#pragma once
#include <bdn/java/Env.h>
#include <bdn/java/JObject.h>
namespace bdn
{
namespace java
{
/** Wrapper for Java java.nio.ByteBuffer objects.
*
* Creating a new object of this type is mainly useful for these use
cases:
*
1) to have Java directly manipulate the referenced memory.
2) to simply wrap a C++ pointer as a Java object, with the intention
of storing it in a Java object and then retrieving it later.
If the memory is not interpreted by Java code then you can pass
a dummy value (e.g. 1) for the buffer size.
*
* */
class JByteBuffer : public JObject
{
private:
Reference newInstance_(void *buffer, int capacityBytes)
{
Env &env = Env::get();
jobject obj = env.getJniEnv()->NewDirectByteBuffer(buffer, capacityBytes);
env.throwAndClearExceptionFromLastJavaCall();
return Reference::convertAndDestroyOwnedLocal(obj);
}
public:
/** @param objectRef the reference to the Java object.
* The JObject instance will copy this reference and keep its
* type. So if you want the JObject instance to hold a strong
* reference then you need to call toStrong() on the reference first
* and pass the result.
* */
explicit JByteBuffer(const Reference &ref) : JObject(ref) {}
/** Creates a new byte buffer on the java side, which references the
* specified memory block.
*
* In some cases the intention of creating the Java buffer is
* simply to store a memory address on the Java side. If only the
* memory address is relevant to your use case and the memory is not
* accessed on the Java side then then you can set the capacity to a
* dummy value >0 (e.g. 1).
*
* */
JByteBuffer(void *buffer, int64_t capacityBytes) : JObject(newInstance_(buffer, capacityBytes)) {}
/** Return a pointer to the buffer data.*/
void *getBuffer_()
{
Env &env = Env::get();
void *buffer = env.getJniEnv()->GetDirectBufferAddress(getJObject_());
env.throwAndClearExceptionFromLastJavaCall();
return buffer;
}
/** Returns the size of the buffer in bytes.*/
int64_t getCapacityBytes_()
{
Env &env = Env::get();
int64_t bytes = env.getJniEnv()->GetDirectBufferCapacity(getJObject_());
env.throwAndClearExceptionFromLastJavaCall();
return bytes;
}
/** Returns the java class object for the Java object's class.
*
* Note that the returned class object is not necessarily unique
* for the whole process. You might get different objects if this
* function is called from different shared libraries.
*
* If you want to check for type equality then you should compare
* the type name (see getTypeName() )
* */
static JClass &getStaticClass_()
{
static JClass cls("java/nio/ByteBuffer");
return cls;
}
JClass &getClass_() override { return getStaticClass_(); }
};
}
}