diff options
author | Allen Byrne <50328838+byrnHDF@users.noreply.github.com> | 2022-01-18 20:02:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-18 20:02:53 (GMT) |
commit | 8dc72928b8c2f0390659d14011b34983cf9b1171 (patch) | |
tree | cbaea8d84759aab14b2c16cbb2c0983736659029 /java | |
parent | 2f34c433dc26811909673922838e4a97d7c98b9c (diff) | |
download | hdf5-8dc72928b8c2f0390659d14011b34983cf9b1171.zip hdf5-8dc72928b8c2f0390659d14011b34983cf9b1171.tar.gz hdf5-8dc72928b8c2f0390659d14011b34983cf9b1171.tar.bz2 |
Develop java newrefs fixes for obj ref datasets (#1354)
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Dana Robinson <43805+derobins@users.noreply.github.com>
Diffstat (limited to 'java')
-rw-r--r-- | java/examples/datatypes/H5Ex_T_ObjectReference.java | 165 | ||||
-rw-r--r-- | java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java | 194 | ||||
-rw-r--r-- | java/test/TestH5Ocopy.java | 97 |
3 files changed, 207 insertions, 249 deletions
diff --git a/java/examples/datatypes/H5Ex_T_ObjectReference.java b/java/examples/datatypes/H5Ex_T_ObjectReference.java index 98a11df..b0f98de 100644 --- a/java/examples/datatypes/H5Ex_T_ObjectReference.java +++ b/java/examples/datatypes/H5Ex_T_ObjectReference.java @@ -205,113 +205,86 @@ public class H5Ex_T_ObjectReference { // Open an existing file. try { file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Open an existing dataset. - try { - if (file_id >= 0) - dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Get dataspace and allocate memory for read buffer. - try { - if (dataset_id >= 0) - dataspace_id = H5.H5Dget_space(dataset_id); - } - catch (Exception e) { - e.printStackTrace(); - } - - try { - if (dataspace_id >= 0) - H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Read data. - try { - if (dataset_id >= 0) { - H5.H5Dread(dataset_id, HDF5Constants.H5T_STD_REF, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, - HDF5Constants.H5P_DEFAULT, dset_data); - } - } - catch (Exception e) { - e.printStackTrace(); - } - // Output the data to the screen. - for (int indx = 0; indx < dims[0]; indx++) { - System.out.println(DATASETNAME + "[" + indx + "]:"); - System.out.print(" ->"); - // Open the referenced object, get its name and type. + // Open an existing dataset. try { - if (dataset_id >= 0) { - object_id = H5.H5Ropen_object(dset_data[indx], HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); - object_type = H5.H5Rget_obj_type(dataset_id, HDF5Constants.H5R_OBJECT, dset_data[indx]); + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + + try { + // Get dataspace and allocate memory for read buffer. + dataspace_id = H5.H5Dget_space(dataset_id); + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + + // Read data. + H5.H5Dread(dataset_id, HDF5Constants.H5T_STD_REF, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, + HDF5Constants.H5P_DEFAULT, dset_data); + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(DATASETNAME + "[" + indx + "]:"); + System.out.print(" ->"); + // Open the referenced object, get its name and type. + try { + object_id = H5.H5Ropen_object(dset_data[indx], HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + try { + object_type = H5.H5Rget_obj_type3(dset_data[indx], HDF5Constants.H5R_OBJECT); + String obj_name = null; + if (object_type >= 0) { + // Get the name. + obj_name = H5.H5Iget_name(object_id); + } + if ((object_id >= 0) && (object_type >= -1)) { + switch (H5G_obj.get(object_type)) { + case H5G_GROUP: + System.out.print("H5G_GROUP"); + break; + case H5G_DATASET: + System.out.print("H5G_DATASET"); + break; + case H5G_TYPE: + System.out.print("H5G_TYPE"); + break; + default: + System.out.print("UNHANDLED"); + } + } + // Print the name. + System.out.println(": " + obj_name); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try {H5.H5Oclose(object_id);} catch (Exception e) {} + } + } + catch (Exception e4) { + e4.printStackTrace(); + } + finally { + try {H5.H5Rdestroy(dset_data[indx]);} catch (Exception e4) {} + } + } // end for } - String obj_name = null; - if (object_type >= 0) { - // Get the name. - obj_name = H5.H5Iget_name(object_id); + catch (Exception e3) { + e3.printStackTrace(); } - if ((object_id >= 0) && (object_type >= -1)) { - switch (H5G_obj.get(object_type)) { - case H5G_GROUP: - System.out.print("H5G_GROUP"); - break; - case H5G_DATASET: - System.out.print("H5G_DATASET"); - break; - case H5G_TYPE: - System.out.print("H5G_TYPE"); - break; - default: - System.out.print("UNHANDLED"); - } + finally { + try {H5.H5Sclose(dataspace_id);} catch (Exception e3) {} } - // Print the name. - System.out.println(": " + obj_name); } - catch (Exception e) { - e.printStackTrace(); + catch (Exception e2) { + e2.printStackTrace(); } finally { - try {H5.H5Oclose(object_id);} catch (Exception ex) {} + try {H5.H5Dclose(dataset_id);} catch (Exception e2) {} } } - - // End access to the dataset and release resources used by it. - try { - if (dataspace_id >= 0) - H5.H5Sclose(dataspace_id); - } - catch (Exception e) { - e.printStackTrace(); + catch (Exception e1) { + e1.printStackTrace(); } - - try { - if (dataset_id >= 0) - H5.H5Dclose(dataset_id); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Close the file. - try { - if (file_id >= 0) - H5.H5Fclose(file_id); - } - catch (Exception e) { - e.printStackTrace(); + finally { + try {H5.H5Fclose(file_id);} catch (Exception e1) {} } } diff --git a/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java b/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java index 885b1da..f61ae0d 100644 --- a/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java +++ b/java/examples/datatypes/H5Ex_T_ObjectReferenceAttribute.java @@ -85,7 +85,7 @@ public class H5Ex_T_ObjectReferenceAttribute { // Create dataset with a scalar dataspace. try { dataspace_id = H5.H5Screate(HDF5Constants.H5S_SCALAR); - if (dataspace_id >= 0) { + if ((file_id >= 0) && (dataspace_id >= 0)) { dataset_id = H5.H5Dcreate(file_id, DATASETNAME2, HDF5Constants.H5T_STD_I32LE, dataspace_id, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); if (dataset_id >= 0) @@ -197,7 +197,6 @@ public class H5Ex_T_ObjectReferenceAttribute { e.printStackTrace(); } - // Terminate access to the data space. try { if (dataspace_id >= 0) H5.H5Sclose(dataspace_id); @@ -229,129 +228,96 @@ public class H5Ex_T_ObjectReferenceAttribute { // Open an existing file. try { file_id = H5.H5Fopen(FILENAME, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Open an existing dataset. - try { - if (file_id >= 0) - dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); - } - catch (Exception e) { - e.printStackTrace(); - } - try { - if (dataset_id >= 0) - attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, - HDF5Constants.H5P_DEFAULT); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Get dataspace and allocate memory for read buffer. - try { - if (attribute_id >= 0) - dataspace_id = H5.H5Aget_space(attribute_id); - } - catch (Exception e) { - e.printStackTrace(); - } - - try { - if (dataspace_id >= 0) - H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Read data. - try { - if (attribute_id >= 0) - H5.H5Aread(attribute_id, HDF5Constants.H5T_STD_REF, dset_data); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Output the data to the screen. - for (int indx = 0; indx < dims[0]; indx++) { - System.out.println(ATTRIBUTENAME + "[" + indx + "]:"); - System.out.print(" ->"); - // Open the referenced object, get its name and type. + // Open an existing dataset. try { - if (dataset_id >= 0) { - object_id = H5.H5Ropen_object(dset_data[indx], HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); - object_type = H5.H5Rget_obj_type(dataset_id, HDF5Constants.H5R_OBJECT, dset_data[indx]); + dataset_id = H5.H5Dopen(file_id, DATASETNAME, HDF5Constants.H5P_DEFAULT); + + try { + attribute_id = H5.H5Aopen_by_name(dataset_id, ".", ATTRIBUTENAME, HDF5Constants.H5P_DEFAULT, + HDF5Constants.H5P_DEFAULT); + + // Get dataspace and allocate memory for read buffer. + try { + dataspace_id = H5.H5Aget_space(attribute_id); + H5.H5Sget_simple_extent_dims(dataspace_id, dims, null); + + // Read data. + H5.H5Aread(attribute_id, HDF5Constants.H5T_STD_REF, dset_data); + + // Output the data to the screen. + for (int indx = 0; indx < dims[0]; indx++) { + System.out.println(ATTRIBUTENAME + "[" + indx + "]:"); + System.out.print(" ->"); + // Open the referenced object, get its name and type. + try { + object_id = H5.H5Ropen_object(dset_data[indx], HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); + try { + object_type = H5.H5Rget_obj_type3(dset_data[indx], HDF5Constants.H5R_OBJECT); + String obj_name = null; + if (object_type >= 0) { + // Get the name. + obj_name = H5.H5Iget_name(object_id); + } + if ((object_id >= 0) && (object_type >= -1)) { + switch (H5G_obj.get(object_type)) { + case H5G_GROUP: + System.out.print("H5G_GROUP"); + break; + case H5G_DATASET: + System.out.print("H5G_DATASET"); + break; + case H5G_TYPE: + System.out.print("H5G_TYPE"); + break; + default: + System.out.print("UNHANDLED"); + } + } + // Print the name. + System.out.println(": " + obj_name); + } + catch (Exception e) { + e.printStackTrace(); + } + finally { + try {H5.H5Oclose(object_id);} catch (Exception e) {} + } + } + catch (Exception e5) { + e5.printStackTrace(); + } + finally { + try {H5.H5Rdestroy(dset_data[indx]);} catch (Exception e5) {} + } + } // end for + } + catch (Exception e4) { + e4.printStackTrace(); + } + finally { + try {H5.H5Sclose(dataspace_id);} catch (Exception e3) {} + } } - String obj_name = null; - if (object_type >= 0) { - // Get the name. - obj_name = H5.H5Iget_name(object_id); + catch (Exception e3) { + e3.printStackTrace(); } - if ((object_id >= 0) && (object_type >= -1)) { - switch (H5G_obj.get(object_type)) { - case H5G_GROUP: - System.out.print("H5G_GROUP"); - break; - case H5G_DATASET: - System.out.print("H5G_DATASET"); - break; - case H5G_TYPE: - System.out.print("H5G_TYPE"); - break; - default: - System.out.print("UNHANDLED"); - } + finally { + try {H5.H5Aclose(attribute_id);} catch (Exception e4) {} } - // Print the name. - System.out.println(": " + obj_name); } - catch (Exception e) { - e.printStackTrace(); + catch (Exception e2) { + e2.printStackTrace(); } finally { - try {H5.H5Oclose(object_id);} catch (Exception ex) {} + try {H5.H5Dclose(dataset_id);} catch (Exception e2) {} } } - - // End access to the dataset and release resources used by it. - try { - if (attribute_id >= 0) - H5.H5Aclose(attribute_id); - } - catch (Exception e) { - e.printStackTrace(); + catch (Exception e1) { + e1.printStackTrace(); } - - try { - if (dataset_id >= 0) - H5.H5Dclose(dataset_id); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Terminate access to the data space. - try { - if (dataspace_id >= 0) - H5.H5Sclose(dataspace_id); - } - catch (Exception e) { - e.printStackTrace(); - } - - // Close the file. - try { - if (file_id >= 0) - H5.H5Fclose(file_id); - } - catch (Exception e) { - e.printStackTrace(); + finally { + try {H5.H5Fclose(file_id);} catch (Exception e1) {} } } diff --git a/java/test/TestH5Ocopy.java b/java/test/TestH5Ocopy.java index 8ac73c6..821cad3 100644 --- a/java/test/TestH5Ocopy.java +++ b/java/test/TestH5Ocopy.java @@ -136,18 +136,16 @@ public class TestH5Ocopy { @Test public void testH5OcopyRefsAttr() { long ocp_plist_id = HDF5Constants.H5I_INVALID_HID; - byte[][] dset_data = null; - byte[][] read_data = null; + byte[][] dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; long attribute_id = HDF5Constants.H5I_INVALID_HID; try { - dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; try { dset_data[0] = H5.H5Rcreate_object(H5fid, "/G1", HDF5Constants.H5P_DEFAULT); } catch (Throwable err) { err.printStackTrace(); - fail("testH5OcopyInvalidRef: H5Rcreate_object " + err); + fail("testH5OcopyRefsAttr: H5Rcreate_object " + err); } try { @@ -155,7 +153,7 @@ public class TestH5Ocopy { } catch (Throwable err) { err.printStackTrace(); - fail("testH5OcopyInvalidRef: H5Rcreate_object " + err); + fail("testH5OcopyRefsAttr: H5Rcreate_object " + err); } try { @@ -196,25 +194,30 @@ public class TestH5Ocopy { @Test public void testH5OcopyRefsDatasettodiffFile() { - byte[][] dset_data = null; - byte[][] read_data = null; + byte[][] dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; long ocp_plist_id = HDF5Constants.H5I_INVALID_HID; long dataset_id = HDF5Constants.H5I_INVALID_HID; long H5fid2 = HDF5Constants.H5I_INVALID_HID; try { - dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; + try { + dset_data[0] = H5.H5Rcreate_object(H5fid, "/G1", HDF5Constants.H5P_DEFAULT); + } + catch (Throwable err) { + err.printStackTrace(); + fail("testH5OcopyRefsDatasettodiffFile: H5Rcreate_object " + err); + } try { dset_data[1] = H5.H5Rcreate_object(H5fid, "DS2", HDF5Constants.H5P_DEFAULT); } catch (Throwable err) { err.printStackTrace(); - fail("testH5OcopyInvalidRef: H5Rcreate_object " + err); + fail("testH5OcopyRefsDatasettodiffFile: H5Rcreate_object " + err); } try { dataset_id = H5.H5Dcreate(H5fid, "DSREF", - HDF5Constants.H5T_STD_REF_OBJ, H5dsid, + HDF5Constants.H5T_STD_REF, H5dsid, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT); assertTrue("testH5OcopyRefsDatasettodiffFile.H5Dcreate: ", dataset_id >= 0); H5.H5Dwrite(dataset_id, HDF5Constants.H5T_STD_REF, @@ -233,6 +236,7 @@ public class TestH5Ocopy { ex.printStackTrace(); } finally { + try {H5.H5Rdestroy(dset_data[0]);} catch (Exception ex) {} try {H5.H5Rdestroy(dset_data[1]);} catch (Exception ex) {} } @@ -270,21 +274,20 @@ public class TestH5Ocopy { @Test public void testH5OcopyRefsDatasettosameFile() { - byte[][] dset_data = null; - byte[][] read_data = null; + byte[][] dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; + byte[][] read_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; long ocp_plist_id = HDF5Constants.H5I_INVALID_HID; long dataset_id = HDF5Constants.H5I_INVALID_HID; long did = HDF5Constants.H5I_INVALID_HID; int obj_type = -1; try { - dset_data = new byte[2][HDF5Constants.H5R_REF_BUF_SIZE]; try { dset_data[0] = H5.H5Rcreate_object(H5fid, "/G1", HDF5Constants.H5P_DEFAULT); } catch (Throwable err) { err.printStackTrace(); - fail("testH5OcopyInvalidRef: H5Rcreate_object " + err); + fail("testH5OcopyRefsDatasettosameFile: H5Rcreate_object " + err); } try { @@ -292,7 +295,7 @@ public class TestH5Ocopy { } catch (Throwable err) { err.printStackTrace(); - fail("testH5OcopyInvalidRef: H5Rcreate_object " + err); + fail("testH5OcopyRefsDatasettosameFile: H5Rcreate_object " + err); } try { @@ -308,9 +311,11 @@ public class TestH5Ocopy { H5.H5Dclose(dataset_id); } catch (Exception ex) { - try {H5.H5Dclose(dataset_id);} catch (Exception exx) {} fail("testH5OcopyRefsDatasettosameFile: create dataset failed"); } + finally { + try {H5.H5Dclose(dataset_id);} catch (Exception exx) {} + } } catch (Exception ex) { ex.printStackTrace(); @@ -324,49 +329,63 @@ public class TestH5Ocopy { ocp_plist_id = H5.H5Pcreate(HDF5Constants.H5P_OBJECT_COPY); assertTrue("testH5OcopyRefsDatasettosameFile.H5Pcreate: ", ocp_plist_id >= 0); H5.H5Pset_copy_object(ocp_plist_id, HDF5Constants.H5O_COPY_EXPAND_REFERENCE_FLAG); + //Perform copy function. + try { + H5.H5Ocopy(H5fid, "DSREF", H5fid, "CPYREFD", ocp_plist_id, HDF5Constants.H5P_DEFAULT); + } + catch(Exception ex) { + fail("testH5OcopyRefsDatasettosameFile: H5Ocopy failed"); + } } catch (Exception ex) { - try {H5.H5Pclose(ocp_plist_id);} catch (Exception exx) {} fail("testH5OcopyRefsDatasettosameFile: H5Pset_copy_object failed"); } - - //Perform copy function. - try { - H5.H5Ocopy(H5fid, "DSREF", H5fid, "CPYREFD", ocp_plist_id, HDF5Constants.H5P_DEFAULT); - } - catch(Exception ex) { + finally { try {H5.H5Pclose(ocp_plist_id);} catch (Exception exx) {} - fail("testH5OcopyRefsDatasettosameFile: H5Ocopy failed"); } - //Open the dataset that has been copied try { - did = H5.H5Dopen(H5fid, "DSREF", HDF5Constants.H5P_DEFAULT); - assertTrue("testH5OcopyRefsDatasettosameFile.H5Dopen: ", did >= 0); + //Open the dataset that has been copied + try { + did = H5.H5Dopen(H5fid, "DSREF", HDF5Constants.H5P_DEFAULT); + assertTrue("testH5OcopyRefsDatasettosameFile.H5Dopen: ", did >= 0); + } + catch (Exception e) { + e.printStackTrace(); + fail("testH5OcopyRefsDatasettosameFile: H5Dopen failed"); + } + + //Read the dataset object references in the read_data buffer. + try { + H5.H5Dread(did, HDF5Constants.H5T_STD_REF, HDF5Constants.H5S_ALL,HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, read_data); + } + catch (Exception e) { + e.printStackTrace(); + fail("testH5OcopyRefsDatasettosameFile: H5Dread failed"); + } } - catch (Exception e) { - try {H5.H5Dclose(did);} catch (Exception exx) {} - e.printStackTrace(); - fail("testH5OcopyRefsDatasettosameFile: H5Dopen failed"); + catch (Exception ex) { + ex.printStackTrace(); + fail("testH5OcopyRefsDatasettosameFile: open and read dataset failed"); + } + finally { + try {H5.H5Dclose(did);} catch (Exception ex) {} } try { - //Read the dataset object references in the read_data buffer. - H5.H5Dread(did, HDF5Constants.H5T_STD_REF, HDF5Constants.H5S_ALL,HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, read_data); - //Get the type of object the reference points to. - obj_type = H5.H5Rget_obj_type(H5fid, HDF5Constants.H5R_OBJECT, read_data[1]); + obj_type = H5.H5Rget_obj_type3(read_data[1], HDF5Constants.H5R_OBJECT); assertEquals(obj_type, HDF5Constants.H5O_TYPE_DATASET); - - obj_type = H5.H5Rget_obj_type(H5fid, HDF5Constants.H5R_OBJECT, read_data[0]); + + obj_type = H5.H5Rget_obj_type3(read_data[0], HDF5Constants.H5R_OBJECT); assertEquals(obj_type, HDF5Constants.H5O_TYPE_GROUP); } catch (Exception ex) { ex.printStackTrace(); } finally { - try {H5.H5Dclose(did);} catch (Exception ex) {} - try {H5.H5Pclose(ocp_plist_id);} catch (Exception ex) {} + try {H5.H5Rdestroy(read_data[1]);} catch (Exception ex) {} + try {H5.H5Rdestroy(read_data[0]);} catch (Exception ex) {} } } |