Skip to content

Commit 44133a2

Browse files
committed
refactor v2 code
1 parent 5f42cfb commit 44133a2

1 file changed

Lines changed: 46 additions & 17 deletions

File tree

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,58 @@ public byte[] getFillValue() {
117117
*/
118118
public static HdfMessage parseHeaderMessage(int flags, byte[] data, HdfDataFile hdfDataFile) {
119119
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
120-
121-
// Parse the first 4 bytes
122-
int version = Byte.toUnsignedInt(buffer.get());
123-
int spaceAllocationTime = Byte.toUnsignedInt(buffer.get());
124-
int fillValueWriteTime = Byte.toUnsignedInt(buffer.get());
125-
int fillValueDefined = Byte.toUnsignedInt(buffer.get());
126-
127120
// Initialize optional fields
128121
int size = 0;
129122
byte[] fillValue = null;
123+
int spaceAllocationTime = -1;
124+
int fillValueWriteTime = -1;
125+
int fillValueDefined;
130126

131-
// Handle Version 2+ behavior and fillValueDefined flag
132-
if (version >= 2 && fillValueDefined == 1) {
133-
// Parse Size (unsigned 4 bytes)
134-
size = buffer.getInt();
135127

136-
// Parse Fill Value
137-
fillValue = new byte[size];
138-
buffer.get(fillValue);
139-
}
128+
// Parse the first 4 bytes
129+
int version = Byte.toUnsignedInt(buffer.get());
130+
if (version < 3) {
131+
spaceAllocationTime = Byte.toUnsignedInt(buffer.get());
132+
fillValueWriteTime = Byte.toUnsignedInt(buffer.get());
133+
fillValueDefined = Byte.toUnsignedInt(buffer.get());
134+
135+
// Handle Version 2+ behavior and fillValueDefined flag
136+
if (version >= 2 && fillValueDefined == 1) {
137+
// Parse Size (unsigned 4 bytes)
138+
size = buffer.getInt();
139+
140+
// Parse Fill Value
141+
fillValue = new byte[size];
142+
buffer.get(fillValue);
143+
}
144+
145+
// Return a constructed instance of FillValueMessage
146+
return new FillValueMessage(version, spaceAllocationTime, fillValueWriteTime, fillValueDefined, size, fillValue, flags, data.length);
147+
148+
} else {
149+
final int FILL_VALUE_DEFINED_MASK = 0x20; // Bit 5// Read Flags (1 byte)
150+
151+
int fvFlags = buffer.get();
152+
153+
// Check if Fill Value Defined flag is set (bit 5)
154+
fillValueDefined = (fvFlags & FILL_VALUE_DEFINED_MASK);
155+
156+
if (fillValueDefined != 0) {
157+
// Read Size (4 bytes, assuming 32-bit integer as per HDF5 convention for size fields)
158+
size = buffer.getInt();
159+
if (size < 0) {
160+
throw new IllegalArgumentException("Invalid fill value size: " + size);
161+
}
162+
// Read Fill Value (variable size)
163+
fillValue = new byte[Math.toIntExact(size)];
164+
buffer.get(fillValue);
165+
} else {
166+
size = 0;
167+
fillValue = null;
168+
} // Return a constructed instance of FillValueMessage
169+
return new FillValueMessage(version, spaceAllocationTime, fillValueWriteTime, fillValueDefined, size, fillValue, flags, data.length);
140170

141-
// Return a constructed instance of FillValueMessage
142-
return new FillValueMessage(version, spaceAllocationTime, fillValueWriteTime, fillValueDefined, size, fillValue, flags, data.length);
171+
}
143172
}
144173

145174
/**

0 commit comments

Comments
 (0)