Search in sources :

Example 6 with BytePointer

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;
}
Also used : BytePointer(org.bytedeco.javacpp.BytePointer) org.bytedeco.javacpp.hdf5(org.bytedeco.javacpp.hdf5) IOException(java.io.IOException) ObjectMapper(org.nd4j.shade.jackson.databind.ObjectMapper)

Aggregations

BytePointer (org.bytedeco.javacpp.BytePointer)6 DoublePointer (org.bytedeco.javacpp.DoublePointer)4 IntPointer (org.bytedeco.javacpp.IntPointer)4 ByteBuffer (java.nio.ByteBuffer)3 FloatPointer (org.bytedeco.javacpp.FloatPointer)3 PointerPointer (org.bytedeco.javacpp.PointerPointer)3 ShortPointer (org.bytedeco.javacpp.ShortPointer)3 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 ShortBuffer (java.nio.ShortBuffer)2 IOException (java.io.IOException)1 Buffer (java.nio.Buffer)1 LongBuffer (java.nio.LongBuffer)1 LongPointer (org.bytedeco.javacpp.LongPointer)1 Pointer (org.bytedeco.javacpp.Pointer)1 org.bytedeco.javacpp.hdf5 (org.bytedeco.javacpp.hdf5)1 ObjectMapper (org.nd4j.shade.jackson.databind.ObjectMapper)1