Reading one variable-length string field of compound dataset with Java API


#1

Hi,
I have a compound dataset with a variable number of double-fields and string-fields (variable length strings):

Compound:
–> “Double1”
–> “Double2”
–> …
–> “String1”
–> “String2”
–> …

I’m trying to read only one specific String-field with Java API:

String fileName = "file.hdf5";
String datasetName = "Data/CompDataSet";
String colName = "String1";

long fileId = H5.H5Fopen(fileName, HDF5Constants.H5F_ACC_RDONLY, HDF5Constants.H5P_DEFAULT);
long datasetId = H5.H5Dopen(fileId , datasetName, HDF5Constants.H5P_DEFAULT);

long varStrType = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(varStrType, HDF5Constants.H5T_VARIABLE);
H5.H5Tset_strpad(varStrType, HDF5Constants.H5T_STR_NULLTERM);
H5.H5Tset_cset(varStrType, HDF5Constants.H5T_CSET_ASCII);
long memtypeVar = H5.H5Tcreate(HDF5Constants.H5T_COMPOUND, H5.H5Tget_size(varStrType));
H5.H5Tinsert(memtypeVar, colName, 0, varStrType);

String[] data = new String[460];    // 460 number of Rows
H5.H5Dread(datasetId, memtypeVar, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, data );

The strings I get have random characters and length though the strings in the file are in this case all the same two characters “ab”.
Usually I’m using C/C++ API and in C++ I can read one variable-length string field in a similar way as shown above.
Instead of
String[] data = new String[460];
I’m using
char *data[460];

I know the examples of read/write compound and read/write variable length string. But I can’t get it together.
https://confluence.hdfgroup.org/display/HDF5/Examples+by+API

Can someone help?
Thanks


#2

I was able to solve it:
I’m using HDF5 v1.10.0
From version 1.10.1 on there is a function H5DreadVL, which is working like a charm.