Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Time Data Type support #525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions jhdf/src/main/java/io/jhdf/object/datatype/DataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

import io.jhdf.Utils;
import io.jhdf.exceptions.HdfException;
import io.jhdf.exceptions.UnsupportedHdfException;
import io.jhdf.storage.HdfBackingStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.BitSet;

public abstract class DataType {

private static final Logger logger = LoggerFactory.getLogger(DataType.class);

private final int version;
private final int dataClass;
private final int size; // In bytes
Expand All @@ -33,7 +36,9 @@ public static DataType readDataType(ByteBuffer bb) {
int version = Utils.bitsToInt(classAndVersion, 4, 4);
int dataClass = Utils.bitsToInt(classAndVersion, 0, 4);

if (version == 0 || version > 3) {
if (version == 0) {
logger.warn("Data type version 0 detected. This is out of spec");
} else if (version > 3) {
throw new HdfException("Unrecognized datatype version '" + version + "' detected");
}

Expand All @@ -46,7 +51,7 @@ public static DataType readDataType(ByteBuffer bb) {
case 1: // Floating point
return new FloatingPoint(bb);
case 2: // Time
throw new UnsupportedHdfException("Time data type is not yet supported");
return new TimeDataType(bb);
case 3: // String
return new StringData(bb);
case 4: // Bit field
Expand Down
72 changes: 72 additions & 0 deletions jhdf/src/main/java/io/jhdf/object/datatype/TimeDataType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This file is part of jHDF. A pure Java library for accessing HDF5 files.
*
* http://jhdf.io
*
* Copyright (c) 2023 James Mudd
*
* MIT License see 'LICENSE' file
*/
package io.jhdf.object.datatype;

import io.jhdf.Utils;
import io.jhdf.storage.HdfBackingStorage;

import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.time.Instant;

import static io.jhdf.Utils.stripLeadingIndex;

public class TimeDataType extends DataType implements OrderedDataType {
private final ByteOrder order;
private final short bitPrecision;

public TimeDataType(ByteBuffer bb) {
super(bb);

if (classBits.get(0)) {
order = ByteOrder.BIG_ENDIAN;
} else {
order = ByteOrder.LITTLE_ENDIAN;
}

bitPrecision = bb.getShort();
}

@Override
public ByteOrder getByteOrder() {
return order;
}
@Override
public Class<?> getJavaType() {
return Instant.class;
}

@Override
public Object fillData(ByteBuffer buffer, int[] dimensions, HdfBackingStorage hdfBackingStorage) {
final Object data = Array.newInstance(getJavaType(), dimensions);
fillData(data, dimensions, buffer.order(getByteOrder()));
return data;
}

private void fillData(Object data, int[] dims, ByteBuffer buffer) {
if (dims.length > 1) {
for (int i = 0; i < dims[0]; i++) {
Object newArray = Array.get(data, i);
fillData(newArray, stripLeadingIndex(dims), buffer);
}
} else {
for (int i = 0; i < Array.getLength(data); i++) {
long epochMills = Utils.readBytesAsUnsignedLong(buffer, this.getSize());
Instant instant = Instant.ofEpochMilli(epochMills);
Array.set(data, i, instant);
}
}
}

public short getBitPrecision() {
return bitPrecision;
}
}
Binary file added jhdf/src/test/resources/hdf5/isssue-523.hdf5
Binary file not shown.
Loading