Skip to content

Commit 37ab25f

Browse files
committed
Updates for v2 arch
1 parent 0017fee commit 37ab25f

2 files changed

Lines changed: 17 additions & 52 deletions

File tree

src/main/java/org/hdf5javalib/examples/hdf5examples/HDF5Debug.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ private void run() {
5454
try {
5555
// List all .h5 files in HDF5Examples resources directory
5656
// ATL03_20250302235544_11742607_006_01
57-
// Path dirPath = Paths.get(Objects.requireNonNull(HDF5Debug.class.getClassLoader().getResource("HDF5Examples/h5ex_g_compact2.h5")).toURI());
58-
Path dirPath = Paths.get("c:/users/karln/Downloads/ATL03_20250302235544_11742607_006_01.h5");
57+
Path dirPath = Paths.get(Objects.requireNonNull(HDF5Debug.class.getClassLoader().getResource("HDF5Examples/h5ex_g_compact2.h5")).toURI());
58+
// Path dirPath = Paths.get("c:/users/karln/Downloads/ATL03_20250302235544_11742607_006_01.h5");
5959
processFile(dirPath);
6060
} catch (Exception e) {
6161
throw new IllegalStateException(e);

src/main/java/org/hdf5javalib/hdfjava/HdfFileReader.java

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ private void processV2GroupLinks(HdfGroup group) throws Exception {
243243

244244
// 2. Process links stored directly as Link Messages in the header (for smaller, compact groups)
245245
for (HdfMessage hdfMessage : group.getObjectHeader().getHeaderMessages()) {
246-
if (hdfMessage instanceof LinkMessage) {
247-
LinkMessage linkMessage = (LinkMessage) hdfMessage;
246+
if (hdfMessage instanceof LinkMessage linkMessage) {
248247
long objectHeaderOffset = linkMessage.getLinkInformation().getInstance(Long.class);
249248
String linkName = linkMessage.getLinkName();
250249
processLink(group, linkName, objectHeaderOffset);
@@ -289,7 +288,7 @@ private void processLink(HdfGroup parentGroup, String linkName, long objectHeade
289288
String hardLink = isHardLink(parentGroup, objectHeaderOffset);
290289
HdfObjectHeaderPrefix objectHeader = null;
291290
if (hardLink == null) {
292-
objectHeader = readObjectHeaderPrefixFromSeekableByteChannel(fileChannel, objectHeaderOffset, this, linkName);
291+
objectHeader = readObjectHeaderPrefixFromSeekableByteChannel(fileChannel, objectHeaderOffset, this);
293292
}
294293

295294
// A node is a dataset if it has a DataLayoutMessage, otherwise it's a group.
@@ -310,19 +309,6 @@ private void processLink(HdfGroup parentGroup, String linkName, long objectHeade
310309
}
311310
}
312311

313-
/*
314-
*
315-
* The original, unchanged helper methods from your code would go here.
316-
* For example:
317-
*
318-
* private static long findSuperblockOffset(SeekableByteChannel fileChannel) throws IOException { ... }
319-
*
320-
* private String isHardLink(HdfGroup parentGroup, Long objectHeaderAddress) { ... }
321-
*
322-
* // ...and any other custom reader methods like readV2ObjectHeader, readObjectHeader, etc.
323-
*
324-
*/
325-
326312
private static long findSuperblockOffset(SeekableByteChannel fileChannel) throws IOException {
327313
long size = fileChannel.size();
328314
long offset = 0;
@@ -374,7 +360,7 @@ public List<HdfDataset> getDatasets() {
374360
// Convert Iterator to List using a for loop
375361
List<HdfDataset> resultList = new ArrayList<>();
376362
Iterator<HdfDataset> iterator = datasetIterator();
377-
for (; iterator.hasNext(); ) {
363+
while (iterator.hasNext()) {
378364
resultList.add(iterator.next());
379365
}
380366
return resultList;
@@ -383,6 +369,7 @@ public List<HdfDataset> getDatasets() {
383369
public Optional<HdfDataset> getDataset(String path) {
384370
return getHdfDataObject(path, HdfDataset.class);
385371
}
372+
386373
public Optional<HdfGroup> getGroup(String path) {
387374
return getHdfDataObject(path, HdfGroup.class);
388375
}
@@ -494,9 +481,10 @@ public static HdfSuperblock readSuperblockFromSeekableByteChannel(SeekableByteCh
494481
HdfFixedPoint endOfFileAddress = null;
495482
HdfFixedPoint driverInformationAddress = null;
496483
HdfFixedPoint rootObjectHeaderAddress = null;
484+
buffer.position(SIGNATURE_SIZE + VERSION_SIZE); // Skip the file signature
485+
// Skip the file signature
497486
if ( version < 2) {
498487
// Step 4: Parse the remaining superblock fields
499-
buffer.position(SIGNATURE_SIZE + VERSION_SIZE); // Skip the file signature
500488
freeSpaceVersion = Byte.toUnsignedInt(buffer.get());
501489
rootGroupVersion = Byte.toUnsignedInt(buffer.get());
502490
buffer.get(); // Skip reserved
@@ -525,7 +513,6 @@ public static HdfSuperblock readSuperblockFromSeekableByteChannel(SeekableByteCh
525513
driverInformationAddress = HdfReadUtils.readHdfFixedPointFromBuffer(fixedPointDatatypeForOffset, buffer);
526514
} else {
527515
// Step 4: Parse the remaining superblock fields
528-
buffer.position(SIGNATURE_SIZE + VERSION_SIZE); // Skip the file signature
529516
offsetSize = Byte.toUnsignedInt(buffer.get());
530517
lengthSize = Byte.toUnsignedInt(buffer.get());
531518
int fileConsistencyFlags = Byte.toUnsignedInt(buffer.get()); // Skip reserved
@@ -745,7 +732,7 @@ public static HdfLocalHeap readLocalHeapFromSeekableByteChannel(
745732
buffer.get(signatureBytes);
746733
// String signature = new String(signatureBytes);
747734
if (Arrays.compare(LOCAL_HEAP_SIGNATURE, signatureBytes) != 0) {
748-
throw new IllegalArgumentException("Invalid heap signature: " + signatureBytes);
735+
throw new IllegalArgumentException("Invalid heap signature: " + Arrays.toString(signatureBytes));
749736
}
750737

751738
int version = Byte.toUnsignedInt(buffer.get());
@@ -757,7 +744,7 @@ public static HdfLocalHeap readLocalHeapFromSeekableByteChannel(
757744
HdfFixedPoint dataSegmentAddress = HdfReadUtils.readHdfFixedPointFromBuffer(hdfDataFile.getSuperblock().getFixedPointDatatypeForOffset(), buffer);
758745

759746
HdfLocalHeapData hdfLocalHeapData = readLocalHeapDataFromSeekableByteChannel(
760-
fileChannel, dataSegmentSize, freeListOffset, dataSegmentAddress, hdfDataFile);
747+
fileChannel, dataSegmentSize, freeListOffset, dataSegmentAddress);
761748

762749
return new HdfLocalHeap(version,hdfDataFile, hdfLocalHeapData);
763750
}
@@ -766,8 +753,7 @@ public static HdfLocalHeapData readLocalHeapDataFromSeekableByteChannel(
766753
SeekableByteChannel fileChannel,
767754
HdfFixedPoint dataSegmentSize,
768755
HdfFixedPoint freeListOffset,
769-
HdfFixedPoint dataSegmentAddress,
770-
HdfDataFile hdfDataFile
756+
HdfFixedPoint dataSegmentAddress
771757
) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException {
772758

773759
Map<HdfFixedPoint, HdfLocalHeapDataValue> data = new LinkedHashMap<>();
@@ -832,7 +818,7 @@ public static HdfSymbolTableEntry readSteFromSeekableByteChannel(
832818

833819
HdfSymbolTableEntryCache cache;
834820
if (cacheType == 0) {
835-
cache = readCacheNoScratchFromSeekableByteChannel(fileChannel, hdfDataFile);
821+
cache = readCacheNoScratchFromSeekableByteChannel(fileChannel);
836822
} else if (cacheType == 1) {
837823
cache = readCacheWithScratchFromSeekableByteChannel(fileChannel, hdfDataFile);
838824
} else {
@@ -843,8 +829,7 @@ public static HdfSymbolTableEntry readSteFromSeekableByteChannel(
843829
}
844830

845831
public static HdfSymbolTableEntryCacheNoScratch readCacheNoScratchFromSeekableByteChannel(
846-
SeekableByteChannel fileChannel,
847-
HdfDataFile hdfDataFile
832+
SeekableByteChannel fileChannel
848833
) throws IOException {
849834
HdfReadUtils.skipBytes(fileChannel, SYMBOL_TABLE_ENTRY_SCRATCH_SIZE); // Skip 16 bytes for scratch-pad
850835
// return new HdfSymbolTableEntryCacheNoScratch(hdfDataFile, objectHeader, objectName);
@@ -867,14 +852,8 @@ public static HdfSymbolTableEntryCacheNoScratch readCacheNoScratchFromSeekableBy
867852
public static HdfObjectHeaderPrefix readObjectHeaderPrefixFromSeekableByteChannel(
868853
SeekableByteChannel fileChannel,
869854
long objectHeaderAddress,
870-
HdfDataFile hdfDataFile,
871-
String objectName
855+
HdfDataFile hdfDataFile
872856
) throws Exception {
873-
// long offset = fileChannel.position();
874-
// ByteBuffer buffer = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN); // Buffer for the fixed-size header
875-
// fileChannel.read(buffer);
876-
// buffer.flip();
877-
878857
// The first part of the header is 6 bytes: Signature (4) + Version (1) + Flags (1)
879858
ByteBuffer headerStartBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
880859
fileChannel.position(objectHeaderAddress);
@@ -883,7 +862,6 @@ public static HdfObjectHeaderPrefix readObjectHeaderPrefixFromSeekableByteChanne
883862
HdfObjectHeaderPrefix objectHeader;
884863
if (Arrays.equals(headerStartBuffer.array(), HdfObjectHeaderPrefixV2.OBJECT_HEADER_MESSAGE_SIGNATURE)) {
885864
objectHeader = readV2ObjectHeader(fileChannel, objectHeaderAddress, hdfDataFile);
886-
// readV2Arch(fileChannel, hdfDataFile, objectHeader);
887865
} else {
888866
int version = Byte.toUnsignedInt(headerStartBuffer.get());
889867
if( version > 1 ) {
@@ -892,19 +870,6 @@ public static HdfObjectHeaderPrefix readObjectHeaderPrefixFromSeekableByteChanne
892870
objectHeader = readObjectHeader(fileChannel, objectHeaderAddress, hdfDataFile);
893871
}
894872

895-
// // Parse Version (1 byte)
896-
// int version = Byte.toUnsignedInt(buffer.get());
897-
// if ( version == 1 ) {
898-
// return readObjectHeader(fileChannel, offset, hdfDataFile);
899-
// } else {
900-
// buffer.rewind();
901-
// byte[] signature = new byte[HdfObjectHeaderPrefixV2.OBJECT_HEADER_MESSAGE_SIGNATURE.length];
902-
// buffer.get(signature);
903-
// if (Arrays.compare(signature, HdfObjectHeaderPrefixV2.OBJECT_HEADER_MESSAGE_SIGNATURE) != 0) {
904-
// throw new IllegalStateException("Object header signature mismatch");
905-
// }
906-
// return readObjectHeader(fileChannel, offset, hdfDataFile);
907-
// }
908873
return objectHeader;
909874
}
910875

@@ -971,7 +936,7 @@ static HdfObjectHeaderPrefixV2 readV2ObjectHeader(SeekableByteChannel fileChanne
971936
// Verify Signature ("OHDR")
972937
byte[] signatureBytes = new byte[4];
973938
headerStartBuffer.get(signatureBytes);
974-
String signature = new String(signatureBytes, "ASCII");
939+
String signature = new String(signatureBytes, StandardCharsets.US_ASCII);
975940
if (!"OHDR".equals(signature)) {
976941
throw new IOException("Invalid HDF5 Object Header V2 signature. Expected 'OHDR', found '" + signature + "' at offset " + objectHeaderAddress);
977942
}
@@ -1152,7 +1117,7 @@ public static void printRows(byte[] input) {
11521117
if (b == 0) break; // Stop at null terminator
11531118
secondString.append((char) (b & 0xFF));
11541119
}
1155-
System.out.println(secondString.toString());
1120+
System.out.println(secondString);
11561121

11571122
// Third row: 16 bytes, print as Arrays.toString
11581123
int thirdRowStart = stringStart + paddedLength;
@@ -1165,7 +1130,7 @@ public static void printRows(byte[] input) {
11651130
for (int i = fourthRowStart; i < input.length && input[i] != 0; i++) {
11661131
fourthString.append((char) (input[i] & 0xFF));
11671132
}
1168-
System.out.println(fourthString.toString());
1133+
System.out.println(fourthString);
11691134
}
11701135

11711136

0 commit comments

Comments
 (0)