Skip to content

Commit 5f42cfb

Browse files
committed
refactor v2 code
1 parent fdfbfb8 commit 5f42cfb

2 files changed

Lines changed: 38 additions & 51 deletions

File tree

src/main/java/org/hdf5javalib/hdffile/dataobjects/messages/DataspaceMessage.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ public static HdfMessage parseHeaderMessage(int flags, byte[] data, HdfDataFile
106106
int dimensionality = Byte.toUnsignedInt(buffer.get());
107107
int flagByte = Byte.toUnsignedInt(buffer.get());
108108
BitSet flagSet = BitSet.valueOf(new byte[]{(byte) flagByte});
109-
110-
buffer.position(buffer.position() + DATASPACE_MESSAGE_RESERVED_1);
111-
109+
if (version == 1) {
110+
buffer.position(buffer.position() + DATASPACE_MESSAGE_RESERVED_1);
111+
} else {
112+
int type = Byte.toUnsignedInt(buffer.get());
113+
}
112114
HdfFixedPoint[] dimensions = new HdfFixedPoint[dimensionality];
113115
for (int i = 0; i < dimensionality; i++) {
114116
dimensions[i] = HdfReadUtils.readHdfFixedPointFromBuffer(hdfDataFile.getSuperblock().getFixedPointDatatypeForLength(), buffer);
@@ -124,6 +126,8 @@ public static HdfMessage parseHeaderMessage(int flags, byte[] data, HdfDataFile
124126
}
125127

126128
return new DataspaceMessage(version, dimensionality, flagSet, dimensions, maxDimensions, hasMaxDimensions, flags, data.length);
129+
130+
127131
}
128132

129133
/**

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

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -117,54 +117,53 @@ public HdfFileReader readFile() throws Exception {
117117
fileChannel.read(headerStartBuffer);
118118
headerStartBuffer.flip();
119119

120+
HdfGroup rootGroup;
120121
if (Arrays.equals(headerStartBuffer.array(), HdfObjectHeaderPrefixV2.OBJECT_HEADER_MESSAGE_SIGNATURE)) {
121122
// The object header has a V2 signature ('OHDR').
122-
readV2Structure(rootObjectHeaderAddr);
123+
// The signature check was already performed in readFile(), so we can proceed directly.
124+
HdfObjectHeaderPrefix rootObjectHeader = readV2ObjectHeader(fileChannel, rootObjectHeaderAddr, this);
125+
rootGroup = new HdfGroup("", rootObjectHeader, null, null);
126+
127+
this.bTree = new HdfTree(rootGroup);
128+
123129
} else {
124130
// The object header does not have a V2 signature, so we treat it as V1.
125131
// This case should only happen with V1 superblocks.
126-
if (superblock.getVersion() >= 2) {
127-
throw new IllegalStateException("HDF5 format inconsistency: Superblock is V2+ but root object header is not.");
132+
// The version check is now more robust. We use the buffer that was already read.
133+
int version = Byte.toUnsignedInt(headerStartBuffer.get(0));
134+
if (version > 1) {
135+
// The character 'O' has ASCII value 79. This error indicates we are incorrectly trying to parse
136+
// a V2 'OHDR' signature as a V1 header version.
137+
throw new UnsupportedOperationException("Unsupported V1 Object Header version: " + version);
128138
}
129-
readV1Structure(rootObjectHeaderAddr, rootGroupSTE, headerStartBuffer);
130-
}
131139

132-
return this;
133-
}
140+
HdfObjectHeaderPrefix rootObjectHeader = readObjectHeader(fileChannel, rootObjectHeaderAddr, this);
134141

135-
// --- V1 Architecture Reading Logic ---
142+
rootGroup = new HdfGroup("", rootObjectHeader, null, null);
143+
144+
this.bTree = new HdfTree(rootGroup);
136145

137-
/**
138-
* Handles the reading and parsing of HDF5 v1 file structures.
139-
* @param rootObjectHeaderAddr The address of the root object header.
140-
* @param rootGroupSTE The root group's Symbol Table Entry.
141-
* @param headerStartBuffer The already-read first few bytes of the header, containing the version.
142-
*/
143-
private void readV1Structure(long rootObjectHeaderAddr, HdfSymbolTableEntry rootGroupSTE, ByteBuffer headerStartBuffer) throws Exception {
144-
// The version check is now more robust. We use the buffer that was already read.
145-
int version = Byte.toUnsignedInt(headerStartBuffer.get(0));
146-
if (version > 1) {
147-
// The character 'O' has ASCII value 79. This error indicates we are incorrectly trying to parse
148-
// a V2 'OHDR' signature as a V1 header version.
149-
throw new UnsupportedOperationException("Unsupported V1 Object Header version: " + version);
150146
}
151147

152-
HdfObjectHeaderPrefix rootObjectHeader = readObjectHeader(fileChannel, rootObjectHeaderAddr, this);
148+
if (superblock.getVersion() < 2) {
149+
// Extract V1-specific metadata (heap and B-Tree addresses)
150+
long heapOffset = ((HdfSymbolTableEntryCacheWithScratch) rootGroupSTE.getCache()).getLocalHeapAddress().getInstance(Long.class);
151+
long bTreeAddress = ((HdfSymbolTableEntryCacheWithScratch) rootGroupSTE.getCache()).getbTreeAddress().getInstance(Long.class);
153152

154-
// Extract V1-specific metadata (heap and B-Tree addresses)
155-
long heapOffset = ((HdfSymbolTableEntryCacheWithScratch) rootGroupSTE.getCache()).getLocalHeapAddress().getInstance(Long.class);
156-
long bTreeAddress = ((HdfSymbolTableEntryCacheWithScratch) rootGroupSTE.getCache()).getbTreeAddress().getInstance(Long.class);
153+
HdfLocalHeap localHeap = readLocalHeapFromSeekableByteChannel(fileChannel, heapOffset, this);
154+
HdfBTreeV1ForGroup groupBTree = readBTreeFromSeekableByteChannelForGroups(fileChannel, bTreeAddress, this);
157155

158-
HdfLocalHeap localHeap = readLocalHeapFromSeekableByteChannel(fileChannel, heapOffset, this);
159-
HdfBTreeV1ForGroup groupBTree = readBTreeFromSeekableByteChannelForGroups(fileChannel, bTreeAddress, this);
156+
String rootGroupName = localHeap.stringAtOffset(rootGroupSTE.getLinkNameOffset());
157+
readV1GroupHierarchy(rootGroup, localHeap, groupBTree);
158+
} else {
159+
processV2GroupLinks(rootGroup);
160+
}
160161

161-
String rootGroupName = localHeap.stringAtOffset(rootGroupSTE.getLinkNameOffset());
162-
HdfGroup rootGroup = new HdfGroup(rootGroupName, rootObjectHeader, null, null);
162+
return this;
163+
}
163164

164-
this.bTree = new HdfTree(rootGroup);
165+
// --- V1 Architecture Reading Logic ---
165166

166-
readV1GroupHierarchy(rootGroup, localHeap, groupBTree);
167-
}
168167

169168
/**
170169
* Recursively traverses the V1 B-Tree and Symbol Table Nodes to build the group/dataset hierarchy.
@@ -220,22 +219,6 @@ private void readV1GroupHierarchy(HdfGroup parentGroup, HdfLocalHeap localHeap,
220219
}
221220

222221

223-
// --- V2 Architecture Reading Logic ---
224-
225-
/**
226-
* Handles the reading and parsing of HDF5 v2 file structures.
227-
* @param rootObjectHeaderAddr The address of the root object header.
228-
*/
229-
private void readV2Structure(long rootObjectHeaderAddr) throws Exception {
230-
// The signature check was already performed in readFile(), so we can proceed directly.
231-
HdfObjectHeaderPrefix rootObjectHeader = readV2ObjectHeader(fileChannel, rootObjectHeaderAddr, this);
232-
HdfGroup rootGroup = new HdfGroup("", rootObjectHeader, null, null);
233-
234-
this.bTree = new HdfTree(rootGroup);
235-
236-
processV2GroupLinks(rootGroup);
237-
}
238-
239222
/**
240223
* Processes all links within a V2 group to find and create child groups and datasets.
241224
*/

0 commit comments

Comments
 (0)