|
| 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 | +} |
0 commit comments