private String readAttributeAsString ( Attribute attribute ) throws UnsupportedKerasConfigurationException { synchronized ( Hdf5Archive . LOCK_OBJECT ) { 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, increase buffer and repeat * until the buffer ends with \u0000 */ while ( true ) { byte [ ] attrBuffer = new byte [ bufferSizeMult * 2000 ] ; BytePointer attrPointer = new BytePointer ( attrBuffer ) ; attribute . read ( vl , attrPointer ) ; attrPointer . get ( attrBuffer ) ; s = new String ( attrBuffer ) ; if ( s . endsWith ( "\u0000" ) ) { s = s . replace ( "\u0000" , "" ) ; break ; } bufferSizeMult ++ ; if ( bufferSizeMult > 1000 ) { throw new UnsupportedKerasConfigurationException ( "Could not read abnormally long HDF5 attribute" ) ; } } vl . deallocate ( ) ; return s ; } }
Read attribute as string .