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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use crate::sys::{jboolean, jbyte, jchar, jdouble, jfloat, jint, jlong, jshort};
pub(crate) mod type_array_sealed {
use jni_sys::jsize;
use crate::sys::{jarray, jboolean, jbyte, jchar, jdouble, jfloat, jint, jlong, jshort};
use crate::{env::Env, errors::*};
use std::ptr::NonNull;
/// Trait to define type array access/release
///
/// # Safety
///
/// The methods of this trait must uphold the invariants described in [`Env::unsafe_clone`] when
/// using the provided [`Env`].
///
/// The `get` method must return a valid pointer to the beginning of the JNI array.
///
/// The `release` method must not invalidate the `ptr` if the `mode` is [`sys::JNI_COMMIT`].
pub unsafe trait TypeArraySealed: Copy + Send + Sync {
/// Creates a new array of this type with the given length.
unsafe fn new_array(env: &mut Env, length: jsize) -> Result<jarray>;
/// getter
///
/// # Safety
///
/// `array` must be a valid pointer to an `Array` object, or `null`
///
/// The caller is responsible for passing the returned pointer to [`release`], along
/// with the same `env` and `array` reference (which needs to still be valid)
unsafe fn get_elements(
env: &Env,
array: jarray,
is_copy: &mut jboolean,
) -> Result<*mut Self>;
/// releaser
///
/// # Safety
///
/// `array` must be a valid pointer to an `Array` object, or `null`
///
/// `ptr` must have been previously returned by the `get` function.
///
/// If `mode` is not [`sys::JNI_COMMIT`], `ptr` must not be used again after calling this
/// function.
unsafe fn release_elements(
env: &Env,
array: jarray,
ptr: NonNull<Self>,
mode: i32,
) -> Result<()>;
/// Copy elements of the array from the `start` index to the `buf` slice. The number of
/// copied elements is equal to the `buf` length.
///
/// # Errors
///
/// If `start` is negative _or_ `start + buf.len()` is greater than [`JPrimitiveArray::len`]
/// then no elements are copied, an `ArrayIndexOutOfBoundsException` is thrown, and `Err` is
/// returned.
///
/// # Safety
///
/// `array` must be a valid pointer to a primitive JNI array object, matching the type of
/// `Self`, or `null`.
unsafe fn get_region(
env: &Env,
array: jarray,
start: jsize,
buf: &mut [Self],
) -> Result<()>;
/// Copy the contents of the `buf` slice to the java byte array at the
/// `start` index.
///
/// # Safety
///
/// `array` must be a valid pointer to a primitive JNI array object, matching the type of
/// `Self`, or `null`.
unsafe fn set_region(env: &Env, array: jarray, start: jsize, buf: &[Self]) -> Result<()>;
}
// TypeArray builder
macro_rules! type_array {
( $jni_type:ty, $jni_type_name:ident, $new_array:ident, $get_elements:ident, $release_elements:ident, $get_region:ident, $set_region:ident) => {
/// $jni_type array access/release impl
unsafe impl TypeArraySealed for $jni_type {
/// Create new Java $jni_type array
unsafe fn new_array(env: &mut Env, length: jsize) -> Result<jarray> {
let raw_array = unsafe {
jni_call_with_catch_and_null_check!(
catch |env| {
crate::exceptions::JOutOfMemoryError =>
Err(Error::JniCall(JniError::NoMemory)),
else => Err(Error::JniCall(JniError::Unknown)),
},
env, v1_1, $new_array, length)?
};
Ok(raw_array)
}
/// Get Java $jni_type array
unsafe fn get_elements(
env: &Env,
array: jarray,
is_copy: &mut jboolean,
) -> Result<*mut Self> {
// There are no documented exceptions for Get<Primitive>ArrayElements() but
// they may return `NULL`.
let ptr = unsafe {
jni_call_with_catch_and_null_check!(
catch |env| {
crate::exceptions::JOutOfMemoryError =>
Err(Error::JniCall(JniError::NoMemory)),
else => Err(Error::JniCall(JniError::Unknown)),
},
env, v1_1, $get_elements, array, is_copy)?
};
Ok(ptr as _)
}
/// Release Java $jni_type array
unsafe fn release_elements(
env: &Env,
array: jarray,
ptr: NonNull<Self>,
mode: i32,
) -> Result<()> {
// Release<Primitive>ArrayElements() is safe to call while there is a
// pending exception and there are no documented exceptions for
// Release<Primitive>ArrayElements()
unsafe {
ex_safe_jni_call_no_post_check_ex!(
env,
v1_1,
$release_elements,
array,
ptr.as_ptr(),
mode as i32
)
};
Ok(())
}
unsafe fn get_region(
env: &Env,
array: jarray,
start: jsize,
buf: &mut [Self],
) -> Result<()> {
let array = null_check!(array, "get_*_array_region array argument")?;
unsafe {
Ok(jni_call_with_catch!(
catch |env| {
crate::exceptions::JArrayIndexOutOfBoundsException =>
Err(Error::IndexOutOfBounds),
else => Err(Error::JniCall(JniError::Unknown)),
},
env,
v1_1,
$get_region,
array,
start,
buf.len() as jsize,
buf.as_mut_ptr()
)?)
}
}
unsafe fn set_region(
env: &Env,
array: jarray,
start: jsize,
buf: &[Self],
) -> Result<()> {
let array = null_check!(array, "set_*_array_region array argument")?;
unsafe {
Ok(jni_call_with_catch!(
catch |env| {
crate::exceptions::JArrayIndexOutOfBoundsException =>
Err(Error::IndexOutOfBounds),
else => Err(Error::JniCall(JniError::Unknown)),
},
env,
v1_1,
$set_region,
array,
start,
buf.len() as jsize,
buf.as_ptr()
)?)
}
}
}
};
}
type_array!(
jint,
Int,
NewIntArray,
GetIntArrayElements,
ReleaseIntArrayElements,
GetIntArrayRegion,
SetIntArrayRegion
);
type_array!(
jlong,
Long,
NewLongArray,
GetLongArrayElements,
ReleaseLongArrayElements,
GetLongArrayRegion,
SetLongArrayRegion
);
type_array!(
jbyte,
Byte,
NewByteArray,
GetByteArrayElements,
ReleaseByteArrayElements,
GetByteArrayRegion,
SetByteArrayRegion
);
type_array!(
jboolean,
Boolean,
NewBooleanArray,
GetBooleanArrayElements,
ReleaseBooleanArrayElements,
GetBooleanArrayRegion,
SetBooleanArrayRegion
);
type_array!(
jchar,
Char,
NewCharArray,
GetCharArrayElements,
ReleaseCharArrayElements,
GetCharArrayRegion,
SetCharArrayRegion
);
type_array!(
jshort,
Short,
NewShortArray,
GetShortArrayElements,
ReleaseShortArrayElements,
GetShortArrayRegion,
SetShortArrayRegion
);
type_array!(
jfloat,
Float,
NewFloatArray,
GetFloatArrayElements,
ReleaseFloatArrayElements,
GetFloatArrayRegion,
SetFloatArrayRegion
);
type_array!(
jdouble,
Double,
NewDoubleArray,
GetDoubleArrayElements,
ReleaseDoubleArrayElements,
GetDoubleArrayRegion,
SetDoubleArrayRegion
);
}
/// A sealed trait to define type array access/release for primitive JNI types
pub trait TypeArray: type_array_sealed::TypeArraySealed {}
impl TypeArray for jint {}
impl TypeArray for jlong {}
impl TypeArray for jbyte {}
impl TypeArray for jboolean {}
impl TypeArray for jchar {}
impl TypeArray for jshort {}
impl TypeArray for jfloat {}
impl TypeArray for jdouble {}