|
7 | 7 | import org.hdf5javalib.hdffile.infrastructure.HdfBTreeV1; |
8 | 8 | import org.hdf5javalib.hdffile.infrastructure.HdfChunkBTreeEntry; |
9 | 9 | import org.hdf5javalib.hdfjava.HdfDataFile; |
| 10 | +import org.hdf5javalib.hdfjava.HdfFileReader; |
10 | 11 | import org.hdf5javalib.utils.HdfDisplayUtils; |
11 | 12 | import org.hdf5javalib.utils.HdfReadUtils; |
12 | 13 |
|
@@ -160,7 +161,8 @@ public static HdfMessage parseHeaderMessage(int flags, byte[] data, HdfDataFile |
160 | 161 | 8, 0, (8 * 8), |
161 | 162 | hdfDataFile); |
162 | 163 |
|
163 | | - bTree = readBTreeFromSeekableByteChannel(hdfDataFile.getSeekableByteChannel(), chunkedDataAddress.getInstance(Long.class), numDimensions, eightByteFixedPointType, hdfDataFile); |
| 164 | +// bTree = HdfFileReader.readBTreeFromSeekableByteChannel(hdfDataFile.getSeekableByteChannel(), chunkedDataAddress.getInstance(Long.class), numDimensions, eightByteFixedPointType, hdfDataFile); |
| 165 | + bTree = HdfFileReader.readBTreeFromSeekableByteChannel(hdfDataFile.getSeekableByteChannel(), chunkedDataAddress.getInstance(Long.class), hdfDataFile); |
164 | 166 | } else { |
165 | 167 | bTree = null; |
166 | 168 | } |
@@ -198,98 +200,6 @@ public static HdfMessage parseHeaderMessage(int flags, byte[] data, HdfDataFile |
198 | 200 | return new DataLayoutMessage(version, dataLayoutStorage, flags, data.length); |
199 | 201 | } |
200 | 202 |
|
201 | | - /** |
202 | | - * Reads an HdfTree from a file channel. |
203 | | - * |
204 | | - * @param fileChannel the file channel to read from |
205 | | - * @param hdfDataFile the HDF5 file context |
206 | | - * @return the constructed HdfTree instance |
207 | | - * @throws IOException if an I/O error occurs or the B-Tree data is invalid |
208 | | - */ |
209 | | - public static HdfBTreeV1 readBTreeFromSeekableByteChannel( |
210 | | - SeekableByteChannel fileChannel, |
211 | | - long btreeAddress, |
212 | | - int dimensions, |
213 | | - FixedPointDatatype eightByteFixedPointType, |
214 | | - HdfDataFile hdfDataFile |
215 | | - ) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException { |
216 | | - return readFromSeekableByteChannelRecursive(fileChannel, btreeAddress, dimensions, eightByteFixedPointType, hdfDataFile, new LinkedHashMap<>()); |
217 | | - } |
218 | | - |
219 | | - /** |
220 | | - * Recursively reads an HdfTree from a file channel, handling cycles. |
221 | | - * |
222 | | - * @param fileChannel the file channel to read from |
223 | | - * @param nodeAddress the address of the current node |
224 | | - * @param visitedNodes a map of visited node addresses to detect cycles |
225 | | - * @param hdfDataFile the HDF5 file context |
226 | | - * @return the constructed HdfTree instance |
227 | | - * @throws IOException if an I/O error occurs or the B-Tree data is invalid |
228 | | - */ |
229 | | - private static HdfBTreeV1 readFromSeekableByteChannelRecursive(SeekableByteChannel fileChannel, |
230 | | - long nodeAddress, |
231 | | - int dimensions, |
232 | | - FixedPointDatatype eightByteFixedPointType, |
233 | | - HdfDataFile hdfDataFile, |
234 | | - Map<Long, HdfBTreeV1> visitedNodes |
235 | | - ) throws IOException, InvocationTargetException, InstantiationException, IllegalAccessException { |
236 | | - if (visitedNodes.containsKey(nodeAddress)) { |
237 | | - throw new IllegalStateException("Cycle detected or node re-visited: BTree node address " |
238 | | - + nodeAddress + " encountered again during recursive read."); |
239 | | - } |
240 | | - |
241 | | - fileChannel.position(nodeAddress); |
242 | | - FixedPointDatatype hdfOffset = hdfDataFile.getSuperblock().getFixedPointDatatypeForOffset(); |
243 | | - final int offsetSize = hdfOffset.getSize(); |
244 | | - |
245 | | - int headerSize = BTREE_HEADER_INITIAL_SIZE + offsetSize + offsetSize; |
246 | | - ByteBuffer headerBuffer = ByteBuffer.allocate(headerSize).order(ByteOrder.LITTLE_ENDIAN); |
247 | | - fileChannel.read(headerBuffer); |
248 | | - headerBuffer.flip(); |
249 | | - |
250 | | - byte[] signatureBytes = new byte[BTREE_SIGNATURE.length]; |
251 | | - headerBuffer.get(signatureBytes); |
252 | | - if (Arrays.compare(signatureBytes, BTREE_SIGNATURE) != 0) { |
253 | | - throw new IOException("Invalid B-tree node signature: '" + Arrays.toString(signatureBytes) + "' at position " + nodeAddress); |
254 | | - } |
255 | | - |
256 | | - int nodeType = Byte.toUnsignedInt(headerBuffer.get()); |
257 | | - int nodeLevel = Byte.toUnsignedInt(headerBuffer.get()); |
258 | | - int entriesUsed = Short.toUnsignedInt(headerBuffer.getShort()); |
259 | | - |
260 | | - HdfFixedPoint leftSiblingAddress = HdfReadUtils.readHdfFixedPointFromBuffer(hdfOffset, headerBuffer); |
261 | | - HdfFixedPoint rightSiblingAddress = HdfReadUtils.readHdfFixedPointFromBuffer(hdfOffset, headerBuffer); |
262 | | - |
263 | | - int entriesDataSize = (4 + 4 + 8*dimensions + offsetSize + offsetSize) * entriesUsed; |
264 | | - ByteBuffer entriesBuffer = ByteBuffer.allocate(entriesDataSize).order(ByteOrder.LITTLE_ENDIAN); |
265 | | - fileChannel.read(entriesBuffer); |
266 | | - entriesBuffer.flip(); |
267 | | - |
268 | | - List<HdfBTreeEntryBase> entries = new ArrayList<>(entriesUsed); |
269 | | - |
270 | | - HdfBTreeV1 currentNode = new HdfBTreeV1(nodeType, nodeLevel, entriesUsed, leftSiblingAddress, rightSiblingAddress, null, entries, hdfDataFile); |
271 | | - visitedNodes.put(nodeAddress, currentNode); |
272 | | - |
273 | | - // (4 + 4 + 8*dimensions + 1*length + 1*length) * entriesUsed |
274 | | - // 4 + 4 + 8*dimensions + 8 + 8 |
275 | | - // 8 + 24 + 16 |
276 | | - // 48 |
277 | | - |
278 | | - for (int i = 0; i < entriesUsed; i++) { |
279 | | - long sizeOfChunk = Integer.toUnsignedLong(entriesBuffer.getInt()); |
280 | | - long filterMask = Integer.toUnsignedLong(entriesBuffer.getInt()); |
281 | | - List<HdfFixedPoint> dimensionOffsets = new ArrayList<>(); |
282 | | - for (int j = 0; j < dimensions; j++) { |
283 | | - dimensionOffsets.add(HdfReadUtils.readHdfFixedPointFromBuffer(eightByteFixedPointType, entriesBuffer)); |
284 | | - } |
285 | | - HdfFixedPoint zeroValue = HdfReadUtils.readHdfFixedPointFromBuffer(hdfOffset, entriesBuffer); |
286 | | - HdfFixedPoint childPointer = HdfReadUtils.readHdfFixedPointFromBuffer(hdfOffset, entriesBuffer); |
287 | | - |
288 | | - entries.add(new HdfChunkBTreeEntry(null, childPointer, null, sizeOfChunk, filterMask, dimensionOffsets)); |
289 | | - } |
290 | | - return currentNode; |
291 | | - } |
292 | | - |
293 | 203 |
|
294 | 204 | /** |
295 | 205 | * Returns a string representation of this DataLayoutMessage. |
|
0 commit comments