Skip to content

Commit a593dba

Browse files
authored
Merge pull request #525 from jamesmudd/issue-523
Add Time Data Type support
2 parents adb3542 + cd31723 commit a593dba

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

Diff for: jhdf/src/main/java/io/jhdf/object/datatype/DataType.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515
import io.jhdf.exceptions.UnsupportedHdfException;
1616
import io.jhdf.storage.HdfBackingStorage;
1717
import io.jhdf.storage.HdfFileChannel;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1820

1921
import java.nio.ByteBuffer;
2022
import java.util.BitSet;
2123

2224
public abstract class DataType {
2325

26+
private static final Logger logger = LoggerFactory.getLogger(DataType.class);
2427
private final int version;
2528
private final int dataClass;
2629
private final int size; // In bytes
@@ -42,7 +45,9 @@ public static DataType readDataType(ByteBuffer bb) {
4245
int version = Utils.bitsToInt(classAndVersion, 4, 4);
4346
int dataClass = Utils.bitsToInt(classAndVersion, 0, 4);
4447

45-
if (version == 0 || version > 3) {
48+
if (version == 0) {
49+
logger.warn("Datatype version 0 detected this is out of spec, attempting to read anyway");
50+
} else if (version > 3) {
4651
throw new HdfException("Unrecognized datatype version '" + version + "' detected");
4752
}
4853

@@ -55,8 +60,8 @@ public static DataType readDataType(ByteBuffer bb) {
5560
return new FixedPoint(bb);
5661
case FloatingPoint.CLASS_ID: // Floating point
5762
return new FloatingPoint(bb);
58-
case 2: // Time
59-
throw new UnsupportedHdfException("Time data type is not yet supported");
63+
case TimeDataType.CLASS_ID: // Time
64+
return new TimeDataType(bb);
6065
case StringData.CLASS_ID: // String
6166
return new StringData(bb);
6267
case BitField.CLASS_ID: // Bit field
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* This file is part of jHDF. A pure Java library for accessing HDF5 files.
3+
*
4+
* https://jhdf.io
5+
*
6+
* Copyright (c) 2025 James Mudd
7+
*
8+
* MIT License see 'LICENSE' file
9+
*/
10+
package io.jhdf.object.datatype;
11+
12+
import io.jhdf.storage.HdfBackingStorage;
13+
14+
import java.lang.reflect.Array;
15+
import java.nio.ByteBuffer;
16+
import java.nio.ByteOrder;
17+
18+
import static io.jhdf.Utils.stripLeadingIndex;
19+
20+
public class TimeDataType extends DataType implements OrderedDataType {
21+
public static final int CLASS_ID = 2;
22+
23+
private final ByteOrder order;
24+
private final short bitPrecision;
25+
26+
public TimeDataType(ByteBuffer bb) {
27+
super(bb);
28+
29+
if (classBits.get(0)) {
30+
order = ByteOrder.BIG_ENDIAN;
31+
} else {
32+
order = ByteOrder.LITTLE_ENDIAN;
33+
}
34+
35+
bitPrecision = bb.getShort();
36+
}
37+
38+
@Override
39+
public ByteOrder getByteOrder() {
40+
return order;
41+
}
42+
@Override
43+
public Class<?> getJavaType() {
44+
return byte[].class;
45+
}
46+
47+
@Override
48+
public Object fillData(ByteBuffer buffer, int[] dimensions, HdfBackingStorage hdfBackingStorage) {
49+
final Object data = Array.newInstance(getJavaType(), dimensions);
50+
fillData(data, dimensions, buffer.order(getByteOrder()));
51+
return data;
52+
}
53+
54+
private void fillData(Object data, int[] dims, ByteBuffer buffer) {
55+
if (dims.length > 1) {
56+
for (int i = 0; i < dims[0]; i++) {
57+
Object newArray = Array.get(data, i);
58+
fillData(newArray, stripLeadingIndex(dims), buffer);
59+
}
60+
} else {
61+
for (int i = 0; i < Array.getLength(data); i++) {
62+
byte[] bytes = new byte[getSize()];
63+
buffer.get(bytes);
64+
Array.set(data, i, bytes);
65+
}
66+
}
67+
}
68+
69+
public short getBitPrecision() {
70+
return bitPrecision;
71+
}
72+
}

Diff for: jhdf/src/test/resources/hdf5/isssue-523.hdf5

323 KB
Binary file not shown.

0 commit comments

Comments
 (0)