diff options
author | Allen Byrne <50328838+byrnHDF@users.noreply.github.com> | 2023-08-29 21:28:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-29 21:28:52 (GMT) |
commit | 1dec26ca327c615e9f34e981102998d3256010da (patch) | |
tree | e4a93205afd734852d0d3137f6e711e8bfd9dbd6 /java | |
parent | 6129233f4e5c892623e0533b13de3b987af436ea (diff) | |
download | hdf5-1dec26ca327c615e9f34e981102998d3256010da.zip hdf5-1dec26ca327c615e9f34e981102998d3256010da.tar.gz hdf5-1dec26ca327c615e9f34e981102998d3256010da.tar.bz2 |
Sync Java changes - particularly VL handling (#3442)
Diffstat (limited to 'java')
-rw-r--r-- | java/src/jni/h5aImp.c | 597 | ||||
-rw-r--r-- | java/src/jni/h5dImp.c | 561 | ||||
-rw-r--r-- | java/src/jni/h5util.c | 891 | ||||
-rw-r--r-- | java/src/jni/h5util.h | 5 | ||||
-rw-r--r-- | java/test/TestH5A.java | 436 | ||||
-rw-r--r-- | java/test/TestH5D.java | 819 | ||||
-rw-r--r-- | java/test/testfiles/JUnit-TestH5A.txt | 5 | ||||
-rw-r--r-- | java/test/testfiles/JUnit-TestH5D.txt | 8 |
8 files changed, 2384 insertions, 938 deletions
diff --git a/java/src/jni/h5aImp.c b/java/src/jni/h5aImp.c index d69ab95..2058d61 100644 --- a/java/src/jni/h5aImp.c +++ b/java/src/jni/h5aImp.c @@ -155,13 +155,15 @@ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5Aread(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jbyteArray buf, jboolean isCriticalPinning) { - jboolean readBufIsCopy; - jbyte *readBuf = NULL; - hsize_t dims[H5S_MAX_RANK]; - hid_t sid = H5I_INVALID_HID; - jsize n; - htri_t vl_data_class; - herr_t status = FAIL; + jboolean readBufIsCopy; + jbyte *readBuf = NULL; + hsize_t dims[H5S_MAX_RANK]; + hid_t sid = H5I_INVALID_HID; + size_t typeSize; + H5T_class_t type_class; + jsize vl_array_len = 0; // Only used by vl_data_class types + htri_t vl_data_class; + herr_t status = FAIL; UNUSED(clss); @@ -173,40 +175,58 @@ Java_hdf_hdf5lib_H5_H5Aread(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_t if (vl_data_class) { /* Get size of data array */ - if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Aread: readBuf length < 0"); - } - dims[0] = (hsize_t)n; - if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + if (!(typeSize = H5Tget_size(mem_type_id))) H5_LIBRARY_ERROR(ENVONLY); - } - if (isCriticalPinning) { - PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, - "H5Aread: read buffer not critically pinned"); + if (NULL == (readBuf = HDcalloc((size_t)vl_array_len, typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Aread: failed to allocate raw VL read buffer"); } else { - PIN_BYTE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Aread: read buffer not pinned"); + if (isCriticalPinning) { + PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, + "H5Aread: read buffer not critically pinned"); + } + else { + PIN_BYTE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Aread: read buffer not pinned"); + } } if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, (void *)readBuf)) < 0) H5_LIBRARY_ERROR(ENVONLY); + if (vl_data_class) { + 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) { + dims[0] = (hsize_t)vl_array_len; + if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + H5Dvlen_reclaim(attr_id, sid, H5P_DEFAULT, readBuf); + if (sid >= 0) H5Sclose(sid); } - if (isCriticalPinning) { - UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (vl_data_class) { + HDfree(readBuf); } else { - UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (isCriticalPinning) { + UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + } + else { + UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + } } } @@ -222,38 +242,52 @@ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5Awrite(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jbyteArray buf, jboolean isCriticalPinning) { - jboolean writeBufIsCopy; - jbyte *writeBuf = NULL; - hsize_t dims[H5S_MAX_RANK]; - hid_t sid = H5I_INVALID_HID; - jsize n; - htri_t vl_data_class; - herr_t status = FAIL; + jboolean writeBufIsCopy; + jbyte *writeBuf = NULL; + hsize_t dims[H5S_MAX_RANK]; + hid_t sid = H5I_INVALID_HID; + size_t typeSize; + H5T_class_t type_class; + jsize vl_array_len = 0; // 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, "H5Awrite: write buffer is NULL"); - /* Get size of data array */ - if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Aread: readBuf length < 0"); - } - - dims[0] = (hsize_t)n; - if ((sid = H5Screate_simple(1, dims, NULL)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if (isCriticalPinning) { - PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, - "H5Awrite: write buffer not critically pinned"); + if (vl_data_class) { + /* 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, "H5Awrite: write buffer length < 0"); + } + + 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, "H5Awrite: failed to allocate raw VL write buffer"); } else { - PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Awrite: write buffer not pinned"); + if (isCriticalPinning) { + PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, + "H5Awrite: write buffer not critically pinned"); + } + else { + PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Awrite: write buffer not pinned"); + } + } + + if (vl_data_class) { + 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 = H5Awrite((hid_t)attr_id, (hid_t)mem_type_id, writeBuf)) < 0) @@ -261,14 +295,24 @@ Java_hdf_hdf5lib_H5_H5Awrite(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_ done: if (writeBuf) { - if ((status >= 0) && vl_data_class) + if ((status >= 0) && vl_data_class) { + dims[0] = (hsize_t)vl_array_len; + if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + H5Dvlen_reclaim(attr_id, sid, H5P_DEFAULT, writeBuf); + } - if (isCriticalPinning) { - UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + if (vl_data_class) { + HDfree(writeBuf); } else { - UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + if (isCriticalPinning) { + UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + } + else { + UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + } } } @@ -1063,13 +1107,15 @@ done: JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5AreadVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf) { - H5T_class_t type_class; + jbyte *readBuf = NULL; hsize_t dims[H5S_MAX_RANK]; hid_t sid = H5I_INVALID_HID; - jsize n = 0; + size_t typeSize; + H5T_class_t type_class; + jsize vl_array_len; htri_t vl_data_class; - herr_t status = FAIL; - jbyteArray *readBuf = NULL; + herr_t status = FAIL; + htri_t is_variable = 0; UNUSED(clss); @@ -1078,228 +1124,42 @@ Java_hdf_hdf5lib_H5_H5AreadVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem 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) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Aread: readBuf length < 0"); + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) + H5_LIBRARY_ERROR(ENVONLY); - if (vl_data_class) { - /* Get size of data array */ - if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5AreadVL: readBuf length < 0"); - } + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); - dims[0] = (hsize_t)n; - if ((sid = H5Screate_simple(1, dims, NULL)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - } + if (NULL == (readBuf = HDcalloc((size_t)vl_array_len, typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5Aread: failed to allocate raw VL read buffer"); + if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, (void *)readBuf)) < 0) + H5_LIBRARY_ERROR(ENVONLY); if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if (type_class == H5T_VLEN) { - size_t typeSize; - hid_t memb = H5I_INVALID_HID; - H5T_class_t vlClass; - size_t vlSize; - void *rawBuf = NULL; - jobjectArray jList = NULL; - - size_t i, j, x; - - if (!(typeSize = H5Tget_size(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - - if (!(memb = H5Tget_super(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - if ((vlClass = H5Tget_class((hid_t)memb)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - if (!(vlSize = H5Tget_size(memb))) - H5_LIBRARY_ERROR(ENVONLY); - if (NULL == (rawBuf = HDcalloc((size_t)n, typeSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5AreadVL: failed to allocate raw VL read buffer"); - - if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, (void *)rawBuf)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - - /* Cache class types */ - /* jclass cBool = ENVPTR->FindClass(ENVONLY, "java/lang/Boolean"); */ - jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); - jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); - jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); - jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); - jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); - jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); - - /* - jmethodID boolValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cBool, "valueOf", "(Z)Ljava/lang/Boolean;"); - */ - jmethodID byteValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cByte, "valueOf", "(B)Ljava/lang/Byte;"); - jmethodID shortValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cShort, "valueOf", "(S)Ljava/lang/Short;"); - jmethodID intValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cInt, "valueOf", "(I)Ljava/lang/Integer;"); - jmethodID longValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cLong, "valueOf", "(J)Ljava/lang/Long;"); - jmethodID floatValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cFloat, "valueOf", "(F)Ljava/lang/Float;"); - jmethodID doubleValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cDouble, "valueOf", "(D)Ljava/lang/Double;"); - - // retrieve the java.util.List interface class - jclass cList = ENVPTR->FindClass(ENVONLY, "java/util/List"); - jmethodID addMethod = ENVPTR->GetMethodID(ENVONLY, cList, "add", "(Ljava/lang/Object;)Z"); - - /* Convert each element to a list */ - for (i = 0; i < (size_t)n; i++) { - hvl_t vl_elem; - - // The list we're going to return: - if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, (jsize)i))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - /* Get the number of sequence elements */ - HDmemcpy(&vl_elem, (char *)rawBuf + i * typeSize, sizeof(hvl_t)); - - jsize nelmts = (jsize)vl_elem.len; - if (vl_elem.len != (size_t)nelmts) - H5_JNI_FATAL_ERROR(ENVONLY, "H5AreadVL: overflow of number of VL elements"); - if (nelmts < 0) - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5AreadVL: number of VL elements < 0"); - - jobject jobj = NULL; - for (j = 0; j < (size_t)nelmts; j++) { - switch (vlClass) { - /* case H5T_BOOL: { - jboolean boolValue; - for (x = 0; x < vlSize; x++) { - ((char *)&boolValue)[x] = ((char *)vl_elem.p)[j*vlSize+x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cBool, boolValueMid, boolValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } */ - case H5T_INTEGER: { - switch (vlSize) { - case sizeof(jbyte): { - jbyte byteValue; - for (x = 0; x < vlSize; x++) { - ((char *)&byteValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = - ENVPTR->CallStaticObjectMethod(ENVONLY, cByte, byteValueMid, byteValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jshort): { - jshort shortValue; - for (x = 0; x < vlSize; x++) { - ((char *)&shortValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cShort, shortValueMid, - shortValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jint): { - jint intValue; - for (x = 0; x < vlSize; x++) { - ((char *)&intValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cInt, intValueMid, intValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jlong): { - jlong longValue; - for (x = 0; x < vlSize; x++) { - ((char *)&longValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = - ENVPTR->CallStaticObjectMethod(ENVONLY, cLong, longValueMid, longValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - } - break; - } - case H5T_FLOAT: { - switch (vlSize) { - case sizeof(jfloat): { - jfloat floatValue; - for (x = 0; x < vlSize; x++) { - ((char *)&floatValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cFloat, floatValueMid, - (double)floatValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jdouble): { - jdouble doubleValue; - for (x = 0; x < vlSize; x++) { - ((char *)&doubleValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cDouble, doubleValueMid, - doubleValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - } - break; - } - case H5T_REFERENCE: { - jboolean bb; - jbyte *barray = NULL; - - jsize byteArraySize = (jsize)vlSize; - if (vlSize != (size_t)byteArraySize) - H5_JNI_FATAL_ERROR(ENVONLY, "H5AreadVL: overflow of byteArraySize"); - - if (NULL == (jobj = ENVPTR->NewByteArray(ENVONLY, byteArraySize))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - PIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, &bb, - "readVL reference: byte array not pinned"); - - for (x = 0; x < vlSize; x++) { - barray[x] = ((jbyte *)vl_elem.p)[j * vlSize + x]; - } - if (barray) - UNPIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, jobj ? 0 : JNI_ABORT); - break; - } - default: - H5_UNIMPLEMENTED(ENVONLY, "H5AreadVL: invalid class type"); - break; - } - - // Add it to the list - ENVPTR->CallBooleanMethod(ENVONLY, jList, addMethod, jobj); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - } - } /* end for */ - - if (rawBuf) - HDfree(rawBuf); - } - else { - if ((status = H5Aread((hid_t)attr_id, (hid_t)mem_type_id, (void *)readBuf)) < 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) { + dims[0] = (hsize_t)vl_array_len; + if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + H5Dvlen_reclaim(attr_id, sid, H5P_DEFAULT, readBuf); + if (sid >= 0) H5Sclose(sid); } - - UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (is_variable) { + for (size_t i = 0; i < (size_t)vl_array_len; i++) + HDfree(((char **)readBuf)[i]); + } + HDfree(readBuf); } return (jint)status; @@ -1313,205 +1173,61 @@ done: JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5AwriteVL(JNIEnv *env, jclass clss, jlong attr_id, jlong mem_type_id, jobjectArray buf) { - H5T_class_t type_class; + jbyte *writeBuf = NULL; hsize_t dims[H5S_MAX_RANK]; hid_t sid = H5I_INVALID_HID; - jsize n; + size_t typeSize; + H5T_class_t type_class; + jsize vl_array_len; htri_t vl_data_class; - herr_t status = FAIL; - jboolean writeBufIsCopy; - jbyteArray writeBuf = NULL; + herr_t status = FAIL; + htri_t is_variable = 0; UNUSED(clss); if (NULL == buf) H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5AwriteVL: 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 ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { + if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5AwriteVL: readBuf length < 0"); + H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Awrite: write buffer length < 0"); } - - dims[0] = (hsize_t)n; - if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) + 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, "H5Awrite: failed to allocate raw VL write buffer"); + if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if (type_class == H5T_VLEN) { - size_t typeSize; - hid_t memb = H5I_INVALID_HID; - H5T_class_t vlClass; - size_t vlSize; - void *rawBuf = NULL; - jobjectArray jList = NULL; - - size_t i, j, x; - if (!(typeSize = H5Tget_size(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - - if (!(memb = H5Tget_super(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - if ((vlClass = H5Tget_class((hid_t)memb)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - if (!(vlSize = H5Tget_size(memb))) - H5_LIBRARY_ERROR(ENVONLY); - - if (NULL == (rawBuf = HDcalloc((size_t)n, typeSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5AwriteVL: failed to allocate raw VL write buffer"); - - /* Cache class types */ - /* jclass cBool = ENVPTR->FindClass(ENVONLY, "java/lang/Boolean"); */ - jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); - jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); - jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); - jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); - jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); - jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); - - /* jmethodID boolValueMid = ENVPTR->GetMethodID(ENVONLY, cBool, "booleanValue", "()Z"); */ - jmethodID byteValueMid = ENVPTR->GetMethodID(ENVONLY, cByte, "byteValue", "()B"); - jmethodID shortValueMid = ENVPTR->GetMethodID(ENVONLY, cShort, "shortValue", "()S"); - jmethodID intValueMid = ENVPTR->GetMethodID(ENVONLY, cInt, "intValue", "()I"); - jmethodID longValueMid = ENVPTR->GetMethodID(ENVONLY, cLong, "longValue", "()J"); - jmethodID floatValueMid = ENVPTR->GetMethodID(ENVONLY, cFloat, "floatValue", "()F"); - jmethodID doubleValueMid = ENVPTR->GetMethodID(ENVONLY, cDouble, "doubleValue", "()D"); - - /* Convert each list to a vlen element */ - for (i = 0; i < (size_t)n; i++) { - hvl_t vl_elem; - - if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, (jsize)i))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - // retrieve the java.util.List interface class - jclass cList = ENVPTR->FindClass(ENVONLY, "java/util/List"); - - // retrieve the toArray method and invoke it - jmethodID mToArray = ENVPTR->GetMethodID(ENVONLY, cList, "toArray", "()[Ljava/lang/Object;"); - if (mToArray == NULL) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, jList, mToArray); - jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); - - if (jnelmts < 0) - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5AwriteVL: number of VL elements < 0"); - - HDmemcpy(&vl_elem, (char *)rawBuf + i * typeSize, sizeof(hvl_t)); - vl_elem.len = (size_t)jnelmts; - - if (NULL == (vl_elem.p = HDmalloc((size_t)jnelmts * vlSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5AwriteVL: failed to allocate vlen ptr buffer"); - - jobject jobj = NULL; - for (j = 0; j < (size_t)jnelmts; j++) { - if (NULL == (jobj = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)array, (jsize)j))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - switch (vlClass) { - /* case H5T_BOOL: { - jboolean boolValue = ENVPTR->CallBooleanMethod(ENVONLY, jobj, boolValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&boolValue)[x]; - } - break; - } */ - case H5T_INTEGER: { - switch (vlSize) { - case sizeof(jbyte): { - jbyte byteValue = ENVPTR->CallByteMethod(ENVONLY, jobj, byteValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&byteValue)[x]; - } - break; - } - case sizeof(jshort): { - jshort shortValue = ENVPTR->CallShortMethod(ENVONLY, jobj, shortValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&shortValue)[x]; - } - break; - } - case sizeof(jint): { - jint intValue = ENVPTR->CallIntMethod(ENVONLY, jobj, intValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&intValue)[x]; - } - break; - } - case sizeof(jlong): { - jlong longValue = ENVPTR->CallLongMethod(ENVONLY, jobj, longValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&longValue)[x]; - } - break; - } - } - break; - } - case H5T_FLOAT: { - switch (vlSize) { - case sizeof(jfloat): { - jfloat floatValue = ENVPTR->CallFloatMethod(ENVONLY, jobj, floatValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&floatValue)[x]; - } - break; - } - case sizeof(jdouble): { - jdouble doubleValue = ENVPTR->CallDoubleMethod(ENVONLY, jobj, doubleValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&doubleValue)[x]; - } - break; - } - } - break; - } - case H5T_REFERENCE: { - jbyte *barray = (jbyte *)ENVPTR->GetByteArrayElements(ENVONLY, jobj, 0); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)barray)[x]; - } - ENVPTR->ReleaseByteArrayElements(ENVONLY, jobj, barray, 0); - break; - } - default: - H5_UNIMPLEMENTED(ENVONLY, "H5AwriteVL: invalid class type"); - break; - } - ENVPTR->DeleteLocalRef(ENVONLY, jobj); - } - - HDmemcpy((char *)rawBuf + i * typeSize, &vl_elem, sizeof(hvl_t)); - - ENVPTR->DeleteLocalRef(ENVONLY, jList); - } /* end for (i = 0; i < n; i++) */ - - if ((status = H5Awrite((hid_t)attr_id, (hid_t)mem_type_id, rawBuf)) < 0) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + translate_wbuf(ENVONLY, buf, mem_type_id, type_class, vl_array_len, writeBuf); - if (rawBuf) - HDfree(rawBuf); - } - else { - PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5AwriteVL: write buffer not pinned"); - if ((status = H5Awrite((hid_t)attr_id, (hid_t)mem_type_id, writeBuf)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - } + if ((status = H5Awrite((hid_t)attr_id, (hid_t)mem_type_id, writeBuf)) < 0) + H5_LIBRARY_ERROR(ENVONLY); done: if (writeBuf) { - if ((status >= 0) && vl_data_class) + if ((status >= 0) && vl_data_class) { + dims[0] = (hsize_t)vl_array_len; + if ((sid = H5Screate_simple(1, dims, NULL)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + H5Dvlen_reclaim(attr_id, sid, H5P_DEFAULT, writeBuf); + } + if (is_variable) { + for (size_t i = 0; i < (size_t)vl_array_len; i++) + HDfree(((char **)writeBuf)[i]); + } - if (type_class != H5T_VLEN) - UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + HDfree(writeBuf); } return (jint)status; @@ -1914,21 +1630,24 @@ H5AwriteVL_asstr(JNIEnv *env, hid_t aid, hid_t tid, jobjectArray buf) continue; } - /* - * length = ENVPTR->GetStringUTFLength(ENVONLY, jstr); - * CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - */ - PIN_JAVA_STRING(ENVONLY, jstr, utf8, NULL, "H5AwriteVL_asstr: failed to pin string buffer"); /* - * TODO: If the string isn't a copy, we should probably make - * one before destroying it with h5str_convert. + * Make a copy of the string since h5str_convert uses strtok. */ + char *utf8_copy = NULL; - if (!h5str_convert(ENVONLY, (char **)&utf8, aid, tid, &(((char *)writeBuf)[i * typeSize]), 0)) + jsize length = ENVPTR->GetStringUTFLength(ENVONLY, jstr); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + if (NULL == (utf8_copy = HDstrndup(utf8, (size_t)length))) + H5_LIBRARY_ERROR(ENVONLY); + + if (!h5str_convert(ENVONLY, &utf8_copy, aid, tid, &(((char *)writeBuf)[i * typeSize]), 0)) CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + free(utf8_copy); + UNPIN_JAVA_STRING(ENVONLY, jstr, utf8); utf8 = NULL; diff --git a/java/src/jni/h5dImp.c b/java/src/jni/h5dImp.c index f0ba4ac..3dc4b98 100644 --- a/java/src/jni/h5dImp.c +++ b/java/src/jni/h5dImp.c @@ -181,47 +181,71 @@ Java_hdf_hdf5lib_H5_H5Dread(JNIEnv *env, jclass clss, jlong dataset_id, jlong me jlong file_space_id, jlong xfer_plist_id, jbyteArray buf, jboolean isCriticalPinning) { - jboolean readBufIsCopy; - jbyte *readBuf = NULL; - htri_t vl_data_class; - herr_t status = FAIL; + jboolean readBufIsCopy; + jbyte *readBuf = 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, "H5Dread: read buffer is NULL"); - /* Get size of data array */ - if (ENVPTR->GetArrayLength(ENVONLY, buf) < 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread: readBuf length < 0"); - } - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if (isCriticalPinning) { - PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, - "H5Dread: read buffer not critically pinned"); + if (vl_data_class) { + /* 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"); } else { - PIN_BYTE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread: read buffer not pinned"); + if (isCriticalPinning) { + PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, readBuf, &readBufIsCopy, + "H5Dread: read buffer not critically pinned"); + } + else { + PIN_BYTE_ARRAY(ENVONLY, buf, readBuf, &readBufIsCopy, "H5Dread: read buffer not pinned"); + } } 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, readBuf)) < 0) + (hid_t)xfer_plist_id, (void *)readBuf)) < 0) H5_LIBRARY_ERROR(ENVONLY); + if (vl_data_class) { + 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) H5Dvlen_reclaim(dataset_id, mem_space_id, H5P_DEFAULT, readBuf); - if (isCriticalPinning) { - UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (vl_data_class) { + HDfree(readBuf); } else { - UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (isCriticalPinning) { + UNPIN_ARRAY_CRITICAL(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + } + else { + UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + } } } @@ -238,31 +262,50 @@ Java_hdf_hdf5lib_H5_H5Dwrite(JNIEnv *env, jclass clss, jlong dataset_id, jlong m jlong mem_space_id, jlong file_space_id, jlong xfer_plist_id, jbyteArray buf, jboolean isCriticalPinning) { - jboolean writeBufIsCopy; - jbyte *writeBuf = NULL; - htri_t vl_data_class; - 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"); - /* Get size of data array */ - if (ENVPTR->GetArrayLength(ENVONLY, buf) < 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5Dread: readBuf length < 0"); - } - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if (isCriticalPinning) { - PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, - "H5Dwrite: write buffer not critically pinned"); + if (vl_data_class) { + /* 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"); + } + + 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"); } else { - PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite: write buffer not pinned"); + if (isCriticalPinning) { + PIN_BYTE_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, &writeBufIsCopy, + "H5Dwrite: write buffer not critically pinned"); + } + else { + PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5Dwrite: write buffer not pinned"); + } + } + + if (vl_data_class) { + 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, @@ -274,11 +317,16 @@ done: if ((status >= 0) && vl_data_class) H5Dvlen_reclaim(dataset_id, mem_space_id, H5P_DEFAULT, writeBuf); - if (isCriticalPinning) { - UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + if (vl_data_class) { + HDfree(writeBuf); } else { - UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + if (isCriticalPinning) { + UNPIN_ARRAY_CRITICAL(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + } + else { + UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + } } } @@ -1074,234 +1122,52 @@ 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) { + jbyte *readBuf = NULL; + size_t typeSize; H5T_class_t type_class; - jsize n; + jsize vl_array_len; htri_t vl_data_class; - herr_t status = FAIL; - jbyteArray *readBuf = NULL; + herr_t status = FAIL; + htri_t is_variable = 0; UNUSED(clss); if (NULL == buf) H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5DreadVL: 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 ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { + if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DreadVL: readBuf length < 0"); } - - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0) + if (!(typeSize = H5Tget_size(mem_type_id))) H5_LIBRARY_ERROR(ENVONLY); - if (type_class == H5T_VLEN) { - size_t typeSize; - hid_t memb = H5I_INVALID_HID; - H5T_class_t vlClass; - size_t vlSize; - void *rawBuf = NULL; - jobjectArray jList = NULL; - - size_t i, j, x; - - if (!(typeSize = H5Tget_size(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - - if (!(memb = H5Tget_super(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - if ((vlClass = H5Tget_class((hid_t)memb)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - if (!(vlSize = H5Tget_size(memb))) - H5_LIBRARY_ERROR(ENVONLY); - if (NULL == (rawBuf = HDcalloc((size_t)n, typeSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DreadVL: 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 *)rawBuf)) < 0) - H5_LIBRARY_ERROR(ENVONLY); + if (NULL == (readBuf = HDcalloc((size_t)vl_array_len, typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DreadVL: failed to allocate raw VL read buffer"); - /* Cache class types */ - /* jclass cBool = ENVPTR->FindClass(ENVONLY, "java/lang/Boolean"); */ - jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); - jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); - jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); - jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); - jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); - jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); - - /* - jmethodID boolValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cBool, "valueOf", "(Z)Ljava/lang/Boolean;"); - */ - jmethodID byteValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cByte, "valueOf", "(B)Ljava/lang/Byte;"); - jmethodID shortValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cShort, "valueOf", "(S)Ljava/lang/Short;"); - jmethodID intValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cInt, "valueOf", "(I)Ljava/lang/Integer;"); - jmethodID longValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cLong, "valueOf", "(J)Ljava/lang/Long;"); - jmethodID floatValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cFloat, "valueOf", "(F)Ljava/lang/Float;"); - jmethodID doubleValueMid = - ENVPTR->GetStaticMethodID(ENVONLY, cDouble, "valueOf", "(D)Ljava/lang/Double;"); - - // retrieve the java.util.List interface class - jclass cList = ENVPTR->FindClass(ENVONLY, "java/util/List"); - jmethodID addMethod = ENVPTR->GetMethodID(ENVONLY, cList, "add", "(Ljava/lang/Object;)Z"); - - /* Convert each element to a list */ - for (i = 0; i < (size_t)n; i++) { - hvl_t vl_elem; - - // The list we're going to return: - if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, (jsize)i))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - /* Get the number of sequence elements */ - HDmemcpy(&vl_elem, (char *)rawBuf + i * typeSize, sizeof(hvl_t)); - - jsize nelmts = (jsize)vl_elem.len; - if (vl_elem.len != (size_t)nelmts) - H5_JNI_FATAL_ERROR(ENVONLY, "H5DreadVL: overflow of number of VL elements"); - if (nelmts < 0) - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DreadVL: number of VL elements < 0"); - - jobject jobj = NULL; - for (j = 0; j < (size_t)nelmts; j++) { - switch (vlClass) { - /*case H5T_BOOL: { - jboolean boolValue; - for (x = 0; x < vlSize; x++) { - ((char *)&boolValue)[x] = ((char *)vl_elem.p)[j*vlSize+x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cBool, boolValueMid, boolValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } */ - case H5T_INTEGER: { - switch (vlSize) { - case sizeof(jbyte): { - jbyte byteValue; - for (x = 0; x < vlSize; x++) { - ((char *)&byteValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = - ENVPTR->CallStaticObjectMethod(ENVONLY, cByte, byteValueMid, byteValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jshort): { - jshort shortValue; - for (x = 0; x < vlSize; x++) { - ((char *)&shortValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cShort, shortValueMid, - shortValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jint): { - jint intValue; - for (x = 0; x < vlSize; x++) { - ((char *)&intValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cInt, intValueMid, intValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jlong): { - jlong longValue; - for (x = 0; x < vlSize; x++) { - ((char *)&longValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = - ENVPTR->CallStaticObjectMethod(ENVONLY, cLong, longValueMid, longValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - } - break; - } - case H5T_FLOAT: { - switch (vlSize) { - case sizeof(jfloat): { - jfloat floatValue; - for (x = 0; x < vlSize; x++) { - ((char *)&floatValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cFloat, floatValueMid, - (double)floatValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - case sizeof(jdouble): { - jdouble doubleValue; - for (x = 0; x < vlSize; x++) { - ((char *)&doubleValue)[x] = ((char *)vl_elem.p)[j * vlSize + x]; - } - - jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cDouble, doubleValueMid, - doubleValue); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - break; - } - } - break; - } - case H5T_REFERENCE: { - jboolean bb; - jbyte *barray = NULL; - - jsize byteArraySize = (jsize)vlSize; - if (vlSize != (size_t)byteArraySize) - H5_JNI_FATAL_ERROR(ENVONLY, "H5DreadVL: overflow of byteArraySize"); - - if (NULL == (jobj = ENVPTR->NewByteArray(ENVONLY, byteArraySize))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - PIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, &bb, - "readVL reference: byte array not pinned"); - - for (x = 0; x < vlSize; x++) { - barray[x] = ((jbyte *)vl_elem.p)[j * vlSize + x]; - } - if (barray) - UNPIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, jobj ? 0 : JNI_ABORT); - break; - } - default: - H5_UNIMPLEMENTED(ENVONLY, "H5DreadVL: invalid class type"); - break; - } - - // Add it to the list - ENVPTR->CallBooleanMethod(ENVONLY, jList, addMethod, jobj); - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - } - } /* end for */ + 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); - if (rawBuf) - HDfree(rawBuf); - } - else { - 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); - } + translate_rbuf(env, buf, mem_type_id, type_class, vl_array_len, readBuf); done: if (readBuf) { if ((status >= 0) && vl_data_class) H5Dvlen_reclaim(dataset_id, mem_space_id, H5P_DEFAULT, readBuf); - - UNPIN_BYTE_ARRAY(ENVONLY, buf, readBuf, (status < 0) ? JNI_ABORT : 0); + if (is_variable) { + for (size_t i = 0; i < (size_t)vl_array_len; i++) + HDfree(((char **)readBuf)[i]); + } + HDfree(readBuf); } return (jint)status; @@ -1316,201 +1182,55 @@ 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) { + jbyte *writeBuf = NULL; + size_t typeSize; H5T_class_t type_class; - jsize n; + jsize vl_array_len; // Only used by vl_data_class types htri_t vl_data_class; - herr_t status = FAIL; - jboolean writeBufIsCopy; - jbyteArray writeBuf = NULL; + herr_t status = FAIL; + htri_t is_variable = 0; UNUSED(clss); if (NULL == buf) H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL: 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 ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { + if ((vl_array_len = ENVPTR->GetArrayLength(ENVONLY, buf)) < 0) { CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL: readBuf length < 0"); + H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL: write buffer length < 0"); } - - if ((vl_data_class = h5str_detect_vlen(mem_type_id)) < 0) + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) H5_LIBRARY_ERROR(ENVONLY); - if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0) + if (!(typeSize = H5Tget_size(mem_type_id))) H5_LIBRARY_ERROR(ENVONLY); - if (type_class == H5T_VLEN) { - size_t typeSize; - hid_t memb = H5I_INVALID_HID; - H5T_class_t vlClass; - size_t vlSize; - void *rawBuf = NULL; - jobjectArray jList = NULL; - - size_t i, j, x; - - if (!(typeSize = H5Tget_size(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - - if (!(memb = H5Tget_super(mem_type_id))) - H5_LIBRARY_ERROR(ENVONLY); - if ((vlClass = H5Tget_class((hid_t)memb)) < 0) - H5_LIBRARY_ERROR(ENVONLY); - if (!(vlSize = H5Tget_size(memb))) - H5_LIBRARY_ERROR(ENVONLY); - - if (NULL == (rawBuf = HDcalloc((size_t)n, typeSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DwriteVL: failed to allocate raw VL write buffer"); - - /* Cache class types */ - /* jclass cBool = ENVPTR->FindClass(ENVONLY, "java/lang/Boolean"); */ - jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); - jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); - jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); - jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); - jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); - jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); - - /* jmethodID boolValueMid = ENVPTR->GetMethodID(ENVONLY, cBool, "booleanValue", "()Z"); */ - jmethodID byteValueMid = ENVPTR->GetMethodID(ENVONLY, cByte, "byteValue", "()B"); - jmethodID shortValueMid = ENVPTR->GetMethodID(ENVONLY, cShort, "shortValue", "()S"); - jmethodID intValueMid = ENVPTR->GetMethodID(ENVONLY, cInt, "intValue", "()I"); - jmethodID longValueMid = ENVPTR->GetMethodID(ENVONLY, cLong, "longValue", "()J"); - jmethodID floatValueMid = ENVPTR->GetMethodID(ENVONLY, cFloat, "floatValue", "()F"); - jmethodID doubleValueMid = ENVPTR->GetMethodID(ENVONLY, cDouble, "doubleValue", "()D"); - - /* Convert each list to a vlen element */ - for (i = 0; i < (size_t)n; i++) { - hvl_t vl_elem; - - if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)buf, (jsize)i))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - // retrieve the java.util.List interface class - jclass cList = ENVPTR->FindClass(ENVONLY, "java/util/List"); - - // retrieve the toArray method and invoke it - jmethodID mToArray = ENVPTR->GetMethodID(ENVONLY, cList, "toArray", "()[Ljava/lang/Object;"); - if (mToArray == NULL) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, jList, mToArray); - jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); - - if (jnelmts < 0) - H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5DwriteVL: number of VL elements < 0"); - - HDmemcpy(&vl_elem, (char *)rawBuf + i * typeSize, sizeof(hvl_t)); - vl_elem.len = (size_t)jnelmts; - - if (NULL == (vl_elem.p = HDmalloc((size_t)jnelmts * vlSize))) - H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DwriteVL: failed to allocate vlen ptr buffer"); - - jobject jobj = NULL; - for (j = 0; j < (size_t)jnelmts; j++) { - if (NULL == (jobj = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)array, (jsize)j))) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - - switch (vlClass) { - /* case H5T_BOOL: { - jboolean boolValue = ENVPTR->CallBooleanMethod(ENVONLY, jobj, boolValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&boolValue)[x]; - } - break; - } */ - case H5T_INTEGER: { - switch (vlSize) { - case sizeof(jbyte): { - jbyte byteValue = ENVPTR->CallByteMethod(ENVONLY, jobj, byteValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&byteValue)[x]; - } - break; - } - case sizeof(jshort): { - jshort shortValue = ENVPTR->CallShortMethod(ENVONLY, jobj, shortValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&shortValue)[x]; - } - break; - } - case sizeof(jint): { - jint intValue = ENVPTR->CallIntMethod(ENVONLY, jobj, intValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&intValue)[x]; - } - break; - } - case sizeof(jlong): { - jlong longValue = ENVPTR->CallLongMethod(ENVONLY, jobj, longValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&longValue)[x]; - } - break; - } - } - break; - } - case H5T_FLOAT: { - switch (vlSize) { - case sizeof(jfloat): { - jfloat floatValue = ENVPTR->CallFloatMethod(ENVONLY, jobj, floatValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&floatValue)[x]; - } - break; - } - case sizeof(jdouble): { - jdouble doubleValue = ENVPTR->CallDoubleMethod(ENVONLY, jobj, doubleValueMid); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)&doubleValue)[x]; - } - break; - } - } - break; - } - case H5T_REFERENCE: { - jbyte *barray = (jbyte *)ENVPTR->GetByteArrayElements(ENVONLY, jobj, 0); - for (x = 0; x < vlSize; x++) { - ((char *)vl_elem.p)[j * vlSize + x] = ((char *)barray)[x]; - } - ENVPTR->ReleaseByteArrayElements(ENVONLY, jobj, barray, 0); - break; - } - default: - H5_UNIMPLEMENTED(ENVONLY, "H5DwriteVL: invalid class type"); - break; - } - ENVPTR->DeleteLocalRef(ENVONLY, jobj); - } - HDmemcpy((char *)rawBuf + i * typeSize, &vl_elem, sizeof(hvl_t)); + if (NULL == (writeBuf = HDcalloc((size_t)vl_array_len, typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5DwriteVL: failed to allocate raw VL write buffer"); - ENVPTR->DeleteLocalRef(ENVONLY, jList); - } /* end for (i = 0; i < n; i++) */ + if ((type_class = H5Tget_class((hid_t)mem_type_id)) < 0) + H5_LIBRARY_ERROR(ENVONLY); - 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, rawBuf)) < 0) - CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + translate_wbuf(ENVONLY, buf, mem_type_id, type_class, vl_array_len, writeBuf); - if (rawBuf) - HDfree(rawBuf); - } - else { - PIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, &writeBufIsCopy, "H5DwriteVL: write buffer not pinned"); - 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); - } + 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) H5Dvlen_reclaim(dataset_id, mem_space_id, H5P_DEFAULT, writeBuf); + if (is_variable) { + for (size_t i = 0; i < (size_t)vl_array_len; i++) + HDfree(((char **)writeBuf)[i]); + } - if (type_class != H5T_VLEN) - UNPIN_BYTE_ARRAY(ENVONLY, buf, writeBuf, (status < 0) ? JNI_ABORT : 0); + HDfree(writeBuf); } return (jint)status; @@ -1946,21 +1666,24 @@ H5DwriteVL_asstr(JNIEnv *env, hid_t did, hid_t tid, hid_t mem_sid, hid_t file_si continue; } - /* - * length = ENVPTR->GetStringUTFLength(ENVONLY, jstr); - * CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); - */ - PIN_JAVA_STRING(ENVONLY, jstr, utf8, NULL, "H5DwriteVL_asstr: failed to pin string buffer"); /* - * TODO: If the string isn't a copy, we should probably make - * one before destroying it with h5str_convert. + * Make a copy of the string since h5str_convert uses strtok. */ + char *utf8_copy = NULL; + + jsize length = ENVPTR->GetStringUTFLength(ENVONLY, jstr); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + if (NULL == (utf8_copy = HDstrndup(utf8, (size_t)length))) + H5_LIBRARY_ERROR(ENVONLY); - if (!h5str_convert(ENVONLY, (char **)&utf8, did, tid, &(((char *)writeBuf)[i * typeSize]), 0)) + if (!h5str_convert(ENVONLY, &utf8_copy, did, tid, &(((char *)writeBuf)[i * typeSize]), 0)) CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + HDfree(utf8_copy); + UNPIN_JAVA_STRING(ENVONLY, jstr, utf8); utf8 = NULL; diff --git a/java/src/jni/h5util.c b/java/src/jni/h5util.c index 6a6dde4..5e32125 100644 --- a/java/src/jni/h5util.c +++ b/java/src/jni/h5util.c @@ -75,6 +75,9 @@ static int render_bin_output_region_data_points(FILE *stream, hid_t region_sp hsize_t *ptdata); static int render_bin_output_region_points(FILE *stream, hid_t region_space, hid_t region_id, hid_t container); +jobject translate_atomic_rbuf(JNIEnv *env, jlong mem_type_id, H5T_class_t type_class, void *raw_buf); +void translate_atomic_wbuf(JNIEnv *env, jobject in_obj, jlong mem_type_id, H5T_class_t type_class, + void *raw_buf); /* Strings for output */ #define H5_TOOLS_GROUP "GROUP" @@ -433,40 +436,43 @@ h5str_convert(JNIEnv *env, char **in_str, hid_t container, hid_t tid, void *out_ break; case H5T_ENUM: { - void *value = NULL; + void *value = NULL; + unsigned char tmp_uchar = 0; + unsigned short tmp_ushort = 0; + unsigned int tmp_uint = 0; +#if H5_SIZEOF_LONG != H5_SIZEOF_INT + unsigned long tmp_ulong = 0; +#endif +#if H5_SIZEOF_LONG_LONG != H5_SIZEOF_LONG + unsigned long long tmp_ullong = 0; +#endif token = HDstrtok(this_str, delimiter); switch (typeSize) { case sizeof(char): { - unsigned char tmp_uchar = 0; - value = &tmp_uchar; + value = &tmp_uchar; break; } case sizeof(short): { - unsigned short tmp_ushort = 0; - value = &tmp_ushort; + value = &tmp_ushort; break; } #if H5_SIZEOF_LONG != H5_SIZEOF_INT case sizeof(long): { - unsigned long tmp_ulong = 0; - value = &tmp_ulong; + value = &tmp_ulong; break; } #endif #if H5_SIZEOF_LONG_LONG != H5_SIZEOF_LONG case sizeof(long long): { - unsigned long long tmp_ullong = 0; - value = &tmp_ullong; + value = &tmp_ullong; break; } #endif - default: { - unsigned int tmp_uint = 0; - value = &tmp_uint; + value = &tmp_uint; break; } } @@ -3010,10 +3016,8 @@ Java_hdf_hdf5lib_H5_H5AreadComplex(JNIEnv *env, jclass clss, jlong attr_id, jlon size = (((H5Tget_size(mem_type_id)) > (H5Tget_size(p_type))) ? (H5Tget_size(mem_type_id)) : (H5Tget_size(p_type))); - if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) <= 0) { - CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + if ((n = ENVPTR->GetArrayLength(ENVONLY, buf)) <= 0) H5_BAD_ARGUMENT_ERROR(ENVONLY, "H5AreadComplex: read buffer length <= 0"); - } if (NULL == (readBuf = (char *)HDmalloc((size_t)n * size))) H5_OUT_OF_MEMORY_ERROR(ENVONLY, "H5AreadComplex: failed to allocate read buffer"); @@ -3646,7 +3650,7 @@ done: */ JNIEXPORT void JNICALL Java_hdf_hdf5lib_H5_H5export_1attribute(JNIEnv *env, jclass clss, jstring file_export_name, jlong dset_id, - jstring object_path, jint binary_order) + jstring attribute_name, jint binary_order) { const char *file_export = NULL; const char *object_name = NULL; @@ -3660,10 +3664,11 @@ Java_hdf_hdf5lib_H5_H5export_1attribute(JNIEnv *env, jclass clss, jstring file_e if (NULL == file_export_name) H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5export_dataset: file_export_name is NULL"); - if (NULL == object_path) + if (NULL == attribute_name) H5_NULL_ARGUMENT_ERROR(ENVONLY, "H5export_dataset: object_path is NULL"); - PIN_JAVA_STRING(ENVONLY, object_path, object_name, &isCopy, "H5export_dataset: object_path not pinned"); + PIN_JAVA_STRING(ENVONLY, attribute_name, object_name, &isCopy, + "H5export_dataset: object_path not pinned"); if ((attr_id = H5Aopen(dset_id, object_name, H5P_DEFAULT)) < 0) H5_LIBRARY_ERROR(ENVONLY); @@ -3688,11 +3693,859 @@ done: if (file_export) UNPIN_JAVA_STRING(ENVONLY, file_export_name, file_export); if (object_name) - UNPIN_JAVA_STRING(ENVONLY, object_path, object_name); + UNPIN_JAVA_STRING(ENVONLY, attribute_name, object_name); if (attr_id >= 0) H5Aclose(attr_id); } /* end Java_hdf_hdf5lib_H5_H5export_1attribute */ +jobject +translate_atomic_rbuf(JNIEnv *env, jlong mem_type_id, H5T_class_t type_class, void *raw_buf) +{ + jobject jobj = NULL; + hid_t memb = H5I_INVALID_HID; + jobjectArray jList = NULL; + H5T_class_t vlClass; + size_t vlSize; + size_t i; + size_t typeSize; + // raw_buf is normally bytes except when used for variable length strings + char *char_buf = (char *)raw_buf; + + /* retrieve the java.util.ArrayList interface class */ + jclass arrCList = ENVPTR->FindClass(ENVONLY, "java/util/ArrayList"); + jmethodID arrListMethod = ENVPTR->GetMethodID(ENVONLY, arrCList, "<init>", "(I)V"); + + /* Cache class types */ + /* jclass cBool = ENVPTR->FindClass(ENVONLY, "java/lang/Boolean"); */ + jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); + jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); + jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); + jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); + jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); + jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); + /*jobjectArray + jmethodID boolValueMid = + ENVPTR->GetStaticMethodID(ENVONLY, cBool, "valueOf", "(Z)Ljava/lang/Boolean;"); + */ + jmethodID byteValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cByte, "valueOf", "(B)Ljava/lang/Byte;"); + jmethodID shortValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cShort, "valueOf", "(S)Ljava/lang/Short;"); + jmethodID intValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cInt, "valueOf", "(I)Ljava/lang/Integer;"); + jmethodID longValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cLong, "valueOf", "(J)Ljava/lang/Long;"); + jmethodID floatValueMid = ENVPTR->GetStaticMethodID(ENVONLY, cFloat, "valueOf", "(F)Ljava/lang/Float;"); + jmethodID doubleValueMid = + ENVPTR->GetStaticMethodID(ENVONLY, cDouble, "valueOf", "(D)Ljava/lang/Double;"); + + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + switch (type_class) { + case H5T_VLEN: { + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert element to a list */ + hvl_t vl_elem; + + /* Get the number of sequence elements */ + memcpy(&vl_elem, char_buf, sizeof(hvl_t)); + jsize nelmts = (jsize)vl_elem.len; + if (vl_elem.len != (size_t)nelmts) + H5_JNI_FATAL_ERROR(ENVONLY, "translate_atomic_rbuf: overflow of number of VL elements"); + + if (nelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_atomic_rbuf: number of VL elements < 0"); + + /* The list we're going to return: */ + if (NULL == (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: failed to allocate list read buffer"); + + translate_rbuf(ENVONLY, jList, memb, vlClass, (jsize)nelmts, vl_elem.p); + jobj = jList; + break; + } /* H5T_VLEN */ + case H5T_COMPOUND: { + int nmembs = H5Tget_nmembers(mem_type_id); + + /* The list we're going to return: */ + if (NULL == (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: failed to allocate list read buffer"); + + /* Convert each element to a compound object */ + for (i = 0; i < (size_t)nmembs; i++) { + H5T_class_t memb_vlClass; + size_t memb_vlSize; + size_t memb_offset; + + if ((memb = H5Tget_member_type(mem_type_id, (unsigned int)i)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + memb_offset = H5Tget_member_offset(mem_type_id, (unsigned int)i); + if ((memb_vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(memb_vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + translate_atomic_rbuf(ENVONLY, memb, memb_vlClass, char_buf + i * typeSize + memb_offset); + H5Tclose(memb); + } + jobj = jList; + break; + } /* H5T_COMPOUND */ + case H5T_ARRAY: { + void *objBuf = NULL; + size_t typeCount; + + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + typeCount = typeSize / vlSize; + + if (NULL == (objBuf = HDmalloc(typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: failed to allocate buffer"); + + /* Convert each element */ + /* Get the object element */ + memcpy((char *)objBuf, char_buf, typeSize); + + /* The list we're going to return: */ + if (NULL == (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: failed to allocate list read buffer"); + + translate_rbuf(ENVONLY, jList, memb, vlClass, (jsize)typeCount, objBuf); + jobj = jList; + + if (objBuf) + HDfree(objBuf); + + break; + } /* H5T_ARRAY */ + case H5T_ENUM: + case H5T_BITFIELD: + case H5T_OPAQUE: + case H5T_INTEGER: { + /* Convert each element */ + switch (typeSize) { + case sizeof(jbyte): { + jbyte byteValue; + HDmemcpy(((char *)&byteValue), char_buf, typeSize); + + if (NULL == + (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cByte, byteValueMid, byteValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + case sizeof(jshort): { + jshort shortValue; + HDmemcpy(((char *)&shortValue), char_buf, typeSize); + + if (NULL == + (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cShort, shortValueMid, shortValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + case sizeof(jint): { + jint intValue; + + HDmemcpy(((char *)&intValue), char_buf, typeSize); + + if (NULL == (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cInt, intValueMid, intValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + case sizeof(jlong): { + jlong longValue; + HDmemcpy(((char *)&longValue), char_buf, typeSize); + + if (NULL == + (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cLong, longValueMid, longValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + } + break; + } /* H5T_INTEGER */ + case H5T_FLOAT: { + /* Convert each element to a list */ + switch (typeSize) { + case sizeof(jfloat): { + jfloat floatValue; + HDmemcpy(((char *)&floatValue), char_buf, typeSize); + + if (NULL == (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cFloat, floatValueMid, + (double)floatValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + case sizeof(jdouble): { + jdouble doubleValue; + HDmemcpy(((char *)&doubleValue), char_buf, typeSize); + + if (NULL == (jobj = ENVPTR->CallStaticObjectMethod(ENVONLY, cDouble, doubleValueMid, + doubleValue))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + break; + } + } + break; + } /* H5T_FLOAT */ + case H5T_REFERENCE: { + /* Convert each element to a list */ + jboolean bb; + jbyte *barray = NULL; + + jsize byteArraySize = (jsize)typeSize; + if (typeSize != (size_t)byteArraySize) + H5_JNI_FATAL_ERROR(ENVONLY, "translate_atomic_rbuf: overflow of byteArraySize"); + + if (NULL == (jobj = ENVPTR->NewByteArray(ENVONLY, byteArraySize))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + PIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, &bb, + "translate_atomic_rbuf reference: byte array not pinned"); + + HDmemcpy(barray, ((jbyte *)raw_buf), typeSize); + if (barray) + UNPIN_BYTE_ARRAY(ENVONLY, (jbyteArray)jobj, barray, jobj ? 0 : JNI_ABORT); + + break; + } /* H5T_REFERENCE */ + case H5T_STRING: { + htri_t is_variable = 0; + + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each element */ + if (is_variable) { + char **var_str_buf = (char **)raw_buf; + if (NULL == (jobj = ENVPTR->NewStringUTF(ENVONLY, *var_str_buf))) { + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: out of memory - unable to " + "construct string from UTF characters"); + } + } + else { + if (NULL == (jobj = ENVPTR->NewStringUTF(ENVONLY, char_buf))) { + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_rbuf: out of memory - unable to " + "construct string from UTF characters"); + } + } + + break; + } /* H5T_STRING */ + default: + H5_UNIMPLEMENTED(ENVONLY, "translate_atomic_rbuf: invalid class type"); + break; + } /* switch(type_class) */ + +done: + + return jobj; +} + +void +translate_atomic_wbuf(JNIEnv *env, jobject in_obj, jlong mem_type_id, H5T_class_t type_class, void *raw_buf) +{ + hid_t memb = H5I_INVALID_HID; + H5T_class_t vlClass; + size_t vlSize; + size_t i; + size_t typeSize; + // raw_buf is normally bytes except when used for variable length strings + char *char_buf = (char *)raw_buf; + + /* retrieve the java.util.ArrayList interface class */ + jclass arrCList = ENVPTR->FindClass(ENVONLY, "java/util/ArrayList"); + /* retrieve the toArray method */ + jmethodID mToArray = ENVPTR->GetMethodID(ENVONLY, arrCList, "toArray", "()[Ljava/lang/Object;"); + + /* Cache class types */ + jclass cByte = ENVPTR->FindClass(ENVONLY, "java/lang/Byte"); + jclass cShort = ENVPTR->FindClass(ENVONLY, "java/lang/Short"); + jclass cInt = ENVPTR->FindClass(ENVONLY, "java/lang/Integer"); + jclass cLong = ENVPTR->FindClass(ENVONLY, "java/lang/Long"); + jclass cFloat = ENVPTR->FindClass(ENVONLY, "java/lang/Float"); + jclass cDouble = ENVPTR->FindClass(ENVONLY, "java/lang/Double"); + + jmethodID byteValueMid = ENVPTR->GetMethodID(ENVONLY, cByte, "byteValue", "()B"); + jmethodID shortValueMid = ENVPTR->GetMethodID(ENVONLY, cShort, "shortValue", "()S"); + jmethodID intValueMid = ENVPTR->GetMethodID(ENVONLY, cInt, "intValue", "()I"); + jmethodID longValueMid = ENVPTR->GetMethodID(ENVONLY, cLong, "longValue", "()J"); + jmethodID floatValueMid = ENVPTR->GetMethodID(ENVONLY, cFloat, "floatValue", "()F"); + jmethodID doubleValueMid = ENVPTR->GetMethodID(ENVONLY, cDouble, "doubleValue", "()D"); + + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + switch (type_class) { + case H5T_VLEN: { + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class((hid_t)memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert element to a vlen element */ + hvl_t vl_elem; + + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, in_obj); + + if (jnelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_atomic_wbuf: number of VL elements < 0"); + + vl_elem.len = (size_t)jnelmts; + + if (NULL == (vl_elem.p = HDmalloc((size_t)jnelmts * vlSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_wbuf: failed to allocate vlen ptr buffer"); + + translate_wbuf(ENVONLY, (jobjectArray)in_obj, memb, vlClass, (jsize)jnelmts, vl_elem.p); + + HDmemcpy(char_buf, &vl_elem, sizeof(hvl_t)); + break; + } /* H5T_VLEN */ + case H5T_COMPOUND: { + /* Convert each compound element */ + int nmembs = H5Tget_nmembers(mem_type_id); + + /* invoke the toArray method */ + if (mToArray == NULL) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, in_obj, mToArray); + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); + + if (jnelmts != nmembs) + H5_BAD_ARGUMENT_ERROR( + ENVONLY, "translate_atomic_wbuf: number of elements not equal to number of members"); + + /* Convert each compound object to an element */ + for (i = 0; i < (size_t)nmembs; i++) { + H5T_class_t memb_vlClass; + size_t memb_vlSize; + size_t memb_offset; + + if ((memb = H5Tget_member_type(mem_type_id, (unsigned int)i)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + memb_offset = H5Tget_member_offset(mem_type_id, (unsigned int)i); + if ((memb_vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(memb_vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + jobject arr_obj = ENVPTR->GetObjectArrayElement(ENVONLY, array, (jsize)i); + translate_atomic_wbuf(ENVONLY, arr_obj, memb, memb_vlClass, + char_buf + i * typeSize + memb_offset); + ENVPTR->DeleteLocalRef(ENVONLY, arr_obj); + H5Tclose(memb); + } + break; + } /* H5T_COMPOUND */ + case H5T_ARRAY: { + void *objBuf = NULL; + + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each array element */ + /* invoke the toArray method */ + if (mToArray == NULL) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, in_obj, mToArray); + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); + + if (jnelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_atomic_wbuf: number of array elements < 0"); + + if (NULL == (objBuf = HDmalloc((size_t)jnelmts * vlSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_atomic_wbuf: failed to allocate buffer"); + + translate_wbuf(ENVONLY, array, memb, vlClass, (jsize)jnelmts, objBuf); + + HDmemcpy(char_buf, (char *)objBuf, vlSize * (size_t)jnelmts); + break; + } /* H5T_ARRAY */ + case H5T_ENUM: + case H5T_BITFIELD: + case H5T_OPAQUE: + case H5T_INTEGER: { + /* Convert each element */ + switch (typeSize) { + case sizeof(jbyte): { + jbyte byteValue = ENVPTR->CallByteMethod(ENVONLY, in_obj, byteValueMid); + HDmemcpy(char_buf, ((char *)&byteValue), typeSize); + break; + } + case sizeof(jshort): { + jshort shortValue = ENVPTR->CallShortMethod(ENVONLY, in_obj, shortValueMid); + HDmemcpy(char_buf, ((char *)&shortValue), typeSize); + break; + } + case sizeof(jint): { + jint intValue = ENVPTR->CallIntMethod(ENVONLY, in_obj, intValueMid); + HDmemcpy(char_buf, ((char *)&intValue), typeSize); + break; + } + case sizeof(jlong): { + jlong longValue = ENVPTR->CallLongMethod(ENVONLY, in_obj, longValueMid); + HDmemcpy(char_buf, ((char *)&longValue), typeSize); + break; + } + } + break; + } /* H5T_INTEGER */ + case H5T_FLOAT: { + /* Convert each element */ + switch (typeSize) { + case sizeof(jfloat): { + jfloat floatValue = ENVPTR->CallFloatMethod(ENVONLY, in_obj, floatValueMid); + HDmemcpy(char_buf, ((char *)&floatValue), typeSize); + break; + } + case sizeof(jdouble): { + jdouble doubleValue = ENVPTR->CallDoubleMethod(ENVONLY, in_obj, doubleValueMid); + HDmemcpy(char_buf, ((char *)&doubleValue), typeSize); + break; + } + } + break; + } /* H5T_FLOAT */ + case H5T_REFERENCE: { + /* Convert each array element */ + jbyte *barray = (jbyte *)ENVPTR->GetByteArrayElements(ENVONLY, in_obj, 0); + HDmemcpy(char_buf, ((char *)barray), typeSize); + ENVPTR->ReleaseByteArrayElements(ENVONLY, in_obj, barray, 0); + break; + } /* H5T_REFERENCE */ + case H5T_STRING: { + htri_t is_variable = 0; + + if ((is_variable = H5Tis_variable_str(mem_type_id)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each pointer element */ + jsize length; + const char *utf8 = NULL; + + HDmemset(char_buf, 0, typeSize); + if (NULL != in_obj) { + PIN_JAVA_STRING(ENVONLY, in_obj, utf8, NULL, "translate_atomic_wbuf jobj not pinned"); + length = ENVPTR->GetStringUTFLength(ENVONLY, in_obj); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + if (is_variable) { + char *new_buf = (char *)calloc(1, (size_t)(length + 1)); + HDmemcpy(((char *)new_buf), utf8, (size_t)length); + HDmemcpy(char_buf, &new_buf, typeSize); + } + else { + HDmemcpy(char_buf, utf8, (size_t)length); + } + UNPIN_JAVA_STRING(ENVONLY, in_obj, utf8); + utf8 = NULL; + } + break; + } /* H5T_STRING */ + default: + H5_UNIMPLEMENTED(ENVONLY, "translate_atomic_wbuf: invalid class type"); + break; + } /* switch(type_class) */ + +done: + + return; +} + +void +translate_rbuf(JNIEnv *env, jobjectArray ret_buf, jlong mem_type_id, H5T_class_t type_class, jsize count, + void *raw_buf) +{ + hid_t memb = H5I_INVALID_HID; + int ret_buflen = -1; + jboolean found_jList = JNI_TRUE; + jobjectArray jList = NULL; + jobject jobj = NULL; + H5T_class_t vlClass; + size_t vlSize; + size_t i, x; + size_t typeSize; + // raw_buf is normally bytes except when used for variable length strings + char *char_buf = (char *)raw_buf; + + /* retrieve the java.util.ArrayList interface class */ + jclass arrCList = ENVPTR->FindClass(ENVONLY, "java/util/ArrayList"); + jmethodID arrListMethod = ENVPTR->GetMethodID(ENVONLY, arrCList, "<init>", "(I)V"); + jmethodID arrAddMethod = ENVPTR->GetMethodID(ENVONLY, arrCList, "add", "(Ljava/lang/Object;)Z"); + + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + ret_buflen = ENVPTR->GetArrayLength(ENVONLY, ret_buf); + if (ret_buflen < 0) + H5_JNI_FATAL_ERROR(ENVONLY, "ret_buflen: Array length cannot be negative"); + + switch (type_class) { + case H5T_VLEN: { + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each element to a list */ + for (i = 0; i < (size_t)count; i++) { + hvl_t vl_elem; + + found_jList = JNI_TRUE; + jList = NULL; + + /* Get the number of sequence elements */ + HDmemcpy(&vl_elem, char_buf + i * sizeof(hvl_t), sizeof(hvl_t)); + jsize nelmts = (jsize)vl_elem.len; + if (vl_elem.len != (size_t)nelmts) + H5_JNI_FATAL_ERROR(ENVONLY, "translate_rbuf: overflow of number of VL elements"); + + if (nelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_rbuf: number of VL elements < 0"); + + /* The list we're going to return: */ + if (i < (size_t)ret_buflen) { + jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)ret_buf, (jsize)i); + } + if (NULL == jList) { + found_jList = JNI_FALSE; + if (NULL == + (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, + "translate_rbuf: failed to allocate list read buffer"); + } + + translate_rbuf(ENVONLY, jList, memb, vlClass, (jsize)nelmts, vl_elem.p); + if (found_jList == JNI_FALSE) { + jboolean addResult = + ENVPTR->CallBooleanMethod(ENVONLY, ret_buf, arrAddMethod, (jobject)jList); + if (!addResult) + H5_JNI_FATAL_ERROR(ENVONLY, "translate_rbuf: cannot add VL element"); + } + else { + ENVPTR->SetObjectArrayElement(ENVONLY, ret_buf, (jsize)i, (jobject)jList); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + } + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } + break; + } /* H5T_VLEN */ + case H5T_COMPOUND: { + /* Convert each compound element to a list */ + for (i = 0; i < (size_t)count; i++) { + found_jList = JNI_TRUE; + jList = NULL; + + /* The list we're going to return: */ + if (i < (size_t)ret_buflen) { + jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)ret_buf, (jsize)i); + } + if (NULL == jList) { + found_jList = JNI_FALSE; + if (NULL == + (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, + "translate_rbuf: failed to allocate list read buffer"); + } + int nmembs = H5Tget_nmembers(mem_type_id); + /* Convert each element to a list */ + for (x = 0; x < (size_t)nmembs; x++) { + H5T_class_t memb_vlClass; + size_t memb_vlSize; + size_t memb_offset; + + if ((memb = H5Tget_member_type(mem_type_id, (unsigned int)x)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + memb_offset = H5Tget_member_offset(mem_type_id, (unsigned int)x); + + if ((memb_vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(memb_vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + jobj = translate_atomic_rbuf(ENVONLY, memb, memb_vlClass, + char_buf + i * typeSize + memb_offset); + if (jobj) { + if (found_jList == JNI_FALSE) + ENVPTR->CallBooleanMethod(ENVONLY, jList, arrAddMethod, (jobject)jobj); + else + ENVPTR->SetObjectArrayElement(ENVONLY, jList, (jsize)i, (jobject)jobj); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + ENVPTR->DeleteLocalRef(ENVONLY, jobj); + } + + H5Tclose(memb); + } + if (ret_buflen == 0) + ENVPTR->CallBooleanMethod(ENVONLY, ret_buf, arrAddMethod, jList); + else + ENVPTR->SetObjectArrayElement(ENVONLY, ret_buf, (jsize)i, jList); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } + break; + } /* H5T_COMPOUND */ + case H5T_ARRAY: { + void *objBuf = NULL; + size_t typeCount; + + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + typeCount = typeSize / vlSize; + + if (NULL == (objBuf = HDmalloc(typeSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_rbuf: failed to allocate buffer"); + + /* Convert each element to a list */ + for (i = 0; i < (size_t)count; i++) { + found_jList = JNI_TRUE; + jList = NULL; + + /* Get the object element */ + HDmemcpy((char *)objBuf, char_buf + i * typeSize, typeSize); + + /* The list we're going to return: */ + if (i < (size_t)ret_buflen) { + if (NULL == + (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)ret_buf, (jsize)i))) + found_jList = JNI_FALSE; + } + if (NULL == jList) { + if (NULL == + (jList = (jobjectArray)ENVPTR->NewObject(ENVONLY, arrCList, arrListMethod, 0))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, + "translate_rbuf: failed to allocate list read buffer"); + } + + translate_rbuf(ENVONLY, jList, memb, vlClass, (jsize)typeCount, objBuf); + if (found_jList == JNI_FALSE) + ENVPTR->CallBooleanMethod(ENVONLY, ret_buf, arrAddMethod, jList); + else + ENVPTR->SetObjectArrayElement(ENVONLY, ret_buf, (jsize)i, jList); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } + + if (objBuf) + HDfree(objBuf); + + break; + } /* H5T_ARRAY */ + case H5T_ENUM: + case H5T_BITFIELD: + case H5T_OPAQUE: + case H5T_INTEGER: + case H5T_FLOAT: + case H5T_REFERENCE: + case H5T_STRING: { + /* Convert each element to a list */ + for (i = 0; i < (size_t)count; i++) { + jobj = translate_atomic_rbuf(ENVONLY, mem_type_id, type_class, char_buf + i * typeSize); + if (jobj) { + if (ret_buflen == 0) + ENVPTR->CallBooleanMethod(ENVONLY, ret_buf, arrAddMethod, (jobject)jobj); + else + ENVPTR->SetObjectArrayElement(ENVONLY, ret_buf, (jsize)i, (jobject)jobj); + CHECK_JNI_EXCEPTION(ENVONLY, JNI_TRUE); + ENVPTR->DeleteLocalRef(ENVONLY, jobj); + } + } + break; + } + default: + H5_UNIMPLEMENTED(ENVONLY, "translate_rbuf: invalid class type"); + break; + } /* switch(type_class) */ + +done: + + return; +} + +void +translate_wbuf(JNIEnv *env, jobjectArray in_buf, jlong mem_type_id, H5T_class_t type_class, jsize count, + void *raw_buf) +{ + hid_t memb = H5I_INVALID_HID; + jobjectArray jList = NULL; + jobject jobj = NULL; + H5T_class_t vlClass; + size_t vlSize; + size_t i, x; + size_t typeSize; + // raw_buf is normally bytes except when used for variable length strings + char *char_buf = (char *)raw_buf; + + /* retrieve the java.util.ArrayList interface class */ + jclass arrCList = ENVPTR->FindClass(ENVONLY, "java/util/ArrayList"); + /* retrieve the toArray method */ + jmethodID mToArray = ENVPTR->GetMethodID(ENVONLY, arrCList, "toArray", "()[Ljava/lang/Object;"); + + if (!(typeSize = H5Tget_size(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + + switch (type_class) { + case H5T_VLEN: { + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class((hid_t)memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each list to a vlen element */ + for (i = 0; i < (size_t)count; i++) { + hvl_t vl_elem; + + if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)in_buf, (jsize)i))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + /* invoke the toArray method */ + if (mToArray == NULL) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, jList, mToArray); + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); + + if (jnelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_wbuf: number of VL elements < 0"); + + vl_elem.len = (size_t)jnelmts; + + if (NULL == (vl_elem.p = HDmalloc((size_t)jnelmts * vlSize))) + H5_OUT_OF_MEMORY_ERROR(ENVONLY, "translate_wbuf: failed to allocate vlen ptr buffer"); + + translate_wbuf(ENVONLY, array, memb, vlClass, (jsize)jnelmts, vl_elem.p); + + HDmemcpy(char_buf + i * sizeof(hvl_t), &vl_elem, sizeof(hvl_t)); + + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } /* end for (i = 0; i < count; i++) */ + break; + } /* H5T_VLEN */ + case H5T_COMPOUND: { + /* Convert each list to a compound element */ + for (i = 0; i < (size_t)count; i++) { + if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)in_buf, (jsize)i))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + int nmembs = H5Tget_nmembers(mem_type_id); + + /* invoke the toArray method */ + if (mToArray == NULL) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, jList, mToArray); + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); + + if (jnelmts != nmembs) + H5_BAD_ARGUMENT_ERROR( + ENVONLY, "translate_wbuf: number of elements not equal to number of members"); + + /* Convert each compound object to an element */ + for (x = 0; x < (size_t)nmembs; x++) { + H5T_class_t memb_vlClass; + size_t memb_vlSize; + size_t memb_offset; + + if ((memb = H5Tget_member_type(mem_type_id, (unsigned int)x)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + memb_offset = H5Tget_member_offset(mem_type_id, (unsigned int)x); + + if ((memb_vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(memb_vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + jobject arr_obj = ENVPTR->GetObjectArrayElement(ENVONLY, array, (jsize)x); + translate_atomic_wbuf(ENVONLY, arr_obj, memb, memb_vlClass, + char_buf + i * typeSize + memb_offset); + ENVPTR->DeleteLocalRef(ENVONLY, arr_obj); + H5Tclose(memb); + } + + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } /* end for (i = 0; i < count; i++) */ + break; + } /* H5T_COMPOUND */ + case H5T_ARRAY: { + if (!(memb = H5Tget_super(mem_type_id))) + H5_LIBRARY_ERROR(ENVONLY); + if ((vlClass = H5Tget_class(memb)) < 0) + H5_LIBRARY_ERROR(ENVONLY); + if (!(vlSize = H5Tget_size(memb))) + H5_LIBRARY_ERROR(ENVONLY); + + /* Convert each list to an array element */ + for (i = 0; i < (size_t)count; i++) { + if (NULL == (jList = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)in_buf, (jsize)i))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + + /* invoke the toArray method */ + if (mToArray == NULL) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + jobjectArray array = (jobjectArray)ENVPTR->CallObjectMethod(ENVONLY, jList, mToArray); + jsize jnelmts = ENVPTR->GetArrayLength(ENVONLY, array); + + if (jnelmts < 0) + H5_BAD_ARGUMENT_ERROR(ENVONLY, "translate_wbuf: number of array elements < 0"); + + translate_wbuf(ENVONLY, array, memb, vlClass, jnelmts, + char_buf + i * vlSize * (size_t)jnelmts); + + ENVPTR->DeleteLocalRef(ENVONLY, jList); + } /* end for (i = 0; i < count; i++) */ + break; + } /* H5T_ARRAY */ + case H5T_ENUM: + case H5T_BITFIELD: + case H5T_OPAQUE: + case H5T_INTEGER: + case H5T_FLOAT: + case H5T_REFERENCE: + case H5T_STRING: { + /* Convert each list to an array element */ + for (i = 0; i < (size_t)count; i++) { + if (NULL == (jobj = ENVPTR->GetObjectArrayElement(ENVONLY, (jobjectArray)in_buf, (jsize)i))) + CHECK_JNI_EXCEPTION(ENVONLY, JNI_FALSE); + translate_atomic_wbuf(ENVONLY, jobj, mem_type_id, type_class, char_buf + i * typeSize); + ENVPTR->DeleteLocalRef(ENVONLY, jobj); + } + break; + } + default: + H5_UNIMPLEMENTED(ENVONLY, "translate_wbuf: invalid class type"); + break; + } /* switch(type_class) */ + +done: + + return; +} + #ifdef __cplusplus } #endif diff --git a/java/src/jni/h5util.h b/java/src/jni/h5util.h index 1d76823..e09d94b 100644 --- a/java/src/jni/h5util.h +++ b/java/src/jni/h5util.h @@ -50,6 +50,11 @@ extern int h5str_dump_simple_mem(JNIEnv *env, FILE *stream, hid_t attr, int b extern htri_t H5Tdetect_variable_str(hid_t tid); +extern void translate_rbuf(JNIEnv *env, jobjectArray ret_buf, jlong mem_type_id, H5T_class_t type_class, + jsize count, void *raw_buf); +extern void translate_wbuf(JNIEnv *env, jobjectArray ret_buf, jlong mem_type_id, H5T_class_t type_class, + jsize count, void *raw_buf); + /* * Symbols used to format the output of h5str_sprintf and * to interpret the input to h5str_convert. diff --git a/java/test/TestH5A.java b/java/test/TestH5A.java index 2633fde..0d38914 100644 --- a/java/test/TestH5A.java +++ b/java/test/TestH5A.java @@ -46,6 +46,7 @@ public class TestH5A { private static final int DIM_Y = 6; long H5fid = HDF5Constants.H5I_INVALID_HID; long H5dsid = HDF5Constants.H5I_INVALID_HID; + long H5atid = HDF5Constants.H5I_INVALID_HID; long H5did = HDF5Constants.H5I_INVALID_HID; long[] H5dims = {DIM_X, DIM_Y}; long type_id = HDF5Constants.H5I_INVALID_HID; @@ -133,8 +134,12 @@ public class TestH5A { } catch (Exception ex) { } - - _deleteFile(H5_FILE); + if (H5atid > 0) + try { + H5.H5Tclose(H5atid); + } + catch (Exception ex) { + } if (type_id > 0) try { @@ -154,6 +159,8 @@ public class TestH5A { } catch (Exception ex) { } + + _deleteFile(H5_FILE); System.out.println(); } @@ -1066,7 +1073,7 @@ public class TestH5A { HDF5Constants.H5P_DEFAULT); assertTrue("testH5Awrite_readVL: ", attr_id >= 0); - H5.H5Awrite_VLStrings(attr_id, atype_id, str_data); + H5.H5AwriteVL(attr_id, atype_id, str_data); H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); @@ -1078,7 +1085,7 @@ public class TestH5A { strs[j] = ""; } try { - H5.H5Aread_VLStrings(attr_id, atype_id, strs); + H5.H5AreadVL(attr_id, atype_id, strs); } catch (Exception ex) { ex.printStackTrace(); @@ -1495,9 +1502,8 @@ public class TestH5A { H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); - for (int j = 0; j < dims.length; j++) { + for (int j = 0; j < dims.length; j++) lsize *= dims[j]; - } // Read Integer data ArrayList[] vl_readbuf = new ArrayList[4]; @@ -1570,4 +1576,422 @@ public class TestH5A { } } } + + @Test + public void testH5AVLwrVL() + { + String attr_int_name = "VLIntdata"; + long attr_int_id = HDF5Constants.H5I_INVALID_HID; + long atype_int_id = HDF5Constants.H5I_INVALID_HID; + long base_atype_int_id = HDF5Constants.H5I_INVALID_HID; + long aspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {4}; + long lsize = 1; + + ArrayList[] base_vl_int_data = new ArrayList[4]; + ArrayList[] vl_int_data = new ArrayList[4]; + try { + // Write Integer data + vl_int_data[0] = new ArrayList<Integer>(Arrays.asList(1)); + vl_int_data[1] = new ArrayList<Integer>(Arrays.asList(2, 3)); + vl_int_data[2] = new ArrayList<Integer>(Arrays.asList(4, 5, 6)); + vl_int_data[3] = new ArrayList<Integer>(Arrays.asList(7, 8, 9, 10)); + Class dataClass = vl_int_data.getClass(); + assertTrue("testH5AVLwrVL.getClass: " + dataClass, dataClass.isArray()); + + // Write VL data + base_vl_int_data[0] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[0].add(vl_int_data[0]); + base_vl_int_data[1] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[1].add(vl_int_data[0]); + base_vl_int_data[1].add(vl_int_data[1]); + base_vl_int_data[2] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[2].add(vl_int_data[0]); + base_vl_int_data[2].add(vl_int_data[1]); + base_vl_int_data[2].add(vl_int_data[2]); + base_vl_int_data[3] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[3].add(vl_int_data[0]); + base_vl_int_data[3].add(vl_int_data[1]); + base_vl_int_data[3].add(vl_int_data[2]); + base_vl_int_data[3].add(vl_int_data[3]); + + try { + atype_int_id = H5.H5Tvlen_create(HDF5Constants.H5T_STD_U32LE); + assertTrue("testH5AVLwr.H5Tvlen_create: ", atype_int_id >= 0); + base_atype_int_id = H5.H5Tvlen_create(atype_int_id); + assertTrue("testH5AVLwrVL.H5Tvlen_create: ", base_atype_int_id >= 0); + } + catch (Exception err) { + if (base_atype_int_id > 0) + try { + H5.H5Tclose(base_atype_int_id); + } + catch (Exception ex) { + } + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5AVLwrVL: " + err); + } + + try { + aspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(aspace_id > 0); + attr_int_id = H5.H5Acreate(H5did, attr_int_name, base_atype_int_id, aspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5AVLwrVL: ", attr_int_id >= 0); + + H5.H5AwriteVL(attr_int_id, base_atype_int_id, base_vl_int_data); + } + catch (Exception err) { + if (attr_int_id > 0) + try { + H5.H5Aclose(attr_int_id); + } + catch (Exception ex) { + } + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5AVLwrVL: " + err); + } + finally { + if (aspace_id > 0) + try { + H5.H5Sclose(aspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + // Read Integer data + ArrayList[] base_vl_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + base_vl_readbuf[j] = new ArrayList<ArrayList<Integer>>(); + + try { + H5.H5AreadVL(attr_int_id, base_atype_int_id, base_vl_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + ArrayList<ArrayList<Integer>> vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[0]; + assertTrue("vl_readbuf 0 exists", vl_readbuf != null); + ArrayList<Integer> vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(0)); + /* + * System.out.println(); System.out.println("vl_readbuf: " + vl_readbuf); + * System.out.println("vl_readbuf_int: " + vl_readbuf_int); + */ + assertTrue("testHADVLwrVL:" + vl_readbuf_int.get(0), + vl_int_data[0].get(0).equals(vl_readbuf_int.get(0))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[1]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(1)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5AVLwrVL:" + vl_readbuf_int.get(1), + vl_int_data[1].get(1).equals(vl_readbuf_int.get(1))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[2]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(2)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5AVLwrVL:" + vl_readbuf_int.get(2), + vl_int_data[2].get(2).equals(vl_readbuf_int.get(2))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[3]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(3)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5AVLwrVL:" + vl_readbuf_int.get(3), + vl_int_data[3].get(3).equals(vl_readbuf_int.get(3))); + } + catch (Throwable err) { + err.printStackTrace(); + fail("H5.testH5AVLwrVL: " + err); + } + finally { + if (attr_int_id > 0) + try { + H5.H5Aclose(attr_int_id); + } + catch (Exception ex) { + } + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + if (base_atype_int_id > 0) + try { + H5.H5Tclose(base_atype_int_id); + } + catch (Exception ex) { + } + } + } + + @Test + public void testH5AArraywr() + { + String att_int_name = "ArrayIntdata"; + long att_int_id = HDF5Constants.H5I_INVALID_HID; + long atype_int_id = HDF5Constants.H5I_INVALID_HID; + long aspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {4}; + long lsize = 1; + + ArrayList[] arr_int_data = new ArrayList[4]; + try { + // Write Integer data + arr_int_data[0] = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4)); + arr_int_data[1] = new ArrayList<Integer>(Arrays.asList(2, 3, 4, 5)); + arr_int_data[2] = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 7)); + arr_int_data[3] = new ArrayList<Integer>(Arrays.asList(7, 8, 9, 10)); + Class dataClass = arr_int_data.getClass(); + assertTrue("testH5AArraywr.getClass: " + dataClass, dataClass.isArray()); + + try { + atype_int_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_U32LE, 1, dims); + assertTrue("testH5AArraywr.H5Tarray_create: ", atype_int_id >= 0); + } + catch (Exception err) { + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5AArraywr: " + err); + } + + try { + aspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(aspace_id > 0); + att_int_id = H5.H5Acreate(H5did, att_int_name, atype_int_id, aspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5AVLwr: ", att_int_id >= 0); + + H5.H5AwriteVL(att_int_id, atype_int_id, arr_int_data); + } + catch (Exception err) { + if (att_int_id > 0) + try { + H5.H5Aclose(att_int_id); + } + catch (Exception ex) { + } + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5AVLwr: " + err); + } + finally { + if (aspace_id > 0) + try { + H5.H5Sclose(aspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + // Read Integer data + ArrayList[] arr_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + arr_readbuf[j] = new ArrayList<Integer>(); + + try { + H5.H5AreadVL(att_int_id, atype_int_id, arr_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + assertTrue("testH5AVLwr:" + arr_readbuf[0].get(0), + arr_int_data[0].get(0).equals(arr_readbuf[0].get(0))); + assertTrue("testH5AVLwr:" + arr_readbuf[1].get(0), + arr_int_data[1].get(0).equals(arr_readbuf[1].get(0))); + assertTrue("testH5AVLwr:" + arr_readbuf[2].get(0), + arr_int_data[2].get(0).equals(arr_readbuf[2].get(0))); + assertTrue("testH5AVLwr:" + arr_readbuf[3].get(0), + arr_int_data[3].get(0).equals(arr_readbuf[3].get(0))); + } + catch (Throwable err) { + err.printStackTrace(); + fail("H5.testH5AArraywr: " + err); + } + finally { + if (att_int_id > 0) + try { + H5.H5Aclose(att_int_id); + } + catch (Exception ex) { + } + if (atype_int_id > 0) + try { + H5.H5Tclose(atype_int_id); + } + catch (Exception ex) { + } + } + } + + @Test + public void testH5AArray_string_buffer() throws Throwable + { + String att_str_name = "ArrayStringdata"; + long att_str_id = HDF5Constants.H5I_INVALID_HID; + long atype_str_id = HDF5Constants.H5I_INVALID_HID; + long aspace_id = HDF5Constants.H5I_INVALID_HID; + long[] strdims = {4}; + long[] dims = {6}; + long lsize = 1; + + String[] str_data0 = {"Parting", "is such", "sweet", "sorrow."}; + String[] str_data1 = {"Testing", "one", "two", "three."}; + String[] str_data2 = {"Dog,", "man's", "best", "friend."}; + String[] str_data3 = {"Diamonds", "are", "a", "girls!"}; + String[] str_data4 = {"S A", "T U R", "D A Y", "night"}; + String[] str_data5 = {"That's", "all", "folks", "!!!"}; + + ArrayList[] arr_str_data = new ArrayList[6]; + arr_str_data[0] = new ArrayList<String>(Arrays.asList(str_data0)); + arr_str_data[1] = new ArrayList<String>(Arrays.asList(str_data1)); + arr_str_data[2] = new ArrayList<String>(Arrays.asList(str_data2)); + arr_str_data[3] = new ArrayList<String>(Arrays.asList(str_data3)); + arr_str_data[4] = new ArrayList<String>(Arrays.asList(str_data4)); + arr_str_data[5] = new ArrayList<String>(Arrays.asList(str_data5)); + + try { + H5atid = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5AArray_string_buffer.H5.H5Tcopy: " + err); + } + assertTrue("testH5AArray_string_buffer.H5Tcopy: ", H5atid >= 0); + try { + H5.H5Tset_size(H5atid, HDF5Constants.H5T_VARIABLE); + assertTrue("testH5AArray_string_buffer.H5Tis_variable_str", H5.H5Tis_variable_str(H5atid)); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5DArray_string_buffer.H5Tset_size: " + err); + } + try { + atype_str_id = H5.H5Tarray_create(H5atid, 1, strdims); + assertTrue("testH5AArray_string_buffer.H5Tarray_create: ", atype_str_id >= 0); + } + catch (Exception err) { + if (atype_str_id > 0) + try { + H5.H5Tclose(atype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5AArray_string_buffer: " + err); + } + + try { + aspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(aspace_id > 0); + att_str_id = H5.H5Acreate(H5did, att_str_name, atype_str_id, aspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + assertTrue("testH5AArray_string_buffer: ", att_str_id >= 0); + + H5.H5AwriteVL(att_str_id, atype_str_id, arr_str_data); + } + catch (Exception err) { + if (att_str_id > 0) + try { + H5.H5Dclose(att_str_id); + } + catch (Exception ex) { + } + if (atype_str_id > 0) + try { + H5.H5Tclose(atype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5AArray_string_buffer: " + err); + } + finally { + if (aspace_id > 0) + try { + H5.H5Sclose(aspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + ArrayList[] arr_readbuf = new ArrayList[6]; + for (int j = 0; j < lsize; j++) + arr_readbuf[j] = new ArrayList<String>(); + + try { + H5.H5AreadVL(att_str_id, atype_str_id, arr_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + finally { + if (att_str_id > 0) + try { + H5.H5Aclose(att_str_id); + } + catch (Exception ex) { + } + if (atype_str_id > 0) + try { + H5.H5Tclose(atype_str_id); + } + catch (Exception ex) { + } + } + assertTrue("testH5AArray_string_buffer:" + arr_readbuf[0].get(0), + arr_str_data[0].get(0).equals(arr_readbuf[0].get(0))); + assertTrue("testH5AArray_string_buffer:" + arr_readbuf[1].get(0), + arr_str_data[1].get(0).equals(arr_readbuf[1].get(0))); + assertTrue("testH5AArray_string_buffer:" + arr_readbuf[2].get(0), + arr_str_data[2].get(0).equals(arr_readbuf[2].get(0))); + assertTrue("testH5AArray_string_buffer:" + arr_readbuf[3].get(0), + arr_str_data[3].get(0).equals(arr_readbuf[3].get(0))); + } } diff --git a/java/test/TestH5D.java b/java/test/TestH5D.java index f7e5702..eacaabf 100644 --- a/java/test/TestH5D.java +++ b/java/test/TestH5D.java @@ -31,6 +31,7 @@ import hdf.hdf5lib.exceptions.HDF5LibraryException; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; @@ -157,7 +158,7 @@ public class TestH5D { assertTrue("TestH5D._createDataset.H5Dcreate: ", H5did >= 0); } - private final void _createVLDataset(long fid, long dsid, String name, long dapl) + private final void _createVLStrDataset(String name, long dapl) { try { H5dtid = H5.H5Tcopy(HDF5Constants.H5T_C_S1); @@ -166,24 +167,24 @@ public class TestH5D { err.printStackTrace(); fail("H5.H5Tcopy: " + err); } - assertTrue("TestH5D._createVLDataset.H5Tcopy: ", H5dtid >= 0); + assertTrue("TestH5D._createVLStrDataset.H5Tcopy: ", H5dtid >= 0); try { H5.H5Tset_size(H5dtid, HDF5Constants.H5T_VARIABLE); - assertTrue("TestH5D._createVLDataset.H5Tis_variable_str", H5.H5Tis_variable_str(H5dtid)); + assertTrue("TestH5D._createVLStrDataset.H5Tis_variable_str", H5.H5Tis_variable_str(H5dtid)); } catch (Throwable err) { err.printStackTrace(); fail("H5.H5Tset_size: " + err); } try { - H5did = H5.H5Dcreate(fid, name, H5dtid, dsid, HDF5Constants.H5P_DEFAULT, + H5did = H5.H5Dcreate(H5fid, name, H5dtid, H5dsid, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, dapl); } catch (Throwable err) { err.printStackTrace(); fail("H5.H5Dcreate: " + err); } - assertTrue("TestH5D._createVLDataset.H5Dcreate: ", H5did >= 0); + assertTrue("TestH5D._createVLStrDataset.H5Dcreate: ", H5did >= 0); } private final void _closeH5file() throws HDF5LibraryException @@ -934,7 +935,7 @@ public class TestH5D { buf_data[(indx * DIM_Y) + jndx] == 126); } - @Test + @Ignore public void testH5Dvlen_get_buf_size() { String[] str_data = {"Parting", "is such", "sweet", "sorrow.", "Testing", "one", "two", "three.", @@ -945,12 +946,12 @@ public class TestH5D { for (int idx = 0; idx < str_data.length; idx++) str_data_bytes += str_data[idx].length() + 1; // Account for terminating null - _createVLDataset(H5fid, H5dsid, "dset", HDF5Constants.H5P_DEFAULT); + _createVLStrDataset("dset", HDF5Constants.H5P_DEFAULT); try { if ((H5did >= 0) && (H5dtid >= 0)) - H5.H5Dwrite_VLStrings(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, str_data); + H5.H5DwriteVL(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, str_data); } catch (Exception e) { e.printStackTrace(); @@ -964,26 +965,122 @@ public class TestH5D { assertTrue("H5Dvlen_get_buf_size " + vl_size + " == " + str_data_bytes, vl_size == str_data_bytes); } - @Test - public void testH5Dvlen_read_default_buffer() throws Throwable + @Ignore + public void testH5Dvlen_string_buffer() throws Throwable { - String[] str_data = {"Parting", "is such", "sweet", "sorrow.", "Testing", "one", "two", "three.", - "Dog,", "man's", "best", "friend.", "Diamonds", "are", "a", "girls!", - "S A", "T U R", "D A Y", "night", "That's", "all", "folks", "!!!"}; - byte[] read_data = new byte[512]; + String dset_str_name = "VLStringdata"; + long dset_str_id = HDF5Constants.H5I_INVALID_HID; + long dtype_str_id = HDF5Constants.H5I_INVALID_HID; + long dspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {64}; + long lsize = 1; - _createVLDataset(H5fid, H5dsid, "dset", HDF5Constants.H5P_DEFAULT); + String[] str_data0 = {"Parting", "is such", "sweet", "sorrow."}; + String[] str_data1 = {"Testing", "one", "two", "three."}; + String[] str_data2 = {"Dog,", "man's", "best", "friend."}; + String[] str_data3 = {"Diamonds", "are", "a", "girls!"}; + String[] str_data4 = {"S A", "T U R", "D A Y", "night"}; + String[] str_data5 = {"That's", "all", "folks", "!!!"}; + + ArrayList[] vl_str_data = new ArrayList[6]; + vl_str_data[0] = new ArrayList<String>(Arrays.asList(str_data0)); + vl_str_data[1] = new ArrayList<String>(Arrays.asList(str_data1)); + vl_str_data[2] = new ArrayList<String>(Arrays.asList(str_data2)); + vl_str_data[3] = new ArrayList<String>(Arrays.asList(str_data3)); + vl_str_data[4] = new ArrayList<String>(Arrays.asList(str_data4)); + vl_str_data[5] = new ArrayList<String>(Arrays.asList(str_data5)); try { - H5.H5Dwrite_VLStrings(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, str_data); + H5dtid = H5.H5Tcopy(HDF5Constants.H5T_C_S1); } - catch (Exception e) { - e.printStackTrace(); + catch (Throwable err) { + err.printStackTrace(); + fail("testH5Dvlen_string_buffer.H5.H5Tcopy: " + err); + } + assertTrue("testH5Dvlen_string_buffer.H5Tcopy: ", H5dtid >= 0); + try { + H5.H5Tset_size(H5dtid, HDF5Constants.H5T_VARIABLE); + assertTrue("testH5Dvlen_string_buffer.H5Tis_variable_str", H5.H5Tis_variable_str(H5dtid)); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5Dvlen_string_buffer.H5Tset_size: " + err); + } + try { + dtype_str_id = H5.H5Tvlen_create(H5dtid); + assertTrue("testH5Dvlen_string_buffer.H5Tvlen_create: ", dtype_str_id >= 0); + } + catch (Exception err) { + if (dtype_str_id > 0) + try { + H5.H5Tclose(dtype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5Dvlen_string_buffer: " + err); + } + + try { + dspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(dspace_id > 0); + dset_str_id = + H5.H5Dcreate(H5fid, dset_str_name, dtype_str_id, dspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5Dvlen_string_buffer: ", dset_str_id >= 0); + + H5.H5DwriteVL(dset_str_id, dtype_str_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, vl_str_data); + } + catch (Exception err) { + if (dset_str_id > 0) + try { + H5.H5Dclose(dset_str_id); + } + catch (Exception ex) { + } + if (dtype_str_id > 0) + try { + H5.H5Tclose(dtype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5Dvlen_string_buffer: " + err); + } + finally { + if (dspace_id > 0) + try { + H5.H5Sclose(dspace_id); + } + catch (Exception ex) { + } } - H5.H5Dread(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, - read_data); + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + ArrayList[] vl_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + vl_readbuf[j] = new ArrayList<String>(); + + try { + H5.H5DreadVL(dset_str_id, dtype_str_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, vl_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + assertTrue("testH5Dvlen_string_buffer:" + vl_readbuf[0].get(0), + vl_str_data[0].get(0).equals(vl_readbuf[0].get(0))); + assertTrue("testH5Dvlen_string_buffer:" + vl_readbuf[1].get(0), + vl_str_data[1].get(0).equals(vl_readbuf[1].get(0))); + assertTrue("testH5Dvlen_string_buffer:" + vl_readbuf[2].get(0), + vl_str_data[2].get(0).equals(vl_readbuf[2].get(0))); + assertTrue("testH5Dvlen_string_buffer:" + vl_readbuf[3].get(0), + vl_str_data[3].get(0).equals(vl_readbuf[3].get(0))); } @Test @@ -994,20 +1091,21 @@ public class TestH5D { "S A", "T U R", "D A Y", "night", "That's", "all", "folks", "!!!"}; String[] str_rdata = new String[DIM_X * DIM_Y]; - _createVLDataset(H5fid, H5dsid, "dset", HDF5Constants.H5P_DEFAULT); + _createVLStrDataset("dset", HDF5Constants.H5P_DEFAULT); try { if ((H5did >= 0) && (H5dtid >= 0)) - H5.H5Dwrite_VLStrings(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, str_wdata); + H5.H5DwriteVL(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, str_wdata); } catch (Exception e) { e.printStackTrace(); } + try { if ((H5did >= 0) && (H5dtid >= 0)) - H5.H5Dread_VLStrings(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, str_rdata); + H5.H5DreadVL(H5did, H5dtid, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, str_rdata); } catch (Exception e) { e.printStackTrace(); @@ -1153,51 +1251,50 @@ public class TestH5D { H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); - for (int j = 0; j < dims.length; j++) { + for (int j = 0; j < dims.length; j++) lsize *= dims[j]; - } // Read Integer data - ArrayList[] vl_readbuf = new ArrayList[4]; + ArrayList[] vl_int_readbuf = new ArrayList[4]; for (int j = 0; j < lsize; j++) - vl_readbuf[j] = new ArrayList<Integer>(); + vl_int_readbuf[j] = new ArrayList<Integer>(); try { H5.H5DreadVL(dset_int_id, dtype_int_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, vl_readbuf); + HDF5Constants.H5P_DEFAULT, vl_int_readbuf); } catch (Exception ex) { ex.printStackTrace(); } - assertTrue("testH5DVLwr:" + vl_readbuf[0].get(0), - vl_int_data[0].get(0).equals(vl_readbuf[0].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[1].get(0), - vl_int_data[1].get(0).equals(vl_readbuf[1].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[2].get(0), - vl_int_data[2].get(0).equals(vl_readbuf[2].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[3].get(0), - vl_int_data[3].get(0).equals(vl_readbuf[3].get(0))); + assertTrue("testH5DVLwr:" + vl_int_readbuf[0].get(0), + vl_int_data[0].get(0).equals(vl_int_readbuf[0].get(0))); + assertTrue("testH5DVLwr:" + vl_int_readbuf[1].get(0), + vl_int_data[1].get(0).equals(vl_int_readbuf[1].get(0))); + assertTrue("testH5DVLwr:" + vl_int_readbuf[2].get(0), + vl_int_data[2].get(0).equals(vl_int_readbuf[2].get(0))); + assertTrue("testH5DVLwr:" + vl_int_readbuf[3].get(0), + vl_int_data[3].get(0).equals(vl_int_readbuf[3].get(0))); // Read Double data - vl_readbuf = new ArrayList[4]; + ArrayList[] vl_dbl_readbuf = new ArrayList[4]; for (int j = 0; j < lsize; j++) - vl_readbuf[j] = new ArrayList<Double>(); + vl_dbl_readbuf[j] = new ArrayList<Double>(); try { H5.H5DreadVL(dset_dbl_id, dtype_dbl_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, vl_readbuf); + HDF5Constants.H5P_DEFAULT, vl_dbl_readbuf); } catch (Exception ex) { ex.printStackTrace(); } - assertTrue("testH5DVLwr:" + vl_readbuf[0].get(0), - vl_dbl_data[0].get(0).equals(vl_readbuf[0].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[1].get(0), - vl_dbl_data[1].get(0).equals(vl_readbuf[1].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[2].get(0), - vl_dbl_data[2].get(0).equals(vl_readbuf[2].get(0))); - assertTrue("testH5DVLwr:" + vl_readbuf[3].get(0), - vl_dbl_data[3].get(0).equals(vl_readbuf[3].get(0))); + assertTrue("testH5DVLwr:" + vl_dbl_readbuf[0].get(0), + vl_dbl_data[0].get(0).equals(vl_dbl_readbuf[0].get(0))); + assertTrue("testH5DVLwr:" + vl_dbl_readbuf[1].get(0), + vl_dbl_data[1].get(0).equals(vl_dbl_readbuf[1].get(0))); + assertTrue("testH5DVLwr:" + vl_dbl_readbuf[2].get(0), + vl_dbl_data[2].get(0).equals(vl_dbl_readbuf[2].get(0))); + assertTrue("testH5DVLwr:" + vl_dbl_readbuf[3].get(0), + vl_dbl_data[3].get(0).equals(vl_dbl_readbuf[3].get(0))); } catch (Throwable err) { err.printStackTrace(); @@ -1230,4 +1327,624 @@ public class TestH5D { } } } + + @Test + public void testH5DVLwrVL() + { + String dset_int_name = "VLIntdata"; + long dset_int_id = HDF5Constants.H5I_INVALID_HID; + long dtype_int_id = HDF5Constants.H5I_INVALID_HID; + long base_dtype_int_id = HDF5Constants.H5I_INVALID_HID; + long dspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {4}; + long lsize = 1; + + ArrayList[] base_vl_int_data = new ArrayList[4]; + ArrayList[] vl_int_data = new ArrayList[4]; + try { + // Write Integer data + vl_int_data[0] = new ArrayList<Integer>(Arrays.asList(1)); + vl_int_data[1] = new ArrayList<Integer>(Arrays.asList(2, 3)); + vl_int_data[2] = new ArrayList<Integer>(Arrays.asList(4, 5, 6)); + vl_int_data[3] = new ArrayList<Integer>(Arrays.asList(7, 8, 9, 10)); + Class dataClass = vl_int_data.getClass(); + assertTrue("testH5DVLwrVL.getClass: " + dataClass, dataClass.isArray()); + + // Write VL data + base_vl_int_data[0] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[0].add(vl_int_data[0]); + base_vl_int_data[1] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[1].add(vl_int_data[0]); + base_vl_int_data[1].add(vl_int_data[1]); + base_vl_int_data[2] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[2].add(vl_int_data[0]); + base_vl_int_data[2].add(vl_int_data[1]); + base_vl_int_data[2].add(vl_int_data[2]); + base_vl_int_data[3] = new ArrayList<ArrayList<Integer>>(); + base_vl_int_data[3].add(vl_int_data[0]); + base_vl_int_data[3].add(vl_int_data[1]); + base_vl_int_data[3].add(vl_int_data[2]); + base_vl_int_data[3].add(vl_int_data[3]); + + try { + dtype_int_id = H5.H5Tvlen_create(HDF5Constants.H5T_STD_U32LE); + assertTrue("testH5DVLwrVL.H5Tvlen_create: ", dtype_int_id >= 0); + base_dtype_int_id = H5.H5Tvlen_create(dtype_int_id); + assertTrue("testH5DVLwrVL.H5Tvlen_create: ", base_dtype_int_id >= 0); + } + catch (Exception err) { + if (base_dtype_int_id > 0) + try { + H5.H5Tclose(base_dtype_int_id); + } + catch (Exception ex) { + } + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5DVLwrVL: " + err); + } + + try { + dspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(dspace_id > 0); + dset_int_id = H5.H5Dcreate(H5fid, dset_int_name, base_dtype_int_id, dspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + assertTrue("testH5DVLwrVL: ", dset_int_id >= 0); + + H5.H5DwriteVL(dset_int_id, base_dtype_int_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, base_vl_int_data); + } + catch (Exception err) { + if (dset_int_id > 0) + try { + H5.H5Dclose(dset_int_id); + } + catch (Exception ex) { + } + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5DVLwrVL: " + err); + } + finally { + if (dspace_id > 0) + try { + H5.H5Sclose(dspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + // Read Integer data + ArrayList[] base_vl_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + base_vl_readbuf[j] = new ArrayList<ArrayList<Integer>>(); + + try { + H5.H5DreadVL(dset_int_id, base_dtype_int_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, base_vl_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + ArrayList<ArrayList<Integer>> vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[0]; + assertTrue("vl_readbuf 0 exists", vl_readbuf != null); + ArrayList<Integer> vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(0)); + /* + * System.out.println(); System.out.println("vl_readbuf: " + vl_readbuf); + * System.out.println("vl_readbuf_int: " + vl_readbuf_int); + */ + assertTrue("testH5DVLwrVL:" + vl_readbuf_int.get(0), + vl_int_data[0].get(0).equals(vl_readbuf_int.get(0))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[1]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(1)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5DVLwrVL:" + vl_readbuf_int.get(1), + vl_int_data[1].get(1).equals(vl_readbuf_int.get(1))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[2]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(2)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5DVLwrVL:" + vl_readbuf_int.get(2), + vl_int_data[2].get(2).equals(vl_readbuf_int.get(2))); + + vl_readbuf = (ArrayList<ArrayList<Integer>>)base_vl_readbuf[3]; + vl_readbuf_int = (ArrayList<Integer>)(vl_readbuf.get(3)); + /* + * System.out.println("vl_readbuf: " + vl_readbuf); System.out.println("vl_readbuf_int: " + + * vl_readbuf_int); + */ + assertTrue("testH5DVLwrVL:" + vl_readbuf_int.get(3), + vl_int_data[3].get(3).equals(vl_readbuf_int.get(3))); + } + catch (Throwable err) { + err.printStackTrace(); + fail("H5.testH5DVLwrVL: " + err); + } + finally { + if (dset_int_id > 0) + try { + H5.H5Dclose(dset_int_id); + } + catch (Exception ex) { + } + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + if (base_dtype_int_id > 0) + try { + H5.H5Tclose(base_dtype_int_id); + } + catch (Exception ex) { + } + } + } + + @Test + public void testH5DArraywr() + { + String dset_int_name = "ArrayIntdata"; + long dset_int_id = HDF5Constants.H5I_INVALID_HID; + long dtype_int_id = HDF5Constants.H5I_INVALID_HID; + long dspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {4}; + long lsize = 1; + + ArrayList[] arr_int_data = new ArrayList[4]; + try { + // Write Integer data + arr_int_data[0] = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4)); + arr_int_data[1] = new ArrayList<Integer>(Arrays.asList(2, 3, 4, 5)); + arr_int_data[2] = new ArrayList<Integer>(Arrays.asList(4, 5, 6, 7)); + arr_int_data[3] = new ArrayList<Integer>(Arrays.asList(7, 8, 9, 10)); + Class dataClass = arr_int_data.getClass(); + assertTrue("testH5DArraywr.getClass: " + dataClass, dataClass.isArray()); + + try { + dtype_int_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_U32LE, 1, dims); + assertTrue("testH5DArraywr.H5Tarray_create: ", dtype_int_id >= 0); + } + catch (Exception err) { + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5DArraywr: " + err); + } + + try { + dspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(dspace_id > 0); + dset_int_id = + H5.H5Dcreate(H5fid, dset_int_name, dtype_int_id, dspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5DVLwr: ", dset_int_id >= 0); + + H5.H5DwriteVL(dset_int_id, dtype_int_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_int_data); + } + catch (Exception err) { + if (dset_int_id > 0) + try { + H5.H5Dclose(dset_int_id); + } + catch (Exception ex) { + } + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5DVLwr: " + err); + } + finally { + if (dspace_id > 0) + try { + H5.H5Sclose(dspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + // Read Integer data + ArrayList[] arr_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + arr_readbuf[j] = new ArrayList<Integer>(); + + try { + H5.H5DreadVL(dset_int_id, dtype_int_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + assertTrue("testH5DVLwr:" + arr_readbuf[0].get(0), + arr_int_data[0].get(0).equals(arr_readbuf[0].get(0))); + assertTrue("testH5DVLwr:" + arr_readbuf[1].get(0), + arr_int_data[1].get(0).equals(arr_readbuf[1].get(0))); + assertTrue("testH5DVLwr:" + arr_readbuf[2].get(0), + arr_int_data[2].get(0).equals(arr_readbuf[2].get(0))); + assertTrue("testH5DVLwr:" + arr_readbuf[3].get(0), + arr_int_data[3].get(0).equals(arr_readbuf[3].get(0))); + } + catch (Throwable err) { + err.printStackTrace(); + fail("H5.testH5DArraywr: " + err); + } + finally { + if (dset_int_id > 0) + try { + H5.H5Dclose(dset_int_id); + } + catch (Exception ex) { + } + if (dtype_int_id > 0) + try { + H5.H5Tclose(dtype_int_id); + } + catch (Exception ex) { + } + } + } + + @Test + public void testH5DArray_string_buffer() throws Throwable + { + String dset_str_name = "ArrayStringdata"; + long dset_str_id = HDF5Constants.H5I_INVALID_HID; + long dtype_str_id = HDF5Constants.H5I_INVALID_HID; + long dspace_id = HDF5Constants.H5I_INVALID_HID; + long[] strdims = {4}; + long[] dims = {6}; + long lsize = 1; + + String[] str_data0 = {"Parting", "is such", "sweet", "sorrow."}; + String[] str_data1 = {"Testing", "one", "two", "three."}; + String[] str_data2 = {"Dog,", "man's", "best", "friend."}; + String[] str_data3 = {"Diamonds", "are", "a", "girls!"}; + String[] str_data4 = {"S A", "T U R", "D A Y", "night"}; + String[] str_data5 = {"That's", "all", "folks", "!!!"}; + + ArrayList[] arr_str_data = new ArrayList[6]; + arr_str_data[0] = new ArrayList<String>(Arrays.asList(str_data0)); + arr_str_data[1] = new ArrayList<String>(Arrays.asList(str_data1)); + arr_str_data[2] = new ArrayList<String>(Arrays.asList(str_data2)); + arr_str_data[3] = new ArrayList<String>(Arrays.asList(str_data3)); + arr_str_data[4] = new ArrayList<String>(Arrays.asList(str_data4)); + arr_str_data[5] = new ArrayList<String>(Arrays.asList(str_data5)); + + try { + H5dtid = H5.H5Tcopy(HDF5Constants.H5T_C_S1); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5DArray_string_buffer.H5.H5Tcopy: " + err); + } + assertTrue("testH5DArray_string_buffer.H5Tcopy: ", H5dtid >= 0); + try { + H5.H5Tset_size(H5dtid, HDF5Constants.H5T_VARIABLE); + assertTrue("testH5DArray_string_buffer.H5Tis_variable_str", H5.H5Tis_variable_str(H5dtid)); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5DArray_string_buffer.H5Tset_size: " + err); + } + try { + dtype_str_id = H5.H5Tarray_create(H5dtid, 1, strdims); + assertTrue("testH5DArray_string_buffer.H5Tarray_create: ", dtype_str_id >= 0); + } + catch (Exception err) { + if (dtype_str_id > 0) + try { + H5.H5Tclose(dtype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5DArray_string_buffer: " + err); + } + + try { + dspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(dspace_id > 0); + dset_str_id = + H5.H5Dcreate(H5fid, dset_str_name, dtype_str_id, dspace_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5DArray_string_buffer: ", dset_str_id >= 0); + + H5.H5DwriteVL(dset_str_id, dtype_str_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_str_data); + } + catch (Exception err) { + if (dset_str_id > 0) + try { + H5.H5Dclose(dset_str_id); + } + catch (Exception ex) { + } + if (dtype_str_id > 0) + try { + H5.H5Tclose(dtype_str_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5DArray_string_buffer: " + err); + } + finally { + if (dspace_id > 0) + try { + H5.H5Sclose(dspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + ArrayList[] arr_readbuf = new ArrayList[6]; + for (int j = 0; j < lsize; j++) + arr_readbuf[j] = new ArrayList<String>(); + + try { + H5.H5DreadVL(dset_str_id, dtype_str_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + finally { + if (dset_str_id > 0) + try { + H5.H5Dclose(dset_str_id); + } + catch (Exception ex) { + } + if (dtype_str_id > 0) + try { + H5.H5Tclose(dtype_str_id); + } + catch (Exception ex) { + } + } + assertTrue("testH5DArray_string_buffer:" + arr_readbuf[0].get(0), + arr_str_data[0].get(0).equals(arr_readbuf[0].get(0))); + assertTrue("testH5DArray_string_buffer:" + arr_readbuf[1].get(0), + arr_str_data[1].get(0).equals(arr_readbuf[1].get(0))); + assertTrue("testH5DArray_string_buffer:" + arr_readbuf[2].get(0), + arr_str_data[2].get(0).equals(arr_readbuf[2].get(0))); + assertTrue("testH5DArray_string_buffer:" + arr_readbuf[3].get(0), + arr_str_data[3].get(0).equals(arr_readbuf[3].get(0))); + } + + @Test + public void testH5DArrayenum_rw() + { + String dset_enum_name = "ArrayEnumdata"; + long dset_enum_id = HDF5Constants.H5I_INVALID_HID; + long dtype_enum_id = HDF5Constants.H5I_INVALID_HID; + long dtype_arr_enum_id = HDF5Constants.H5I_INVALID_HID; + long dspace_id = HDF5Constants.H5I_INVALID_HID; + long[] dims = {4}; + long lsize = 1; + String enum_type = "Enum_type"; + byte[] enum_val = new byte[1]; + String enum_name = null; + + // Create a enumerate datatype + try { + dtype_enum_id = H5.H5Tcreate(HDF5Constants.H5T_ENUM, (long)1); + assertTrue("testH5DArrayenum_wr.H5Tarray_create: ", dtype_enum_id >= 0); + } + catch (Throwable err) { + if (dtype_enum_id > 0) + try { + H5.H5Tclose(dtype_enum_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5DArrayenum_rw:H5Tcreate " + err); + } + + try { + enum_val[0] = 10; + H5.H5Tenum_insert(dtype_enum_id, "RED", enum_val); + enum_val[0] = 11; + H5.H5Tenum_insert(dtype_enum_id, "GREEN", enum_val); + enum_val[0] = 12; + H5.H5Tenum_insert(dtype_enum_id, "BLUE", enum_val); + enum_val[0] = 13; + H5.H5Tenum_insert(dtype_enum_id, "ORANGE", enum_val); + enum_val[0] = 14; + H5.H5Tenum_insert(dtype_enum_id, "YELLOW", enum_val); + + // Query member number and member index by member name, for enumeration type. + assertTrue("Can't get member number", H5.H5Tget_nmembers(dtype_enum_id) == 5); + assertTrue("Can't get correct index number", + H5.H5Tget_member_index(dtype_enum_id, "ORANGE") == 3); + + // Commit enumeration datatype and close it */ + H5.H5Tcommit(H5fid, enum_type, dtype_enum_id, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + + H5.H5Tclose(dtype_enum_id); + + // Open the dataytpe for query + dtype_enum_id = H5.H5Topen(H5fid, enum_type, HDF5Constants.H5P_DEFAULT); + assertTrue("testH5DArrayenum_rw:H5Tcreate", dtype_enum_id >= 0); + + // Query member number and member index by member name, for enumeration type + assertTrue("Can't get member number", H5.H5Tget_nmembers(dtype_enum_id) == 5); + assertTrue("Can't get correct index number", + H5.H5Tget_member_index(dtype_enum_id, "ORANGE") == 3); + + // Query member value by member name, for enumeration type + H5.H5Tenum_valueof(dtype_enum_id, "ORANGE", enum_val); + assertTrue("Incorrect value for enum member", enum_val[0] == 13); + + // Query member value by member index, for enumeration type + H5.H5Tget_member_value(dtype_enum_id, 2, enum_val); + assertTrue("Incorrect value for enum member", enum_val[0] == 12); + + // Query member name by member value, for enumeration type + enum_val[0] = 14; + enum_name = H5.H5Tenum_nameof(dtype_enum_id, enum_val, 16); + assertTrue("Incorrect name for enum member", enum_name.compareTo("YELLOW") == 0); + + ArrayList[] arr_enum_data = new ArrayList[4]; + try { + // Write Integer data + arr_enum_data[0] = new ArrayList<Integer>(Arrays.asList(10, 11, 12, 13)); + arr_enum_data[1] = new ArrayList<Integer>(Arrays.asList(11, 12, 13, 14)); + arr_enum_data[2] = new ArrayList<Integer>(Arrays.asList(12, 13, 14, 10)); + arr_enum_data[3] = new ArrayList<Integer>(Arrays.asList(13, 14, 10, 11)); + Class dataClass = arr_enum_data.getClass(); + assertTrue("testH5DArrayenum_wr.getClass: " + dataClass, dataClass.isArray()); + + try { + dtype_arr_enum_id = H5.H5Tarray_create(HDF5Constants.H5T_STD_U32LE, 1, dims); + assertTrue("testH5DArrayenum_wr.H5Tarray_create: ", dtype_arr_enum_id >= 0); + } + catch (Exception err) { + if (dtype_arr_enum_id > 0) + try { + H5.H5Tclose(dtype_arr_enum_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("H5.testH5DArrayenum_wr: " + err); + } + + dspace_id = H5.H5Screate_simple(1, dims, null); + assertTrue(dspace_id > 0); + dset_enum_id = H5.H5Dcreate(H5fid, dset_enum_name, dtype_arr_enum_id, dspace_id, + HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + assertTrue("testH5DVLwr: ", dset_enum_id >= 0); + + H5.H5DwriteVL(dset_enum_id, dtype_arr_enum_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_enum_data); + } + catch (Throwable err) { + if (dset_enum_id > 0) + try { + H5.H5Dclose(dset_enum_id); + } + catch (Exception ex) { + } + if (dtype_enum_id > 0) + try { + H5.H5Tclose(dtype_enum_id); + } + catch (Exception ex) { + } + if (dtype_arr_enum_id > 0) + try { + H5.H5Tclose(dtype_arr_enum_id); + } + catch (Exception ex) { + } + err.printStackTrace(); + fail("testH5DArrayenum_rw:query " + err); + } + finally { + if (dspace_id > 0) + try { + H5.H5Sclose(dspace_id); + } + catch (Exception ex) { + } + } + + H5.H5Fflush(H5fid, HDF5Constants.H5F_SCOPE_LOCAL); + + for (int j = 0; j < dims.length; j++) + lsize *= dims[j]; + + // Read Integer data + ArrayList[] arr_readbuf = new ArrayList[4]; + for (int j = 0; j < lsize; j++) + arr_readbuf[j] = new ArrayList<Integer>(); + + try { + H5.H5DreadVL(dset_enum_id, dtype_arr_enum_id, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, arr_readbuf); + } + catch (Exception ex) { + ex.printStackTrace(); + } + assertTrue("testH5DVLArrayenum_wr:" + arr_readbuf[0].get(0), + arr_enum_data[0].get(0).equals(arr_readbuf[0].get(0))); + assertTrue("testH5DVLArrayenum_wr:" + arr_readbuf[1].get(0), + arr_enum_data[1].get(0).equals(arr_readbuf[1].get(0))); + assertTrue("testH5DVLArrayenum_wr:" + arr_readbuf[2].get(0), + arr_enum_data[2].get(0).equals(arr_readbuf[2].get(0))); + assertTrue("testH5DVLArrayenum_wr:" + arr_readbuf[3].get(0), + arr_enum_data[3].get(0).equals(arr_readbuf[3].get(0))); + } + catch (Throwable err) { + err.printStackTrace(); + fail("H5.testH5DArrayenum_wr: " + err); + } + finally { + if (dset_enum_id > 0) + try { + H5.H5Dclose(dset_enum_id); + } + catch (Exception ex) { + } + if (dtype_enum_id > 0) + try { + H5.H5Tclose(dtype_enum_id); + } + catch (Exception ex) { + } + if (dtype_arr_enum_id > 0) + try { + H5.H5Tclose(dtype_arr_enum_id); + } + catch (Exception ex) { + } + } + } } diff --git a/java/test/testfiles/JUnit-TestH5A.txt b/java/test/testfiles/JUnit-TestH5A.txt index efdd4a2..f31c86c 100644 --- a/java/test/testfiles/JUnit-TestH5A.txt +++ b/java/test/testfiles/JUnit-TestH5A.txt @@ -11,6 +11,7 @@ JUnit version 4.11 .testH5Aget_info_by_name .testH5Aget_create_plist .testH5Adelete_by_name +.testH5AVLwrVL .testH5Aopen_by_name .testH5Aget_info .testH5Aget_name @@ -22,14 +23,16 @@ JUnit version 4.11 .testH5Adelete_by_idx_order .testH5Arename_by_name .testH5Acreate2_invalidobject +.testH5AArraywr .testH5Acreate2 .testH5Aiterate_by_name .testH5Adelete_by_idx_null .testH5Adelete_by_idx_invalidobject .testH5Awrite_readVL +.testH5AArray_string_buffer .testH5Aget_info1 Time: XXXX -OK (29 tests) +OK (32 tests) diff --git a/java/test/testfiles/JUnit-TestH5D.txt b/java/test/testfiles/JUnit-TestH5D.txt index 288e6d0..4c30621 100644 --- a/java/test/testfiles/JUnit-TestH5D.txt +++ b/java/test/testfiles/JUnit-TestH5D.txt @@ -1,5 +1,8 @@ JUnit version 4.11 +.testH5DArrayenum_rw +.testH5DVLwrVL .testH5Dget_storage_size +.testH5DArraywr .testH5Diterate_write .testH5Dcreate .testH5Dget_offset @@ -12,15 +15,14 @@ JUnit version 4.11 .testH5Dget_storage_size_empty .testH5Diterate .testH5Dget_access_plist -.testH5Dvlen_get_buf_size .testH5Dget_space_closed +.testH5DArray_string_buffer .testH5Dget_space_status .testH5Dvlen_write_read .testH5Dget_space .testH5Dget_type_closed -.testH5Dvlen_read_default_buffer Time: XXXX -OK (20 tests) +OK (22 tests) |