use of org.bytedeco.javacpp.BytePointer in project deeplearning4j by deeplearning4j.
the class Hdf5Archive method readAttributeAsJson.
/**
* Read JSON-formatted string attribute.
*
* @param attribute HDF5 attribute to read as JSON formatted string.
* @return
* @throws UnsupportedKerasConfigurationException
*/
private String readAttributeAsJson(hdf5.Attribute attribute) throws UnsupportedKerasConfigurationException {
hdf5.VarLenType vl = attribute.getVarLenType();
int bufferSizeMult = 1;
String s = null;
/* TODO: find a less hacky way to do this.
* Reading variable length strings (from attributes) is a giant
* pain. There does not appear to be any way to determine the
* length of the string in advance, so we use a hack: choose a
* buffer size and read the config. If Jackson fails to parse
* it, then we must not have read the entire config. Increase
* buffer and repeat.
*/
while (true) {
byte[] attrBuffer = new byte[bufferSizeMult * 2000];
BytePointer attrPointer = new BytePointer(attrBuffer);
attribute.read(vl, attrPointer);
attrPointer.get(attrBuffer);
s = new String(attrBuffer);
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
try {
mapper.readTree(s);
break;
} catch (IOException e) {
}
bufferSizeMult++;
if (bufferSizeMult > 100) {
throw new UnsupportedKerasConfigurationException("Could not read abnormally long HDF5 attribute");
}
}
return s;
}
Aggregations