1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /*
15  *  For details of the HDF libraries, see the HDF Documentation at:
16  *    http://hdfgroup.org/HDF5/doc/
17  *
18  */
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif /* __cplusplus */
23 
24 #include <jni.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "hdf5.h"
29 #include "h5util.h"
30 #include "h5jni.h"
31 #include "h5dImp.h"
32 
33 /*
34  * Pointer to the JNI's Virtual Machine; used for callback functions.
35  */
36 extern JavaVM *jvm;
37 
38 typedef struct _cb_wrapper {
39     jobject visit_callback;
40     jobject op_data;
41 } cb_wrapper;
42 
43 /********************/
44 /* Local Prototypes */
45 /********************/
46 
47 static herr_t H5DreadVL_asstr(JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf);
48 static herr_t H5DreadVL_str(JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf);
49 static herr_t H5DwriteVL_asstr(JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf);
50 static herr_t H5DwriteVL_str(JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf);
51 
52 /********************/
53 /* Local Macros     */
54 /********************/
55 
56 
57 /*
58  * Class:     hdf_hdf5lib_H5
59  * Method:    _H5Dcreate
60  * Signature: (JLjava/lang/String;JJJ)J
61  */
62 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dcreate(JNIEnv * env,jclass clss,jlong loc_id,jstring name,jlong type_id,jlong space_id,jlong create_plist_id)63 Java_hdf_hdf5lib_H5__1H5Dcreate
64     (JNIEnv *env, jclass clss, jlong loc_id, jstring name, jlong type_id,
65           jlong space_id, jlong create_plist_id)
66 {
67     const char *dsetName = NULL;
68     hid_t       dset_id = H5I_INVALID_HID;
69 
70     UNUSED(clss);
71 
72     if (NULL == name)
73         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dcreate: dataset name is NULL");
74 
75     PIN_JAVA_STRING(ENVONLY, name, dsetName, NULL, "H5Dcreate: dataset name not pinned");
76 
77     if ((dset_id = H5Dcreate2((hid_t)loc_id, dsetName, (hid_t)type_id, (hid_t)space_id, H5P_DEFAULT, (hid_t)create_plist_id, H5P_DEFAULT)) < 0)
78         H5_LIBRARY_ERROR(ENVONLY);
79 
80 done:
81     if (dsetName)
82         UNPIN_JAVA_STRING(ENVONLY, name, dsetName);
83 
84     return (jlong)dset_id;
85 } /* end Java_hdf_hdf5lib_H5__1H5Dcreate */
86 
87 /*
88  * Class:     hdf_hdf5lib_H5
89  * Method:    _H5Dopen
90  * Signature: (JLjava/lang/String;)J
91  */
92 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dopen(JNIEnv * env,jclass clss,jlong loc_id,jstring name)93 Java_hdf_hdf5lib_H5__1H5Dopen
94     (JNIEnv *env, jclass clss, jlong loc_id, jstring name)
95 {
96     const char *dsetName = NULL;
97     hid_t       dset_id = H5I_INVALID_HID;
98 
99     UNUSED(clss);
100 
101     if (NULL == name)
102         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dopen: dataset name is NULL");
103 
104     PIN_JAVA_STRING(ENVONLY, name, dsetName, NULL, "H5Dopen: dataset name not pinned");
105 
106     if ((dset_id = H5Dopen2((hid_t)loc_id, dsetName, H5P_DEFAULT)))
107         H5_LIBRARY_ERROR(ENVONLY);
108 
109 done:
110     if (dsetName)
111         UNPIN_JAVA_STRING(ENVONLY, name, dsetName);
112 
113     return (jlong)dset_id;
114 } /* end Java_hdf_hdf5lib_H5__1H5Dopen */
115 
116 /*
117  * Class:     hdf_hdf5lib_H5
118  * Method:    _H5Dget_space
119  * Signature: (J)J
120  */
121 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dget_1space(JNIEnv * env,jclass clss,jlong dataset_id)122 Java_hdf_hdf5lib_H5__1H5Dget_1space
123     (JNIEnv *env, jclass clss, jlong dataset_id)
124 {
125     hid_t retVal = H5I_INVALID_HID;
126 
127     UNUSED(clss);
128 
129     if ((retVal = H5Dget_space((hid_t)dataset_id)) < 0)
130         H5_LIBRARY_ERROR(ENVONLY);
131 
132 done:
133     return (jlong)retVal;
134 } /* end Java_hdf_hdf5lib_H5__1H5Dget_1space */
135 
136 /*
137  * Class:     hdf_hdf5lib_H5
138  * Method:    _H5Dget_type
139  * Signature: (J)J
140  */
141 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dget_1type(JNIEnv * env,jclass clss,jlong dataset_id)142 Java_hdf_hdf5lib_H5__1H5Dget_1type
143     (JNIEnv *env, jclass clss, jlong dataset_id)
144 {
145     hid_t retVal = H5I_INVALID_HID;
146 
147     UNUSED(clss);
148 
149     if ((retVal = H5Dget_type((hid_t)dataset_id)) < 0)
150         H5_LIBRARY_ERROR(ENVONLY);
151 
152 done:
153     return (jlong)retVal;
154 } /* end Java_hdf_hdf5lib_H5__1H5Dget_1type */
155 
156 /*
157  * Class:     hdf_hdf5lib_H5
158  * Method:    _H5Dget_create_plist
159  * Signature: (J)J
160  */
161 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dget_1create_1plist(JNIEnv * env,jclass clss,jlong dataset_id)162 Java_hdf_hdf5lib_H5__1H5Dget_1create_1plist
163     (JNIEnv *env, jclass clss, jlong dataset_id)
164 {
165     hid_t retVal = H5I_INVALID_HID;
166 
167     UNUSED(clss);
168 
169     if ((retVal = H5Dget_create_plist((hid_t)dataset_id)) < 0)
170         H5_LIBRARY_ERROR(ENVONLY);
171 
172 done:
173     return (jlong)retVal;
174 } /* end Java_hdf_hdf5lib_H5__1H5Dget_1create_1plist */
175 
176 /*
177  * Class:     hdf_hdf5lib_H5
178  * Method:    H5Dread
179  * Signature: (JJJJJ[BZ)I
180  */
181 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jbyteArray buf,jboolean isCriticalPinning)182 Java_hdf_hdf5lib_H5_H5Dread
183     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
184           jlong file_space_id, jlong xfer_plist_id, jbyteArray buf, jboolean isCriticalPinning)
185 {
186     jboolean  readBufIsCopy;
187     htri_t    data_class;
188     jbyte    *readBuf = NULL;
189     herr_t    status = FAIL;
190 
191     UNUSED(clss);
192 
193     if (NULL == buf)
194         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread: read buffer is NULL");
195 
196     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
197         H5_LIBRARY_ERROR(ENVONLY);
198 
199     if (data_class)
200         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread: variable length type not supported");
201 
202     /* Recursively detect any vlen string in type (compound, array ...) */
203     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
204         H5_LIBRARY_ERROR(ENVONLY);
205 
206     if (data_class)
207         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread: variable length type not supported");
208 
209     if (isCriticalPinning) {
210         PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread: read buffer not critically pinned");
211     }
212     else {
213         PIN_BYTE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread: read buffer not pinned");
214     }
215 
216     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
217             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
218         H5_LIBRARY_ERROR(ENVONLY);
219 
220 done:
221     if (readBuf) {
222         if (isCriticalPinning) {
223             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
224         }
225         else {
226             UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
227         }
228     }
229 
230     return (jint)status;
231 } /* end Java_hdf_hdf5lib_H5_H5Dread */
232 
233 /*
234  * Class:     hdf_hdf5lib_H5
235  * Method:    H5Dwrite
236  * Signature: (JJJJJ[BZ)I
237  */
238 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jbyteArray buf,jboolean isCriticalPinning)239 Java_hdf_hdf5lib_H5_H5Dwrite
240     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
241           jlong file_space_id, jlong xfer_plist_id, jbyteArray buf, jboolean isCriticalPinning)
242 {
243     jboolean  writeBufIsCopy;
244     htri_t    data_class;
245     jbyte    *writeBuf = NULL;
246     herr_t    status = FAIL;
247 
248     UNUSED(clss);
249 
250     if (NULL == buf)
251         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite: write buffer is NULL");
252 
253     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
254         H5_LIBRARY_ERROR(ENVONLY);
255 
256     if (data_class)
257         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite:  variable length type not supported");
258 
259     /* Recursively detect any vlen string in type (compound, array ...) */
260     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
261         H5_LIBRARY_ERROR(ENVONLY);
262 
263     if (data_class)
264         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite: variable length type not supported");
265 
266     if (isCriticalPinning) {
267         PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite: write buffer not critically pinned");
268     }
269     else {
270         PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite: write buffer not pinned");
271     }
272 
273     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
274             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
275         H5_LIBRARY_ERROR(ENVONLY);
276 
277 done:
278     if (writeBuf) {
279         if (isCriticalPinning) {
280             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
281         }
282         else {
283             UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
284         }
285     }
286 
287     return (jint)status;
288 } /* end Java_hdf_hdf5lib_H5_H5Dwrite */
289 
290 /*
291  * Class:     hdf_hdf5lib_H5
292  * Method:    _H5Dclose
293  * Signature: (J)I
294  */
295 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5__1H5Dclose(JNIEnv * env,jclass clss,jlong dataset_id)296 Java_hdf_hdf5lib_H5__1H5Dclose
297     (JNIEnv *env, jclass clss, jlong dataset_id)
298 {
299     herr_t retVal = FAIL;
300 
301     UNUSED(clss);
302 
303     if ((retVal = H5Dclose((hid_t)dataset_id)) < 0)
304         H5_LIBRARY_ERROR(ENVONLY);
305 
306 done:
307     return (jint)retVal;
308 } /* end Java_hdf_hdf5lib_H5__1H5Dclose */
309 
310 /*
311  * Class:     hdf_hdf5lib_H5
312  * Method:    H5Dget_storage_size
313  * Signature: (J)J
314  */
315 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Dget_1storage_1size(JNIEnv * env,jclass clss,jlong dataset_id)316 Java_hdf_hdf5lib_H5_H5Dget_1storage_1size
317     (JNIEnv *env, jclass clss, jlong dataset_id)
318 {
319     hsize_t retVal = 0;
320 
321     UNUSED(clss);
322 
323     if (dataset_id < 0)
324         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dget_storage_size: not a dataset");
325 
326     if (!(retVal = H5Dget_storage_size((hid_t)dataset_id)))
327         H5_LIBRARY_ERROR(ENVONLY);
328 
329 done:
330     return (jlong)retVal;
331 } /* end Java_hdf_hdf5lib_H5_H5Dget_1storage_1size */
332 
333 /*
334  * Class:     hdf_hdf5lib_H5
335  * Method:    H5Dvlen_reclaim
336  * Signature: (JJJ[B)I
337  */
338 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dvlen_1reclaim(JNIEnv * env,jclass clss,jlong type_id,jlong space_id,jlong xfer_plist_id,jbyteArray buf)339 Java_hdf_hdf5lib_H5_H5Dvlen_1reclaim
340     (JNIEnv *env, jclass clss, jlong type_id, jlong space_id,
341           jlong xfer_plist_id, jbyteArray buf)
342 {
343     jboolean  vlenBufIsCopy;
344     jbyte    *vlenBuf = NULL;
345     herr_t    status = FAIL;
346 
347     UNUSED(clss);
348 
349     if (NULL == buf)
350         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dvlen_reclaim: buffer is NULL");
351 
352     PIN_BYTE_ARRAY(ENVONLY, buf, vlenBuf, &vlenBufIsCopy, "H5Dvlen_reclaim: buffer not pinned");
353 
354     if ((status = H5Dvlen_reclaim((hid_t)type_id, (hid_t)space_id, (hid_t)xfer_plist_id, vlenBuf)) < 0)
355         H5_LIBRARY_ERROR(ENVONLY);
356 
357 done:
358     if (vlenBuf)
359         UNPIN_BYTE_ARRAY(ENVONLY, buf, vlenBuf, (status < 0) ? JNI_ABORT : 0);
360 
361     return (jint)status;
362 } /* end Java_hdf_hdf5lib_H5_H5Dvlen_1reclaim */
363 
364 /*
365  * Class:     hdf_hdf5lib_H5
366  * Method:    H5Dread_short
367  * Signature: (JJJJJ[SZ)I
368  */
369 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1short(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jshortArray buf,jboolean isCriticalPinning)370 Java_hdf_hdf5lib_H5_H5Dread_1short
371     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
372           jlong file_space_id, jlong xfer_plist_id, jshortArray buf, jboolean isCriticalPinning)
373 {
374     jboolean  readBufIsCopy;
375     jshort   *readBuf = NULL;
376     htri_t    data_class;
377     herr_t    status = FAIL;
378 
379     UNUSED(clss);
380 
381     if (NULL == buf)
382         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_short: read buffer is NULL");
383 
384     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
385         H5_LIBRARY_ERROR(ENVONLY);
386 
387     if (data_class)
388         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_short: variable length type not supported");
389 
390     /* Recursively detect any vlen string in type (compound, array ...) */
391     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
392         H5_LIBRARY_ERROR(ENVONLY);
393 
394     if (data_class)
395         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_short: variable length type not supported");
396 
397     if (isCriticalPinning) {
398         PIN_SHORT_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_short: read buffer not critically pinned");
399     }
400     else {
401         PIN_SHORT_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_short: read buffer not pinned");
402     }
403 
404     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
405             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
406         H5_LIBRARY_ERROR(ENVONLY);
407 
408 done:
409     if (readBuf) {
410         if (isCriticalPinning) {
411             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
412         }
413         else {
414             UNPIN_SHORT_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
415         }
416     }
417 
418     return (jint)status;
419 } /* end Java_hdf_hdf5lib_H5_H5Dread_1short */
420 
421 /*
422  * Class:     hdf_hdf5lib_H5
423  * Method:    H5Dwrite_short
424  * Signature: (JJJJJ[SZ)I
425  */
426 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1short(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jshortArray buf,jboolean isCriticalPinning)427 Java_hdf_hdf5lib_H5_H5Dwrite_1short
428     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
429           jlong file_space_id, jlong xfer_plist_id, jshortArray buf, jboolean isCriticalPinning)
430 {
431     jboolean  writeBufIsCopy;
432     jshort   *writeBuf = NULL;
433     htri_t    data_class;
434     herr_t    status = FAIL;
435 
436     UNUSED(clss);
437 
438     if (NULL == buf)
439         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_short: write buffer is NULL");
440 
441     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
442         H5_LIBRARY_ERROR(ENVONLY);
443 
444     if (data_class)
445         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_short: variable length type not supported");
446 
447     /* Recursively detect any vlen string in type (compound, array ...) */
448     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
449         H5_LIBRARY_ERROR(ENVONLY);
450 
451     if (data_class)
452         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_short: variable length type not supported");
453 
454     if (isCriticalPinning) {
455         PIN_SHORT_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_short: write buffer not critically pinned");
456     }
457     else {
458         PIN_SHORT_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_short: write buffer not pinned");
459     }
460 
461     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
462             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
463         H5_LIBRARY_ERROR(ENVONLY);
464 
465 done:
466     if (writeBuf) {
467         if (isCriticalPinning) {
468             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
469         }
470         else {
471             UNPIN_SHORT_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
472         }
473     }
474 
475     return (jint)status;
476 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1short */
477 
478 /*
479  * Class:     hdf_hdf5lib_H5
480  * Method:    H5Dread_int
481  * Signature: (JJJJJ[IZ)I
482  */
483 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1int(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jintArray buf,jboolean isCriticalPinning)484 Java_hdf_hdf5lib_H5_H5Dread_1int
485     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
486           jlong file_space_id, jlong xfer_plist_id, jintArray buf, jboolean isCriticalPinning)
487 {
488     jboolean  readBufIsCopy;
489     htri_t    data_class;
490     jint     *readBuf = NULL;
491     herr_t    status = FAIL;
492 
493     UNUSED(clss);
494 
495     if (NULL == buf)
496         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_int: read buffer is NULL");
497 
498     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
499         H5_LIBRARY_ERROR(ENVONLY);
500 
501     if (data_class)
502         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_int: variable length type not supported");
503 
504     /* Recursively detect any vlen string in type (compound, array ...) */
505     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
506         H5_LIBRARY_ERROR(ENVONLY);
507 
508     if (data_class)
509         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_int: variable length type not supported");
510 
511     if (isCriticalPinning) {
512         PIN_INT_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_int: read buffer not critically pinned");
513     }
514     else {
515         PIN_INT_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_int: read buffer not pinned");
516     }
517 
518     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
519             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
520         H5_LIBRARY_ERROR(ENVONLY);
521 
522 done:
523     if (readBuf) {
524         if (isCriticalPinning) {
525             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
526         }
527         else {
528             UNPIN_INT_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
529         }
530     }
531 
532     return (jint)status;
533 } /* end Java_hdf_hdf5lib_H5_H5Dread_1int */
534 
535 /*
536  * Class:     hdf_hdf5lib_H5
537  * Method:    H5Dwrite_int
538  * Signature: (JJJJJ[IZ)I
539  */
540 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1int(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jintArray buf,jboolean isCriticalPinning)541 Java_hdf_hdf5lib_H5_H5Dwrite_1int
542     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
543           jlong file_space_id, jlong xfer_plist_id, jintArray buf, jboolean isCriticalPinning)
544 {
545     jboolean  writeBufIsCopy;
546     htri_t    data_class;
547     jint     *writeBuf = NULL;
548     herr_t    status = FAIL;
549 
550     UNUSED(clss);
551 
552     if (NULL == buf)
553         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_int: write buffer is NULL");
554 
555     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
556         H5_LIBRARY_ERROR(ENVONLY);
557 
558     if (data_class)
559         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_int: variable length type not supported");
560 
561     /* Recursively detect any vlen string in type (compound, array ...) */
562     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
563         H5_LIBRARY_ERROR(ENVONLY);
564 
565     if (data_class)
566         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_int: variable length type not supported");
567 
568     if (isCriticalPinning) {
569         PIN_INT_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_int: write buffer not critically pinned");
570     }
571     else {
572         PIN_INT_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_int: write buffer not pinned");
573     }
574 
575     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
576             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
577         H5_LIBRARY_ERROR(ENVONLY);
578 
579 done:
580     if (writeBuf) {
581         if (isCriticalPinning) {
582             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
583         }
584         else {
585             UNPIN_INT_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
586         }
587     }
588 
589     return (jint)status;
590 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1int */
591 
592 /*
593  * Class:     hdf_hdf5lib_H5
594  * Method:    H5Dread_long
595  * Signature: (JJJJJ[JZ)I
596  */
597 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1long(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jlongArray buf,jboolean isCriticalPinning)598 Java_hdf_hdf5lib_H5_H5Dread_1long
599     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
600           jlong file_space_id, jlong xfer_plist_id, jlongArray buf, jboolean isCriticalPinning)
601 {
602     jboolean  readBufIsCopy;
603     htri_t    data_class;
604     jlong    *readBuf = NULL;
605     herr_t    status = FAIL;
606 
607     UNUSED(clss);
608 
609     if (NULL == buf)
610         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_long: read buffer is NULL");
611 
612     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
613         H5_LIBRARY_ERROR(ENVONLY);
614 
615     if (data_class)
616         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_long: variable length type not supported");
617 
618     /* Recursively detect any vlen string in type (compound, array ...) */
619     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
620         H5_LIBRARY_ERROR(ENVONLY);
621 
622     if (data_class)
623         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_long: variable length type not supported");
624 
625     if (isCriticalPinning) {
626         PIN_LONG_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_long: read buffer not critically pinned");
627     }
628     else {
629         PIN_LONG_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_long: read buffer not pinned");
630     }
631 
632     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
633             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
634         H5_LIBRARY_ERROR(ENVONLY);
635 
636 done:
637     if (readBuf) {
638         if (isCriticalPinning) {
639             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
640         }
641         else {
642             UNPIN_LONG_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
643         }
644     }
645 
646     return (jint)status;
647 } /* end Java_hdf_hdf5lib_H5_H5Dread_1long */
648 
649 /*
650  * Class:     hdf_hdf5lib_H5
651  * Method:    H5Dwrite_long
652  * Signature: (JJJJJ[JZ)I
653  */
654 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1long(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jlongArray buf,jboolean isCriticalPinning)655 Java_hdf_hdf5lib_H5_H5Dwrite_1long
656     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
657           jlong file_space_id, jlong xfer_plist_id, jlongArray buf, jboolean isCriticalPinning)
658 {
659     jboolean  writeBufIsCopy;
660     htri_t    data_class;
661     jlong    *writeBuf = NULL;
662     herr_t    status = FAIL;
663 
664     UNUSED(clss);
665 
666     if (NULL == buf)
667         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_long: write buffer is NULL");
668 
669     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
670         H5_LIBRARY_ERROR(ENVONLY);
671 
672     if (data_class)
673         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_long: variable length type not supported");
674 
675     /* Recursively detect any vlen string in type (compound, array ...) */
676     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
677         H5_LIBRARY_ERROR(ENVONLY);
678 
679     if (data_class)
680         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_long: variable length type not supported");
681 
682     if (isCriticalPinning) {
683         PIN_LONG_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_long: write buffer not critically pinned");
684     }
685     else {
686         PIN_LONG_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_long: write buffer not pinned");
687     }
688 
689     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
690             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
691         H5_LIBRARY_ERROR(ENVONLY);
692 
693 done:
694     if (writeBuf) {
695         if (isCriticalPinning) {
696             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
697         }
698         else {
699             UNPIN_LONG_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
700         }
701     }
702 
703     return (jint)status;
704 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1long */
705 
706 /*
707  * Class:     hdf_hdf5lib_H5
708  * Method:    H5Dread_float
709  * Signature: (JJJJJ[FZ)I
710  */
711 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1float(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jfloatArray buf,jboolean isCriticalPinning)712 Java_hdf_hdf5lib_H5_H5Dread_1float
713     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
714           jlong file_space_id, jlong xfer_plist_id, jfloatArray buf, jboolean isCriticalPinning)
715 {
716     jboolean  readBufIsCopy;
717     htri_t    data_class;
718     jfloat   *readBuf = NULL;
719     herr_t    status = FAIL;
720 
721     UNUSED(clss);
722 
723     if (NULL == buf)
724         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_float: read buffer is NULL");
725 
726     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
727         H5_LIBRARY_ERROR(ENVONLY);
728 
729     if (data_class)
730         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_float: variable length type not supported");
731 
732     /* Recursively detect any vlen string in type (compound, array ...) */
733     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
734         H5_LIBRARY_ERROR(ENVONLY);
735 
736     if (data_class)
737         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_float: variable length type not supported");
738 
739     if (isCriticalPinning) {
740         PIN_FLOAT_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_float: read buffer not critically pinned");
741     }
742     else {
743         PIN_FLOAT_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_float: read buffer not pinned");
744     }
745 
746     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
747             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
748         H5_LIBRARY_ERROR(ENVONLY);
749 
750 done:
751     if (readBuf) {
752         if (isCriticalPinning) {
753             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
754         }
755         else {
756             UNPIN_FLOAT_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
757         }
758     }
759 
760     return (jint)status;
761 } /* end Java_hdf_hdf5lib_H5_H5Dread_1float */
762 
763 /*
764  * Class:     hdf_hdf5lib_H5
765  * Method:    H5Dwrite_float
766  * Signature: (JJJJJ[FZ)I
767  */
768 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1float(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jfloatArray buf,jboolean isCriticalPinning)769 Java_hdf_hdf5lib_H5_H5Dwrite_1float
770     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
771           jlong file_space_id, jlong xfer_plist_id, jfloatArray buf, jboolean isCriticalPinning)
772 {
773     jboolean  writeBufIsCopy;
774     htri_t    data_class;
775     jfloat   *writeBuf = NULL;
776     herr_t    status = FAIL;
777 
778     UNUSED(clss);
779 
780     if (NULL == buf)
781         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_float: write buffer is NULL");
782 
783     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
784         H5_LIBRARY_ERROR(ENVONLY);
785 
786     if (data_class)
787         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_float: variable length type not supported");
788 
789     /* Recursively detect any vlen string in type (compound, array ...) */
790     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
791         H5_LIBRARY_ERROR(ENVONLY);
792 
793     if (data_class)
794         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_float: variable length type not supported");
795 
796     if (isCriticalPinning) {
797         PIN_FLOAT_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_float: write buffer not critically pinned");
798     }
799     else {
800         PIN_FLOAT_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_float: write buffer not pinned");
801     }
802 
803     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
804             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
805         H5_LIBRARY_ERROR(ENVONLY);
806 
807 done:
808     if (writeBuf) {
809         if (isCriticalPinning) {
810             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
811         }
812         else {
813             UNPIN_FLOAT_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
814         }
815     }
816 
817     return (jint)status;
818 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1float */
819 
820 /*
821  * Class:     hdf_hdf5lib_H5
822  * Method:    H5Dread_double
823  * Signature: (JJJJJ[DZ)I
824  */
825 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1double(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jdoubleArray buf,jboolean isCriticalPinning)826 Java_hdf_hdf5lib_H5_H5Dread_1double
827     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
828           jlong file_space_id, jlong xfer_plist_id, jdoubleArray buf, jboolean isCriticalPinning)
829 {
830     jboolean  readBufIsCopy;
831     jdouble  *readBuf = NULL;
832     htri_t    data_class;
833     herr_t    status = FAIL;
834 
835     UNUSED(clss);
836 
837     if (NULL == buf)
838         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_double: read buffer is NULL");
839 
840     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
841         H5_LIBRARY_ERROR(ENVONLY);
842 
843     if (data_class)
844         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_double: variable length type not supported");
845 
846     /* Recursively detect any vlen string in type (compound, array ...) */
847     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
848         H5_LIBRARY_ERROR(ENVONLY);
849 
850     if (data_class)
851         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_double: variable length type not supported");
852 
853     if (isCriticalPinning) {
854         PIN_DOUBLE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_double: read buffer not critically pinned");
855     }
856     else {
857         PIN_DOUBLE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread_double: read buffer not pinned");
858     }
859 
860     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
861             (hid_t)file_space_id, (hid_t)xfer_plist_id, readBuf)) < 0)
862         H5_LIBRARY_ERROR(ENVONLY);
863 
864 done:
865     if (readBuf) {
866         if (isCriticalPinning) {
867             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
868         }
869         else {
870             UNPIN_DOUBLE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0);
871         }
872     }
873 
874     return (jint)status;
875 } /* end Java_hdf_hdf5lib_H5_H5Dread_1double */
876 
877 /*
878  * Class:     hdf_hdf5lib_H5
879  * Method:    H5Dwrite_double
880  * Signature: (JJJJJ[DZ)I
881  */
882 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1double(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jdoubleArray buf,jboolean isCriticalPinning)883 Java_hdf_hdf5lib_H5_H5Dwrite_1double
884     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
885           jlong file_space_id, jlong xfer_plist_id, jdoubleArray buf, jboolean isCriticalPinning)
886 {
887     jboolean  writeBufIsCopy;
888     jdouble  *writeBuf = NULL;
889     htri_t    data_class;
890     herr_t    status = FAIL;
891 
892     UNUSED(clss);
893 
894     if (NULL == buf)
895         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_double: write buffer is NULL");
896 
897     if ((data_class = H5Tdetect_class(mem_type_id, H5T_VLEN)) < 0)
898         H5_LIBRARY_ERROR(ENVONLY);
899 
900     if (data_class)
901         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_double: variable length type not supported");
902 
903     /* Recursively detect any vlen string in type (compound, array ...) */
904     if ((data_class = H5Tdetect_variable_str(mem_type_id)) < 0)
905         H5_LIBRARY_ERROR(ENVONLY);
906 
907     if (data_class)
908         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_double: variable length type not supported");
909 
910     if (isCriticalPinning) {
911         PIN_DOUBLE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_double: write buffer not critically pinned");
912     }
913     else {
914         PIN_DOUBLE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite_double: write buffer not pinned");
915     }
916 
917     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
918             (hid_t)file_space_id, (hid_t)xfer_plist_id, writeBuf)) < 0)
919         H5_LIBRARY_ERROR(ENVONLY);
920 
921 done:
922     if (writeBuf) {
923         if (isCriticalPinning) {
924             UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
925         }
926         else {
927             UNPIN_DOUBLE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0);
928         }
929     }
930 
931     return (jint)status;
932 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1double */
933 
934 /*
935  * Class:     hdf_hdf5lib_H5
936  * Method:    H5Dread_string
937  * Signature: (JJJJJ[Ljava/lang/String;)I
938  */
939 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1string(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray j_buf)940 Java_hdf_hdf5lib_H5_H5Dread_1string
941     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
942           jlong file_space_id, jlong xfer_plist_id, jobjectArray j_buf)
943 {
944     jstring  jstr;
945     size_t   str_len;
946     size_t   pos;
947     jsize    i, n;
948     char    *c_buf = NULL;
949     char    *cstr = NULL;
950     herr_t   status = FAIL;
951 
952     UNUSED(clss);
953 
954     if (NULL == j_buf)
955         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_string: read buffer is NULL");
956 
957     if ((n = ENVPTR->GetArrayLength(ENVONLY, j_buf)) <= 0) {
958         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
959         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_string: read buffer length <= 0");
960     }
961 
962     if (!(str_len = H5Tget_size((hid_t)mem_type_id)))
963         H5_LIBRARY_ERROR(ENVONLY);
964 
965     if (NULL == (cstr = (char *) HDmalloc(str_len + 1)))
966         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dread_string: memory allocation failed");
967 
968     if (NULL == (c_buf = (char *) HDmalloc((size_t)n * str_len)))
969         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dread_string: memory allocation failed");
970 
971     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
972             (hid_t)file_space_id, (hid_t)xfer_plist_id, c_buf)) < 0)
973         H5_LIBRARY_ERROR(ENVONLY);
974 
975     for (i = 0, pos = 0; i < n; i++) {
976         HDmemcpy(cstr, c_buf+pos, str_len);
977         cstr[str_len] = '\0';
978 
979         if (NULL == (jstr = ENVPTR->NewStringUTF(ENVONLY, cstr))) {
980             CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
981             H5_JNI_FATAL_ERROR(ENVONLY, "H5Dread_string: out of memory - unable to construct string from UTF characters");
982         }
983 
984         ENVPTR->SetObjectArrayElement(ENVONLY, j_buf, i, jstr);
985         CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
986 
987         pos += str_len;
988 
989         ENVPTR->DeleteLocalRef(ENVONLY, jstr);
990     } /* end for */
991 
992 done:
993     if (c_buf)
994         HDfree(c_buf);
995     if (cstr)
996         HDfree(cstr);
997 
998     return (jint)status;
999 } /* end Java_hdf_hdf5lib_H5_H5Dread_1string */
1000 
1001 /*
1002  * Class:     hdf_hdf5lib_H5
1003  * Method:    H5Dwrite_string
1004  * Signature: (JJJJJ[Ljava/lang/String;)I
1005  */
1006 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1string(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray j_buf)1007 Java_hdf_hdf5lib_H5_H5Dwrite_1string
1008     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1009           jlong file_space_id, jlong xfer_plist_id, jobjectArray j_buf)
1010 {
1011     const char *utf8 = NULL;
1012     jstring     obj;
1013     size_t      i, str_len;
1014     jsize       n;
1015     char       *c_buf = NULL;
1016     herr_t      status = FAIL;
1017 
1018     UNUSED(clss);
1019 
1020     if (NULL == j_buf)
1021         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_string: write buffer is NULL");
1022 
1023     if ((n = ENVPTR->GetArrayLength(ENVONLY, j_buf)) <= 0) {
1024         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1025         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_string: write buffer length <= 0");
1026     }
1027 
1028     if (!(str_len = H5Tget_size((hid_t)mem_type_id)))
1029         H5_LIBRARY_ERROR(ENVONLY);
1030 
1031     if (NULL == (c_buf = (char *) HDmalloc((size_t)n * str_len)))
1032         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dwrite_string: memory allocation failed");
1033 
1034     for (i = 0; i < (size_t) n; i++) {
1035         if (NULL == (obj = (jstring) ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)j_buf, (jsize) i))) {
1036             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1037 
1038             /*
1039              * If the string object was NULL, skip it.
1040              */
1041             HDmemset(&c_buf[i * str_len], 0, str_len);
1042             continue;
1043         }
1044 
1045         /*
1046          * length = ENVPTR->GetStringUTFLength(ENVONLY, obj);
1047          * CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1048          */
1049 
1050         PIN_JAVA_STRING(ENVONLY, obj, utf8, NULL, "H5Dwrite_string: string not pinned");
1051 
1052         HDstrncpy(&c_buf[i * str_len], utf8, str_len);
1053 
1054         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1055         utf8 = NULL;
1056 
1057         ENVPTR->DeleteLocalRef(ENVONLY, obj);
1058     } /* end for */
1059 
1060     if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id,
1061             (hid_t)file_space_id, (hid_t)xfer_plist_id, c_buf)) < 0)
1062         H5_LIBRARY_ERROR(ENVONLY);
1063 
1064 done:
1065     if (utf8)
1066         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1067     if (c_buf)
1068         HDfree(c_buf);
1069 
1070     return (jint)status;
1071 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1string */
1072 
1073 /*
1074  * Class:     hdf_hdf5lib_H5
1075  * Method:    H5DreadVL
1076  * Signature: (JJJJJ[Ljava/lang/String;)I
1077  */
1078 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5DreadVL(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray buf)1079 Java_hdf_hdf5lib_H5_H5DreadVL
1080     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1081           jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
1082 {
1083     H5T_class_t type_class;
1084     htri_t      isStr = 0;
1085     htri_t      isVlenStr = 0;
1086     htri_t      isComplex = 0;
1087     htri_t      isComplex2 = 0;
1088     hid_t       nested_tid = H5I_INVALID_HID;
1089     herr_t      status = FAIL;
1090 
1091     UNUSED(clss);
1092 
1093     if (NULL == buf)
1094         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5DreadVL: read buffer is NULL");
1095 
1096     if ((isStr = H5Tdetect_class((hid_t)mem_type_id, H5T_STRING)) < 0)
1097         H5_LIBRARY_ERROR(ENVONLY);
1098 
1099     if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
1100         H5_LIBRARY_ERROR(ENVONLY);
1101 
1102     if (type_class == H5T_COMPOUND) {
1103         unsigned i;
1104         int      num_members;
1105 
1106         if ((num_members = H5Tget_nmembers(mem_type_id)) < 0)
1107             H5_LIBRARY_ERROR(ENVONLY);
1108 
1109         for (i = 0; i < (unsigned) num_members; i++) {
1110             if ((nested_tid = H5Tget_member_type((hid_t)mem_type_id, i)) < 0)
1111                 H5_LIBRARY_ERROR(ENVONLY);
1112 
1113             if ((isComplex = H5Tdetect_class((hid_t)nested_tid, H5T_COMPOUND)) < 0)
1114                 H5_LIBRARY_ERROR(ENVONLY);
1115 
1116             if ((isComplex2 = H5Tdetect_class((hid_t)nested_tid, H5T_VLEN)) < 0)
1117                 H5_LIBRARY_ERROR(ENVONLY);
1118 
1119             isComplex = isComplex || isComplex2;
1120 
1121             if (H5Tclose(nested_tid) < 0)
1122                 H5_LIBRARY_ERROR(ENVONLY);
1123             nested_tid = H5I_INVALID_HID;
1124         }
1125     }
1126     else if (type_class == H5T_VLEN) {
1127         isVlenStr = 1; /* Strings created by H5Tvlen_create(H5T_C_S1) */
1128     }
1129 
1130     if (!isStr || isComplex || isVlenStr) {
1131         if ((status = H5DreadVL_asstr(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1132                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1133             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1134     }
1135     else if (isStr) {
1136         if ((status = H5DreadVL_str(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1137                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1138             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1139     }
1140 
1141 done:
1142     if (nested_tid >= 0)
1143         H5Tclose(nested_tid);
1144 
1145     return (jint)status;
1146 } /* end Java_hdf_hdf5lib_H5_H5Dread_1VL */
1147 
1148 /*
1149  * Helper method to read in a buffer of variable-length strings from an HDF5
1150  * dataset. Each C-string is converted to a Java string and set in the output
1151  * buffer in turn.
1152  */
1153 static herr_t
H5DreadVL_str(JNIEnv * env,hid_t did,hid_t tid,hid_t mem_sid,hid_t file_sid,hid_t xfer_plist_id,jobjectArray buf)1154 H5DreadVL_str
1155     (JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf)
1156 {
1157     jstring   jstr;
1158     jsize     i, n;
1159     char    **strs = NULL;
1160     herr_t    status = FAIL;
1161 
1162     if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
1163         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1164         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DreadVL_str: buf length < 0");
1165     }
1166 
1167     if (NULL == (strs = (char **) HDcalloc((size_t)n, sizeof(char *))))
1168         H5_JNI_FATAL_ERROR(ENVONLY, "H5DreadVL_str: failed to allocate variable length string read buffer");
1169 
1170     if ((status = H5Dread(did, tid, mem_sid, file_sid, xfer_plist_id, strs)) < 0)
1171         H5_LIBRARY_ERROR(ENVONLY);
1172 
1173     /*
1174      * When repeatedly reading a dataset with a large number of strs (e.g., 1,000,000 strings),
1175      * H5Dvlen_reclaim() may crash on Windows because the Java GC will not be able to collect
1176      * free space in time. Instead, we use "H5free_memory(strs[i])" to free individual strings
1177      * once done.
1178      */
1179     for (i = 0; i < n; i++) {
1180         if (NULL == (jstr = ENVPTR->NewStringUTF(ENVONLY, strs[i])))
1181             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1182 
1183         ENVPTR->SetObjectArrayElement(ENVONLY, buf, i, jstr);
1184         CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1185 
1186         H5free_memory(strs[i]);
1187         strs[i] = NULL;
1188 
1189         ENVPTR->DeleteLocalRef(ENVONLY, jstr);
1190     } /* end for */
1191 
1192 done:
1193     if (strs) {
1194         for (i = 0; i < n; i++) {
1195             if (strs[i])
1196                 H5free_memory(strs[i]);
1197         }
1198 
1199         HDfree(strs);
1200     }
1201 
1202     return status;
1203 } /* end H5DreadVL_str */
1204 
1205 /*
1206  * Helper method to read in a buffer of variable-length (hvl_t)
1207  * structures from an HDF5 dataset and convert each variable-length
1208  * element's buffer into a Java string. Each string is then set
1209  * in the output buffer in turn.
1210  */
1211 static herr_t
H5DreadVL_asstr(JNIEnv * env,hid_t did,hid_t tid,hid_t mem_sid,hid_t file_sid,hid_t xfer_plist_id,jobjectArray buf)1212 H5DreadVL_asstr
1213     (JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf)
1214 {
1215     H5T_class_t  tclass;
1216     jstring      jstr;
1217     h5str_t      h5str;
1218     hbool_t      close_mem_space = FALSE;
1219     size_t       typeSize;
1220     size_t       i;
1221     hid_t        mem_space = mem_sid;
1222     jsize        n;
1223     void        *readBuf = NULL;
1224     herr_t       status = FAIL;
1225 
1226     HDmemset(&h5str, 0, sizeof(h5str_t));
1227 
1228     if (mem_space == H5S_ALL) {
1229         mem_space = file_sid;
1230 
1231         if (mem_space == H5S_ALL) {
1232             /*
1233              * Retrieve a valid dataspace for H5Dvlen_reclaim().
1234              */
1235             if ((mem_space = H5Dget_space(did)) < 0)
1236                 H5_LIBRARY_ERROR(ENVONLY);
1237             close_mem_space = TRUE;
1238 
1239             if (H5Sselect_all(mem_space) < 0)
1240                 H5_LIBRARY_ERROR(ENVONLY);
1241         }
1242     }
1243 
1244     /* Get size of string array */
1245     if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
1246         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1247         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DreadVL_asstr: buf length < 0");
1248     }
1249 
1250     if (!(typeSize = H5Tget_size(tid)))
1251         H5_LIBRARY_ERROR(ENVONLY);
1252 
1253     if (NULL == (readBuf = HDcalloc((size_t)n, typeSize)))
1254         H5_JNI_FATAL_ERROR(ENVONLY, "H5DreadVL_asstr: failed to allocate read buffer");
1255 
1256     if ((status = H5Dread(did, tid, mem_sid, file_sid, xfer_plist_id, readBuf)) < 0)
1257         H5_LIBRARY_ERROR(ENVONLY);
1258 
1259     /* Allocate a decent-sized initial string */
1260     h5str_new(&h5str, 4 * typeSize);
1261 
1262     if (!h5str.s)
1263         H5_JNI_FATAL_ERROR(ENVONLY, "H5DreadVL_asstr: failed to allocate buffer");
1264 
1265     if ((tclass = H5Tget_class(tid)) < 0)
1266         H5_LIBRARY_ERROR(ENVONLY);
1267 
1268     /* Convert each element to a char string */
1269     for (i = 0; i < (size_t) n; i++) {
1270         h5str.s[0] = '\0';
1271 
1272         if (!h5str_sprintf(ENVONLY, &h5str, did, tid, &(((char *) readBuf)[i * typeSize]), typeSize, 0))
1273             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1274 
1275         if (NULL == (jstr = ENVPTR->NewStringUTF(ENVONLY, h5str.s)))
1276             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1277 
1278         ENVPTR->SetObjectArrayElement(ENVONLY, buf, (jsize) i, jstr);
1279         CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1280 
1281         ENVPTR->DeleteLocalRef(ENVONLY, jstr);
1282     } /* end for */
1283 
1284 done:
1285     if (h5str.s)
1286         h5str_free(&h5str);
1287     if (readBuf) {
1288         H5Dvlen_reclaim(tid, mem_space, xfer_plist_id, readBuf);
1289         HDfree(readBuf);
1290     }
1291     if (close_mem_space)
1292         H5Sclose(mem_space);
1293 
1294     return status;
1295 }
1296 
1297 /**
1298  *  Read VLEN data into array of arrays.
1299  *  Object[] buf contains VL arrays of data points
1300  *  Currently only deal with variable length of atomic data types
1301  */
1302 /* old version */
1303 
1304 /*
1305  * Class:     hdf_hdf5lib_H5
1306  * Method:    H5Dread_VLStrings
1307  * Signature: (JJJJJ[Ljava/lang/String;)I
1308  */
1309 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1VLStrings(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray buf)1310 Java_hdf_hdf5lib_H5_H5Dread_1VLStrings
1311     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1312           jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
1313 {
1314     htri_t isVlenStr = 0;
1315     herr_t status = FAIL;
1316 
1317     UNUSED(clss);
1318 
1319     if (NULL == buf)
1320         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread_VLStrings: read buffer is NULL");
1321 
1322     if ((isVlenStr = H5Tdetect_class((hid_t)mem_type_id, H5T_STRING)) < 0)
1323         H5_LIBRARY_ERROR(ENVONLY);
1324 
1325     if (isVlenStr) {
1326         if ((status = H5DreadVL_str(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1327                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1328             H5_LIBRARY_ERROR(ENVONLY);
1329     }
1330     else
1331         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_VLStrings: datatype is not variable length String");
1332 
1333 done:
1334     return (jint)status;
1335 } /* end Java_hdf_hdf5lib_H5_H5Dread_1VLStrings */
1336 
1337 /*
1338  * Class:     hdf_hdf5lib_H5
1339  * Method:    H5DwriteVL
1340  * Signature: (JJJJJ[Ljava/lang/String;)I
1341  */
1342 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5DwriteVL(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray buf)1343 Java_hdf_hdf5lib_H5_H5DwriteVL
1344     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1345           jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
1346 {
1347     H5T_class_t type_class;
1348     htri_t      isStr = 0;
1349     htri_t      isVlenStr = 0;
1350     htri_t      isComplex = 0;
1351     htri_t      isComplex2 = 0;
1352     hid_t       nested_tid = H5I_INVALID_HID;
1353     herr_t      status = FAIL;
1354 
1355     UNUSED(clss);
1356 
1357     if (NULL == buf)
1358         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL: write buffer is NULL");
1359 
1360     if ((isStr = H5Tdetect_class((hid_t)mem_type_id, H5T_STRING)) < 0)
1361         H5_LIBRARY_ERROR(ENVONLY);
1362 
1363     if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
1364         H5_LIBRARY_ERROR(ENVONLY);
1365 
1366     if (type_class == H5T_COMPOUND) {
1367         unsigned i;
1368         int      num_members;
1369 
1370         if ((num_members = H5Tget_nmembers(mem_type_id)) < 0)
1371             H5_LIBRARY_ERROR(ENVONLY);
1372 
1373         for (i = 0; i < (unsigned) num_members; i++) {
1374             if ((nested_tid = H5Tget_member_type((hid_t)mem_type_id, i)) < 0)
1375                 H5_LIBRARY_ERROR(ENVONLY);
1376 
1377             if ((isComplex = H5Tdetect_class((hid_t)nested_tid, H5T_COMPOUND)) < 0)
1378                 H5_LIBRARY_ERROR(ENVONLY);
1379 
1380             if ((isComplex2 = H5Tdetect_class((hid_t)nested_tid, H5T_VLEN)) < 0)
1381                 H5_LIBRARY_ERROR(ENVONLY);
1382 
1383             isComplex = isComplex || isComplex2;
1384 
1385             if (H5Tclose(nested_tid) < 0)
1386                 H5_LIBRARY_ERROR(ENVONLY);
1387             nested_tid = H5I_INVALID_HID;
1388         }
1389     }
1390     else if (type_class == H5T_VLEN) {
1391         isVlenStr = 1; /* Strings created by H5Tvlen_create(H5T_C_S1) */
1392     }
1393 
1394     if (!isStr || isComplex || isVlenStr) {
1395         if ((status = H5DwriteVL_asstr(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1396                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1397             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1398     }
1399     else if (isStr) {
1400         if ((status = H5DwriteVL_str(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1401                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1402             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1403     }
1404 
1405 done:
1406     if (nested_tid >= 0)
1407         H5Tclose(nested_tid);
1408 
1409     return (jint)status;
1410 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1VL */
1411 
1412 /*
1413  * Helper method to convert an array of Java strings into a buffer of C-strings.
1414  * The buffer of C-strings is then written to the HDF5 dataset specified.
1415  */
1416 static herr_t
H5DwriteVL_str(JNIEnv * env,hid_t dataset_id,hid_t mem_type_id,hid_t mem_space_id,hid_t file_space_id,hid_t xfer_plist_id,jobjectArray buf)1417 H5DwriteVL_str
1418     (JNIEnv *env, hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, jobjectArray buf)
1419 {
1420     const char  *utf8 = NULL;
1421     jstring      obj;
1422     jsize        size;
1423     jsize        i;
1424     char       **writeBuf = NULL;
1425     herr_t       status = FAIL;
1426 
1427     if ((size = ENVPTR->GetArrayLength(ENVONLY, (jarray) buf)) < 0) {
1428         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1429         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL_str: buf length < 0");
1430     }
1431 
1432     if (NULL == (writeBuf = (char **) HDcalloc((size_t)size + 1, sizeof(char *))))
1433         H5_JNI_FATAL_ERROR(ENVONLY, "H5DwriteVL_str: failed to allocate variable length string write buffer");
1434 
1435     for (i = 0; i < size; ++i) {
1436         jsize length;
1437 
1438         if (NULL == (obj = (jstring) ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, i))) {
1439             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1440 
1441             /*
1442              * If the string object was NULL, skip it.
1443              */
1444             writeBuf[i] = NULL;
1445             continue;
1446         }
1447 
1448         length = ENVPTR->GetStringUTFLength(ENVONLY, obj);
1449         CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1450 
1451         PIN_JAVA_STRING(ENVONLY, obj, utf8, NULL, "H5DwriteVL_str: string not pinned");
1452 
1453         if (NULL == (writeBuf[i] = (char *) HDmalloc((size_t)length + 1)))
1454             H5_JNI_FATAL_ERROR(ENVONLY, "H5DwriteVL_str: failed to allocate string buffer");
1455 
1456         HDstrncpy(writeBuf[i], utf8, (size_t)length + 1);
1457         writeBuf[i][length] = '\0';
1458 
1459         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1460         utf8 = NULL;
1461 
1462         ENVPTR->DeleteLocalRef(ENVONLY, obj);
1463     } /* end for (i = 0; i < size; ++i) */
1464 
1465     if ((status = H5Dwrite(dataset_id, mem_type_id, mem_space_id, file_space_id, xfer_plist_id, writeBuf)) < 0)
1466         H5_LIBRARY_ERROR(ENVONLY);
1467 
1468 done:
1469     if (utf8)
1470         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1471     if (writeBuf) {
1472         for (i = 0; i < size; i++) {
1473             if (writeBuf[i])
1474                 HDfree(writeBuf[i]);
1475         }
1476 
1477         HDfree(writeBuf);
1478     }
1479 
1480     return status;
1481 } /* end H5DwriteVL_str */
1482 
1483 /*
1484  * Helper method to convert an array of Java strings into a buffer of
1485  * variable-length (hvl_t) elements. The buffer of variable-length
1486  * elements is then written to the HDF5 dataset.
1487  */
1488 static herr_t
H5DwriteVL_asstr(JNIEnv * env,hid_t did,hid_t tid,hid_t mem_sid,hid_t file_sid,hid_t xfer_plist_id,jobjectArray buf)1489 H5DwriteVL_asstr
1490     (JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_sid, hid_t xfer_plist_id, jobjectArray buf)
1491 {
1492     const char *utf8 = NULL;
1493     jstring     obj;
1494     hbool_t     close_mem_space = FALSE;
1495     size_t      typeSize;
1496     size_t      i;
1497     hid_t       mem_space = mem_sid;
1498     jsize       n;
1499     void       *writeBuf = NULL;
1500     herr_t      status = FAIL;
1501 
1502     if (mem_space == H5S_ALL) {
1503         mem_space = file_sid;
1504 
1505         if (mem_space == H5S_ALL) {
1506             /*
1507              * Retrieve a valid dataspace for H5Dvlen_reclaim().
1508              */
1509             if ((mem_space = H5Dget_space(did)) < 0)
1510                 H5_LIBRARY_ERROR(ENVONLY);
1511             close_mem_space = TRUE;
1512 
1513             if (H5Sselect_all(mem_space) < 0)
1514                 H5_LIBRARY_ERROR(ENVONLY);
1515         }
1516     }
1517 
1518     if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
1519         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1520         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL_asstr: buf length < 0");
1521     }
1522 
1523     if (!(typeSize = H5Tget_size(tid)))
1524         H5_LIBRARY_ERROR(ENVONLY);
1525 
1526     if (NULL == (writeBuf = HDcalloc((size_t)n, typeSize)))
1527         H5_JNI_FATAL_ERROR(ENVONLY, "H5AwriteVL_asstr: failed to allocate write buffer");
1528 
1529     for (i = 0; i < (size_t) n; ++i) {
1530         if (NULL == (obj = (jstring) ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, (jsize) i))) {
1531             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1532 
1533             /*
1534              * If the string object was NULL, skip it.
1535              */
1536             HDmemset(&(((char *) writeBuf)[i * typeSize]), 0, typeSize);
1537             continue;
1538         }
1539 
1540         /*
1541          * length = ENVPTR->GetStringUTFLength(ENVONLY, obj);
1542          * CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1543          */
1544 
1545         PIN_JAVA_STRING(ENVONLY, obj, utf8, NULL, "H5DwriteVL_asstr: failed to pin string buffer");
1546 
1547         /*
1548          * TODO: If the string isn't a copy, we should probably make
1549          * one before destroying it with h5str_convert.
1550          */
1551 
1552         if (!h5str_convert(ENVONLY, (char **) &utf8, did, tid, &(((char *) writeBuf)[i * typeSize]), 0))
1553             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1554 
1555         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1556         utf8 = NULL;
1557 
1558         ENVPTR->DeleteLocalRef(ENVONLY, obj);
1559     } /* end for (i = 0; i < size; ++i) */
1560 
1561     if ((status = H5Dwrite(did, tid, mem_sid, file_sid, xfer_plist_id, writeBuf)) < 0)
1562         H5_LIBRARY_ERROR(ENVONLY);
1563 
1564 done:
1565     if (utf8)
1566         UNPIN_JAVA_STRING(ENVONLY, obj, utf8);
1567     if (writeBuf) {
1568         H5Dvlen_reclaim(tid, mem_space, xfer_plist_id, writeBuf);
1569         HDfree(writeBuf);
1570     }
1571     if (close_mem_space)
1572         H5Sclose(mem_space);
1573 
1574     return status;
1575 } /* end H5DwriteVL_asstr */
1576 
1577 /*
1578  * Class:     hdf_hdf5lib_H5
1579  * Method:    H5Dwrite_VLStrings
1580  * Signature: (JJJJJ[Ljava/lang/String;)I
1581  */
1582 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dwrite_1VLStrings(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray buf)1583 Java_hdf_hdf5lib_H5_H5Dwrite_1VLStrings
1584     (JNIEnv *env, jclass clss, jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1585           jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
1586 {
1587     htri_t  isVlenStr = 0;
1588     herr_t  status = FAIL;
1589 
1590     UNUSED(clss);
1591 
1592     if (NULL == buf)
1593         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_VLStrings: write buffer is NULL");
1594 
1595     if ((isVlenStr = H5Tis_variable_str((hid_t)mem_type_id)) < 0)
1596         H5_LIBRARY_ERROR(ENVONLY);
1597 
1598     if (isVlenStr) {
1599         if ((status = H5DwriteVL_str(env, (hid_t)dataset_id, (hid_t)mem_type_id,
1600                 (hid_t)mem_space_id, (hid_t)file_space_id, (hid_t)xfer_plist_id, buf)) < 0)
1601             H5_LIBRARY_ERROR(ENVONLY);
1602     } /* end if */
1603     else
1604         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite_VLStrings: datatype is not variable length String");
1605 
1606 done:
1607     return (jint)status;
1608 } /* end Java_hdf_hdf5lib_H5_H5Dwrite_1VLStrings */
1609 
1610 /*
1611  * Class:     hdf_hdf5lib_H5
1612  * Method:    H5Dread_reg_ref
1613  * Signature: (JJJJJ[Ljava/lang/String;)I
1614  */
1615 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dread_1reg_1ref(JNIEnv * env,jclass clss,jlong dataset_id,jlong mem_type_id,jlong mem_space_id,jlong file_space_id,jlong xfer_plist_id,jobjectArray buf)1616 Java_hdf_hdf5lib_H5_H5Dread_1reg_1ref
1617     (JNIEnv *env, jclass clss,
1618         jlong dataset_id, jlong mem_type_id, jlong mem_space_id,
1619         jlong file_space_id, jlong xfer_plist_id, jobjectArray buf)
1620 {
1621     hdset_reg_ref_t *ref_data = NULL;
1622     h5str_t          h5str;
1623     jstring          jstr;
1624     jsize            i, n;
1625     herr_t           status = FAIL;
1626 
1627     UNUSED(clss);
1628 
1629     HDmemset(&h5str, 0, sizeof(h5str_t));
1630 
1631     if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
1632         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1633         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread_reg_ref: buf length < 0");
1634     }
1635 
1636     if (NULL == (ref_data = (hdset_reg_ref_t *) HDcalloc(1, (size_t)n * sizeof(hdset_reg_ref_t))))
1637         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dread_reg_ref: failed to allocate read buffer");
1638 
1639     if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id, (hid_t)file_space_id, xfer_plist_id, ref_data)) < 0)
1640         H5_LIBRARY_ERROR(ENVONLY);
1641 
1642     h5str_new(&h5str, 1024);
1643 
1644     if (!h5str.s)
1645         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dread_reg_ref: failed to allocate buffer");
1646 
1647     for (i = 0; i < n; i++) {
1648         h5str.s[0] = '\0';
1649 
1650         if (!h5str_sprintf(ENVONLY, &h5str, (hid_t)dataset_id, (hid_t)mem_type_id, &ref_data[i], 0, 0))
1651             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1652 
1653         if (NULL == (jstr = ENVPTR->NewStringUTF(ENVONLY, h5str.s)))
1654             CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1655 
1656         ENVPTR->SetObjectArrayElement(ENVONLY, buf, i, jstr);
1657         CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1658 
1659         ENVPTR->DeleteLocalRef(ENVONLY, jstr);
1660     } /* end for */
1661 
1662 done:
1663     if (h5str.s)
1664         h5str_free(&h5str);
1665     if (ref_data)
1666         HDfree(ref_data);
1667 
1668     return (jint)status;
1669 } /* end Java_hdf_hdf5lib_H5_H5Dread_1reg_1ref */
1670 
1671 /*
1672  * Class:     hdf_hdf5lib_H5
1673  * Method:    _H5Dcreate2
1674  * Signature: (JLjava/lang/String;JJJJJ)J
1675  */
1676 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dcreate2(JNIEnv * env,jclass clss,jlong loc_id,jstring name,jlong type_id,jlong space_id,jlong link_plist_id,jlong create_plist_id,jlong access_plist_id)1677 Java_hdf_hdf5lib_H5__1H5Dcreate2
1678     (JNIEnv *env, jclass clss, jlong loc_id, jstring name, jlong type_id,
1679           jlong space_id, jlong link_plist_id, jlong create_plist_id, jlong access_plist_id)
1680 {
1681     const char *dsetName = NULL;
1682     hid_t       dset_id = H5I_INVALID_HID;
1683 
1684     UNUSED(clss);
1685 
1686     if (NULL == name)
1687         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dcreate2: dataset name is NULL");
1688 
1689     PIN_JAVA_STRING(ENVONLY, name, dsetName, NULL, "H5Dcreate2: dataset name not pinned");
1690 
1691     if ((dset_id = H5Dcreate2((hid_t)loc_id, dsetName, (hid_t)type_id, (hid_t)space_id, (hid_t)link_plist_id, (hid_t)create_plist_id, (hid_t)access_plist_id)) < 0)
1692         H5_LIBRARY_ERROR(ENVONLY);
1693 
1694 done:
1695     if (dsetName)
1696         UNPIN_JAVA_STRING(ENVONLY, name, dsetName);
1697 
1698     return (jlong)dset_id;
1699 } /* end Java_hdf_hdf5lib_H5__1H5Dcreate2 */
1700 
1701 /*
1702  * Class:     hdf_hdf5lib_H5
1703  * Method:    _H5Dopen2
1704  * Signature: (JLjava/lang/String;J)J
1705  */
1706 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dopen2(JNIEnv * env,jclass clss,jlong loc_id,jstring name,jlong access_plist)1707 Java_hdf_hdf5lib_H5__1H5Dopen2
1708     (JNIEnv *env, jclass clss, jlong loc_id, jstring name, jlong access_plist)
1709 {
1710     const char *dsetName = NULL;
1711     hid_t       dset_id = H5I_INVALID_HID;
1712 
1713     UNUSED(clss);
1714 
1715     if (NULL == name)
1716         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dopen2: dataset name is NULL");
1717 
1718     PIN_JAVA_STRING(ENVONLY, name, dsetName, NULL, "H5Dopen2: dataset name not pinned");
1719 
1720     if ((dset_id = H5Dopen2((hid_t)loc_id, dsetName, (hid_t)access_plist)) < 0)
1721         H5_LIBRARY_ERROR(ENVONLY);
1722 
1723 done:
1724     if (dsetName)
1725         UNPIN_JAVA_STRING(ENVONLY, name, dsetName);
1726 
1727     return (jlong)dset_id;
1728 } /* end Java_hdf_hdf5lib_H5__1H5Dopen2 */
1729 
1730 /*
1731  * Class:     hdf_hdf5lib_H5
1732  * Method:    _H5Dcreate_anon
1733  * Signature: (JJJJJ)J
1734  */
1735 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5__1H5Dcreate_1anon(JNIEnv * env,jclass clss,jlong loc_id,jlong type_id,jlong space_id,jlong dcpl_id,jlong dapl_id)1736 Java_hdf_hdf5lib_H5__1H5Dcreate_1anon
1737     (JNIEnv *env, jclass clss, jlong loc_id, jlong type_id, jlong space_id, jlong dcpl_id, jlong dapl_id)
1738 {
1739     hid_t dset_id = H5I_INVALID_HID;
1740 
1741     UNUSED(clss);
1742 
1743     if ((dset_id = H5Dcreate_anon((hid_t)loc_id, (hid_t)type_id, (hid_t)space_id, (hid_t)dcpl_id, (hid_t)dapl_id)) < 0)
1744         H5_LIBRARY_ERROR(ENVONLY);
1745 
1746 done:
1747     return (jlong)dset_id;
1748 } /* end Java_hdf_hdf5lib_H5__1H5Dcreate_1anon */
1749 
1750 /*
1751  * Class:     hdf_hdf5lib_H5
1752  * Method:    H5Dget_space_status
1753  * Signature: (J)I;
1754  */
1755 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Dget_1space_1status(JNIEnv * env,jclass clss,jlong loc_id)1756 Java_hdf_hdf5lib_H5_H5Dget_1space_1status
1757     (JNIEnv *env, jclass clss, jlong loc_id)
1758 {
1759     H5D_space_status_t space_status = H5D_SPACE_STATUS_ERROR;
1760 
1761     UNUSED(clss);
1762 
1763     if (H5Dget_space_status((hid_t)loc_id, &space_status) < 0)
1764         H5_LIBRARY_ERROR(ENVONLY);
1765 
1766 done:
1767     return (jint)space_status;
1768 } /* end Java_hdf_hdf5lib_H5_H5Dget_1space_1status */
1769 
1770 /*
1771  * Class:     hdf_hdf5lib_H5
1772  * Method:    H5Dget_access_plist
1773  * Signature: (J)J
1774  */
1775 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Dget_1access_1plist(JNIEnv * env,jclass clss,jlong loc_id)1776 Java_hdf_hdf5lib_H5_H5Dget_1access_1plist
1777     (JNIEnv *env, jclass clss, jlong loc_id)
1778 {
1779     hid_t retVal = H5I_INVALID_HID;
1780 
1781     UNUSED(clss);
1782 
1783     if ((retVal = H5Dget_access_plist((hid_t)loc_id)) < 0)
1784         H5_LIBRARY_ERROR(ENVONLY);
1785 
1786 done:
1787     return (jlong)retVal;
1788 } /* end Java_hdf_hdf5lib_H5_H5Dget_1access_1plist */
1789 
1790 /*
1791  * Class:     hdf_hdf5lib_H5
1792  * Method:    H5Dget_offset
1793  * Signature: (J)J
1794  */
1795 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Dget_1offset(JNIEnv * env,jclass clss,jlong loc_id)1796 Java_hdf_hdf5lib_H5_H5Dget_1offset
1797     (JNIEnv *env, jclass clss, jlong loc_id)
1798 {
1799     haddr_t offset = HADDR_UNDEF;
1800 
1801     UNUSED(clss);
1802 
1803     if (HADDR_UNDEF == (offset = H5Dget_offset((hid_t)loc_id)))
1804         H5_LIBRARY_ERROR(ENVONLY);
1805 
1806 done:
1807     return (jlong)offset;
1808 } /* end Java_hdf_hdf5lib_H5_H5Dget_1offset */
1809 
1810 /*
1811  * Class:     hdf_hdf5lib_H5
1812  * Method:    H5Dvlen_get_buf_size
1813  * Signature: (JJJ)J
1814  */
1815 JNIEXPORT jlong JNICALL
Java_hdf_hdf5lib_H5_H5Dvlen_1get_1buf_1size(JNIEnv * env,jclass clss,jlong dataset_id,jlong type_id,jlong space_id)1816 Java_hdf_hdf5lib_H5_H5Dvlen_1get_1buf_1size
1817     (JNIEnv *env, jclass clss, jlong dataset_id, jlong type_id, jlong space_id)
1818 {
1819     hsize_t sz = 0;
1820 
1821     UNUSED(clss);
1822 
1823     if (H5Dvlen_get_buf_size((hid_t)dataset_id, (hid_t)type_id, (hid_t)space_id, &sz) < 0)
1824         H5_LIBRARY_ERROR(ENVONLY);
1825 
1826 done:
1827     return (jlong)sz;
1828 } /* end Java_hdf_hdf5lib_H5_H5Dvlen_1get_1buf_1size_1long */
1829 
1830 /*
1831  * Class:     hdf_hdf5lib_H5
1832  * Method:    H5Dfill
1833  * Signature: ([BJ[BJJ)V
1834  */
1835 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Dfill(JNIEnv * env,jclass clss,jbyteArray fill,jlong fill_type_id,jbyteArray buf,jlong buf_type_id,jlong space_id)1836 Java_hdf_hdf5lib_H5_H5Dfill
1837     (JNIEnv *env, jclass clss, jbyteArray fill, jlong fill_type_id, jbyteArray buf, jlong buf_type_id, jlong space_id)
1838 {
1839     jboolean  isCopy1;
1840     jboolean  isCopy2;
1841     herr_t    status = FAIL;
1842     jbyte    *fillP = NULL;
1843     jbyte    *buffP = NULL;
1844 
1845     UNUSED(clss);
1846 
1847     if (NULL == buf)
1848         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dfill: buffer is NULL");
1849 
1850     PIN_BYTE_ARRAY(ENVONLY, buf, buffP, &isCopy2, "H5Dfill: buffer not pinned");
1851 
1852     if (fill) {
1853         PIN_BYTE_ARRAY(ENVONLY, fill, fillP, &isCopy1, "H5Dfill: fill buffer not pinned");
1854     }
1855 
1856     if ((status = H5Dfill((const void *)fillP, (hid_t)fill_type_id, (void *)buffP, (hid_t)buf_type_id, (hid_t)space_id)) < 0)
1857         H5_LIBRARY_ERROR(ENVONLY);
1858 
1859     if (fillP) {
1860         UNPIN_BYTE_ARRAY(ENVONLY, fill, fillP, JNI_ABORT);
1861         fillP = NULL;
1862     }
1863 
1864 done:
1865     if (fillP)
1866         UNPIN_BYTE_ARRAY(ENVONLY, fill, fillP, JNI_ABORT);
1867     if (buffP)
1868         UNPIN_BYTE_ARRAY(ENVONLY, buf, buffP, (status < 0) ? JNI_ABORT : 0);
1869 } /* end Java_hdf_hdf5lib_H5_H5Dfill */
1870 
1871 /*
1872  * Class:     hdf_hdf5lib_H5
1873  * Method:    H5Dset_extent
1874  * Signature: (J[J)V
1875  */
1876 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Dset_1extent(JNIEnv * env,jclass clss,jlong loc_id,jlongArray buf)1877 Java_hdf_hdf5lib_H5_H5Dset_1extent
1878     (JNIEnv *env, jclass clss, jlong loc_id, jlongArray buf)
1879 {
1880     jboolean  isCopy;
1881     hsize_t  *dims = NULL;
1882     herr_t    status;
1883     jsize     rank;
1884     jlong    *dimsBuf = NULL;
1885     int       i = 0;
1886 
1887     UNUSED(clss);
1888 
1889     if (NULL == buf)
1890         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dset_extent: buffer is NULL");
1891 
1892     if ((rank = ENVPTR->GetArrayLength(ENVONLY, buf)) <= 0) {
1893         CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
1894         H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dset_extent: buf length <= 0");
1895     }
1896 
1897     PIN_LONG_ARRAY(ENVONLY, buf, dimsBuf, &isCopy, "H5Dset_extent: buffer not pinned");
1898 
1899     if (NULL == (dims = (hsize_t *) HDmalloc((size_t)rank * sizeof(hsize_t))))
1900         H5_JNI_FATAL_ERROR(ENVONLY, "H5Dset_extent: failed to allocate dataset dimension buffer");
1901 
1902     for (i = 0; i < rank; i++)
1903         dims[i] = (hsize_t)dimsBuf[i];
1904 
1905     if ((status = H5Dset_extent((hid_t)loc_id, (hsize_t *)dims)) < 0)
1906         H5_LIBRARY_ERROR(ENVONLY);
1907 
1908 done:
1909     if (dims)
1910         HDfree(dims);
1911     if (dimsBuf)
1912         UNPIN_LONG_ARRAY(ENVONLY, buf, dimsBuf, JNI_ABORT);
1913 } /* end Java_hdf_hdf5lib_H5_H5Dset_1extent */
1914 
1915 static herr_t
H5D_iterate_cb(void * elem,hid_t elem_id,unsigned ndim,const hsize_t * point,void * cb_data)1916 H5D_iterate_cb
1917     (void *elem, hid_t elem_id, unsigned ndim, const hsize_t *point, void *cb_data) {
1918     cb_wrapper *wrapper = (cb_wrapper *)cb_data;
1919     jbyteArray  elemArray;
1920     jlongArray  pointArray;
1921     jmethodID   mid;
1922     jobject     visit_callback = wrapper->visit_callback;
1923     jclass      cls;
1924     JNIEnv     *cbenv = NULL;
1925     jint        status = FAIL;
1926     jsize       size;
1927     void       *op_data = (void *)wrapper->op_data;
1928 
1929     if (JVMPTR->AttachCurrentThread(JVMPAR, (void **)&cbenv, NULL) < 0) {
1930         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_TRUE);
1931         H5_JNI_FATAL_ERROR(CBENVONLY, "H5D_iterate_cb: failed to attach current thread to JVM");
1932     }
1933 
1934     if (NULL == (cls = CBENVPTR->GetObjectClass(CBENVONLY, visit_callback)))
1935         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1936 
1937     if (NULL == (mid = CBENVPTR->GetMethodID(CBENVONLY, cls, "callback", "([BJI[JLhdf/hdf5lib/callbacks/H5D_iterate_t;)I")))
1938         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1939 
1940     if (NULL == elem)
1941         H5_NULL_ARGUMENT_ERROR(CBENVONLY, "H5D_iterate_cb: element buffer is NULL");
1942     if (NULL == point)
1943         H5_NULL_ARGUMENT_ERROR(CBENVONLY, "H5D_iterate_cb: point is NULL");
1944 
1945     if (!(size = (jsize)H5Tget_size(elem_id)))
1946         H5_LIBRARY_ERROR(CBENVONLY);
1947 
1948     if (NULL == (elemArray = CBENVPTR->NewByteArray(CBENVONLY, size)))
1949         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1950 
1951     CBENVPTR->SetByteArrayRegion(CBENVONLY, elemArray, 0, size, (jbyte *)elem);
1952     CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1953 
1954     if (NULL == (pointArray = CBENVPTR->NewLongArray(CBENVONLY, 2)))
1955         CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1956 
1957     CBENVPTR->SetLongArrayRegion(CBENVONLY, pointArray, 0, 2, (const jlong *)point);
1958     CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1959 
1960     status = CBENVPTR->CallIntMethod(CBENVONLY, visit_callback, mid, (void *)elemArray, elem_id, ndim, pointArray, op_data);
1961     CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1962 
1963     CBENVPTR->GetByteArrayRegion(CBENVONLY, elemArray, 0, size, (jbyte *)elem);
1964     CHECK_JNI_EXCEPTION(CBENVONLY, JNI_FALSE);
1965 
1966 done:
1967     if (cbenv)
1968         JVMPTR->DetachCurrentThread(JVMPAR);
1969 
1970     return (herr_t)status;
1971 } /* end H5D_iterate_cb */
1972 
1973 /*
1974  * Class:     hdf_hdf5lib_H5
1975  * Method:    H5Diterate
1976  * Signature: ([BJJLjava/lang/Object;Ljava/lang/Object;)I
1977  */
1978 JNIEXPORT jint JNICALL
Java_hdf_hdf5lib_H5_H5Diterate(JNIEnv * env,jclass clss,jbyteArray buf,jlong buf_type,jlong space,jobject callback_op,jobject op_data)1979 Java_hdf_hdf5lib_H5_H5Diterate
1980     (JNIEnv *env, jclass clss, jbyteArray buf, jlong buf_type, jlong space,
1981           jobject callback_op, jobject op_data)
1982 {
1983     cb_wrapper  wrapper = { callback_op, op_data };
1984     jboolean    isCopy;
1985     jbyte      *iterBuf = NULL;
1986     herr_t      status = FAIL;
1987 
1988     UNUSED(clss);
1989 
1990     ENVPTR->GetJavaVM(ENVONLY, &jvm);
1991     CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE);
1992 
1993     if (NULL == op_data)
1994         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Diterate: op_data is NULL");
1995     if (NULL == callback_op)
1996         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Diterate: callback_op is NULL");
1997     if (NULL == buf)
1998         H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Diterate: buffer is NULL");
1999 
2000     PIN_BYTE_ARRAY(ENVONLY, buf, iterBuf, &isCopy, "H5Diterate: buffer not pinned");
2001 
2002     if ((status = H5Diterate((void *)iterBuf, (hid_t)buf_type, (hid_t)space, (H5D_operator_t)H5D_iterate_cb, (void *)&wrapper)) < 0)
2003         H5_LIBRARY_ERROR(ENVONLY);
2004 
2005 done:
2006     if (iterBuf)
2007         UNPIN_BYTE_ARRAY(ENVONLY, buf, iterBuf, (status < 0) ? (JNI_ABORT) : ((isCopy == JNI_TRUE) ? 0 : JNI_ABORT));
2008 
2009     return (jint)status;
2010 } /* end Java_hdf_hdf5lib_H5_H5Diterate */
2011 
2012 /*
2013  * Class:     hdf_hdf5lib_H5
2014  * Method:    H5Dflush
2015  * Signature: (J)V
2016  */
2017 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Dflush(JNIEnv * env,jclass clss,jlong loc_id)2018 Java_hdf_hdf5lib_H5_H5Dflush
2019     (JNIEnv *env, jclass clss, jlong loc_id)
2020 {
2021     UNUSED(clss);
2022 
2023     if (H5Dflush((hid_t)loc_id) < 0)
2024         H5_LIBRARY_ERROR(ENVONLY);
2025 
2026 done:
2027     return;
2028 }
2029 
2030 /*
2031  * Class:     hdf_hdf5lib_H5
2032  * Method:    H5Drefresh
2033  * Signature: (J)V
2034  */
2035 JNIEXPORT void JNICALL
Java_hdf_hdf5lib_H5_H5Drefresh(JNIEnv * env,jclass clss,jlong loc_id)2036 Java_hdf_hdf5lib_H5_H5Drefresh
2037     (JNIEnv *env, jclass clss, jlong loc_id)
2038 {
2039     UNUSED(clss);
2040 
2041     if (H5Drefresh((hid_t)loc_id) < 0)
2042         H5_LIBRARY_ERROR(ENVONLY);
2043 
2044 done:
2045     return;
2046 }
2047 
2048 #ifdef __cplusplus
2049 } /* end extern "C" */
2050 #endif /* __cplusplus */
2051