When 0s are in the data, I find I must use a ShortPointer to get the full data for some reason. I'm not sure why. The top test case passes, native bytes and be written and read correctly, the second test fails.
import org.bytedeco.hdf5.*;
import org.bytedeco.hdf5.global.hdf5;
import org.bytedeco.javacpp.*;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.nio.ByteOrder;
import java.util.Arrays;
public class HDF5TestForReport
{
static
{
System.out.println(String.format("Native byte order: %s", ByteOrder.nativeOrder()));
}
@Test // Passes
public void testBytesWithoutZeros()
{
String filePath = "NativeBytesWithoutZeros.hdf5";
H5File h5File = new H5File(filePath, hdf5.H5F_ACC_TRUNC);
String datasetId = "bytes";
byte[] writeData = new byte[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'f',
-2, -1, 1, 2, 3, 4, 5, Byte.MIN_VALUE, Byte.MAX_VALUE };
BytePointer dataPointer = new BytePointer(writeData);
int rank = 1;
long[] dimensions = { dataPointer.limit() };
DataSpace fileSpace = new DataSpace(rank, dimensions);
DataType dataType = new DataType(PredType.NATIVE_B8());
DataSet dataSet = h5File.createDataSet(datasetId, dataType, fileSpace);
dataSet.write(dataPointer, dataType);
dataSet.close();
fileSpace.close();
h5File.close();
h5File = new H5File(filePath, hdf5.H5F_ACC_RDONLY);
dataSet = h5File.openDataSet(datasetId);
dataPointer = new BytePointer(writeData.length);
dataSet.read(dataPointer, dataType);
byte[] readData = new byte[(int) dataPointer.limit()];
dataPointer.get(readData);
dataSet.close();
h5File.close();
System.out.printf("Wrote: %s%n", Arrays.toString(writeData));
System.out.printf("Read: %s%n", Arrays.toString(readData));
Assertions.assertArrayEquals(writeData, readData);
}
@Test
public void testBytesWithZeros()
{
String filePath = "NativeBytesWithZeros.hdf5";
H5File h5File = new H5File(filePath, hdf5.H5F_ACC_TRUNC);
String datasetId = "bytes";
// There is a 0 in here that causes the problem
byte[] writeData = new byte[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'f',
0, -2, -1, 1, 0, 2, 3, 4, Byte.MIN_VALUE, Byte.MAX_VALUE };
BytePointer dataPointer = new BytePointer(writeData);
byte[] beforeWriteData = new byte[(int) dataPointer.limit()];
dataPointer.get(beforeWriteData);
System.out.printf("Before: %s%n", Arrays.toString(beforeWriteData));
System.out.printf("Writing %s bytes%n", writeData.length);
int rank = 1;
long[] dimensions = { dataPointer.limit() };
DataSpace fileSpace = new DataSpace(rank, dimensions);
DataType dataType = new DataType(PredType.NATIVE_B8());
DataSet dataSet = h5File.createDataSet(datasetId, dataType, fileSpace);
dataSet.write(dataPointer, dataType);
System.out.printf("Wrote: %s%n", Arrays.toString(writeData));
dataSet.close();
fileSpace.close();
h5File.close();
h5File = new H5File(filePath, hdf5.H5F_ACC_RDONLY);
dataSet = h5File.openDataSet(datasetId);
// Workaround wrapping with ShortPointer
dataPointer = new BytePointer(writeData.length);
dataSet.read(new ShortPointer(dataPointer), dataType);
System.out.printf("Read %s bytes%n", dataPointer.limit());
byte[] workaroundReadData = new byte[(int) dataPointer.limit()];
dataPointer.get(workaroundReadData);
System.out.printf("Read: %s%n", Arrays.toString(workaroundReadData));
// I would expect this to work, but it stops before the 0 value
dataPointer = new BytePointer(writeData.length);
dataSet.read(dataPointer, dataType); // <-- Problem line
System.out.printf("Read %s bytes%n", dataPointer.limit());
byte[] readData = new byte[(int) dataPointer.limit()];
dataPointer.get(readData);
System.out.printf("Read: %s%n", Arrays.toString(readData));
dataSet.close();
h5File.close();
Assertions.assertArrayEquals(writeData, workaroundReadData); // Passes
Assertions.assertArrayEquals(writeData, readData); // Fails
}
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4