使用HDF.PInvoke和C#,我希望将数据存储在HDF5文件中,但我不想立即将该文件写入磁盘。相反,我希望将文件作为字节数组传递给另一个类,该类处理与存储介质的交互。
据我所知,这在HDF核心驱动程序中是可能的,因为它允许您在内存中创建HDF文件,而无需将其写入磁盘。
这就是我被困的地方
public byte[] ConvertToHDF(object data)
{
long fileID;
// create HDF5 file, continuously growing in 0x1000 byte chunks
{
long propertyListFileAccess = H5P.create(H5P.FILE_ACCESS);
H5P.set_fapl_core(propertyListFileAccess, (IntPtr)0x1000, 0);
fileID = H5F.create("temporaryHDF", H5F.ACC_TRUNC,
H5P.DEFAULT, propertyListFileAccess);
H5P.close(propertyListFileAccess);
}
// store the data in the HDF file
StoreHDF(fileID, data);
// create byte array of fitting size
ulong size = 0;
H5F.get_filesize(fileID, ref size);
byte[] binary = new byte[size];
// fill binary with the HDF file
...
return binary;
}那么,是否有一种方法可以将HDF文件保存在字节数组中而不将其存储在磁盘上?一个解决方案,我只有一个块HDF文件,并可以简单地使用它,而不是复制到字节数组将更好。
发布于 2018-02-05 08:44:53
我找到了解决问题的办法。
此外,获取问题中字节数组的适当大小的方法是不准确的,它可能导致数组大于所需。下面这个是精确的。
下面的代码用于替换// create byte array of fitting size和// fill binary with the HDF file下面的代码
byte[] fileInRAM;
// make sure the HDF file is up to date
H5F.flush(fileID, H5F.scope_t.GLOBAL);
// retrieve copy H5F
{
// create properly sized byte array for the file
IntPtr sizePointer = (IntPtr)null;
sizePointer = H5F.get_file_image(fileID, (IntPtr)null, ref sizePointer);
fileInRAM = new byte[(int)sizePointer];
// tell Garbage Collector to leave the byte array where it is
GCHandle handle = GCHandle.Alloc(fileInRAM, GCHandleType.Pinned);
IntPtr filePointer = handle.AddrOfPinnedObject();
// get file image
H5F.get_file_image(fileID, filePointer, ref sizePointer);
// allow the Garbage Collector to do its work again
handle.Free();
}来源:https://support.hdfgroup.org/HDF5/doc/Advanced/FileImageOperations/HDF5FileImageOperations.pdf
https://stackoverflow.com/questions/48547010
复制相似问题