summaryrefslogtreecommitdiffstats
path: root/java/src/jni/h5dImp.c
diff options
context:
space:
mode:
authorAllen Byrne <50328838+byrnHDF@users.noreply.github.com>2022-11-16 04:44:33 (GMT)
committerGitHub <noreply@github.com>2022-11-16 04:44:33 (GMT)
commit719d800499c6c3a1abbb92871069eed53222fdbb (patch)
tree23d0086b07472a011f24159d8b6d54ec1047e917 /java/src/jni/h5dImp.c
parent03cc2210165cc02fd50c87e6653c4b95b0bd7d83 (diff)
downloadhdf5-719d800499c6c3a1abbb92871069eed53222fdbb.zip
hdf5-719d800499c6c3a1abbb92871069eed53222fdbb.tar.gz
hdf5-719d800499c6c3a1abbb92871069eed53222fdbb.tar.bz2
Refactor JNI translate functions to a recursive switch on datatype (#2232)
Diffstat (limited to 'java/src/jni/h5dImp.c')
-rw-r--r--java/src/jni/h5dImp.c89
1 files changed, 83 insertions, 6 deletions
diff --git a/java/src/jni/h5dImp.c b/java/src/jni/h5dImp.c
index 1a24bf6..56c5cc8 100644
--- a/java/src/jni/h5dImp.c
+++ b/java/src/jni/h5dImp.c
@@ -1134,10 +1134,47 @@ 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)
{
- herr_t status = FAIL;
+ jboolean readBufIsCopy;
+ jbyte *readBuf = NULL;
+ size_t typeSize;
+ H5T_class_t type_class;
+ jsize vl_array_len;
+ htri_t vl_data_class;
+ herr_t status = FAIL;
- status = Java_hdf_hdf5lib_H5_H5Dread(env, clss, dataset_id, mem_type_id, mem_space_id, file_space_id,
- xfer_plist_id, (jbyteArray)buf, JNI_TRUE);
+ UNUSED(clss);
+
+ if (NULL == buf)
+ H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dread: read buffer is NULL");
+
+ if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+ /* Get size of data array */
+ if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
+ CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
+ H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread: readBuf length < 0");
+ }
+
+ if (!(typeSize = H5Tget_size(mem_type_id)))
+ H5_LIBRARY_ERROR(ENVONLY);
+
+ if (NULL == (readBuf = HDcalloc((size_t)vl_array_len, typeSize)))
+ H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Dread: failed to allocate raw VL read buffer");
+
+ if ((status = H5Dread((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id, (hid_t)file_space_id,
+ (hid_t)xfer_plist_id, (void *)readBuf)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+ if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+
+ translate_rbuf(env, buf, mem_type_id, type_class, vl_array_len, readBuf);
+
+done:
+ if (readBuf) {
+ if ((status >= 0) && vl_data_class)
+ H5Treclaim(dataset_id, mem_space_id, H5P_DEFAULT, readBuf);
+ HDfree(readBuf);
+ }
return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5DreadVL */
@@ -1151,10 +1188,50 @@ 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)
{
- herr_t status = FAIL;
+ jboolean writeBufIsCopy;
+ jbyte *writeBuf = NULL;
+ size_t typeSize;
+ H5T_class_t type_class;
+ jsize vl_array_len; // Only used by vl_data_class types
+ htri_t vl_data_class;
+ herr_t status = FAIL;
+
+ UNUSED(clss);
+
+ if (NULL == buf)
+ H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5Dwrite: write buffer is NULL");
+
+ if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+
+ /* Get size of data array */
+ if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) {
+ CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE);
+ H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dwrite: write buffer length < 0");
+ }
- status = Java_hdf_hdf5lib_H5_H5Dwrite(env, clss, dataset_id, mem_type_id, mem_space_id, file_space_id,
- xfer_plist_id, (jbyteArray)buf, JNI_TRUE);
+ if (!(typeSize = H5Tget_size(mem_type_id)))
+ H5_LIBRARY_ERROR(ENVONLY);
+
+ if (NULL == (writeBuf = HDcalloc((size_t)vl_array_len, typeSize)))
+ H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Dwrite: failed to allocate raw VL write buffer");
+
+ if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+
+ translate_wbuf(ENVONLY, buf, mem_type_id, type_class, vl_array_len, writeBuf);
+
+ if ((status = H5Dwrite((hid_t)dataset_id, (hid_t)mem_type_id, (hid_t)mem_space_id, (hid_t)file_space_id,
+ (hid_t)xfer_plist_id, writeBuf)) < 0)
+ H5_LIBRARY_ERROR(ENVONLY);
+
+done:
+ if (writeBuf) {
+ if ((status >= 0) && vl_data_class)
+ H5Treclaim(dataset_id, mem_space_id, H5P_DEFAULT, writeBuf);
+
+ HDfree(writeBuf);
+ }
return (jint)status;
} /* end Java_hdf_hdf5lib_H5_H5DwriteVL */