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 jHDF info to written files #574

Merged
merged 2 commits into from
May 11, 2024
Merged
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
5 changes: 2 additions & 3 deletions jhdf/src/main/java/io/jhdf/HdfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ public class HdfFile implements Group, AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(HdfFile.class);

static {
final String versionStr = HdfFile.class.getPackage().getImplementationVersion();
if (versionStr != null) {
logger.info("jHDF version: {}", HdfFile.class.getPackage().getImplementationVersion());
if (JhdfInfo.VERSION != null) {
logger.info("jHDF version: {}", JhdfInfo.VERSION);
} else {
logger.warn("Using development version of jHDF");
}
Expand Down
27 changes: 27 additions & 0 deletions jhdf/src/main/java/io/jhdf/JhdfInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* This file is part of jHDF. A pure Java library for accessing HDF5 files.
*
* http://jhdf.io
*
* Copyright (c) 2024 James Mudd
*
* MIT License see 'LICENSE' file
*/
package io.jhdf;

import java.nio.ByteOrder;

public final class JhdfInfo {

private JhdfInfo() {
throw new AssertionError("No instances of JhdfInfo");
}

public static final String VERSION = JhdfInfo.class.getPackage().getImplementationVersion();

public static final String OS = System.getProperty("os.name");

public static final String ARCH = System.getProperty("os.arch");

public static final String BYTE_ORDER = ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN) ? "LE" : "BE";
}
2 changes: 1 addition & 1 deletion jhdf/src/main/java/io/jhdf/Superblock.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public SuperblockV2V3() {
baseAddressByte = 0;
superblockExtensionAddress = Constants.UNDEFINED_ADDRESS;
endOfFileAddress = 500; // TODO
rootGroupObjectHeaderAddress = 48;
rootGroupObjectHeaderAddress = WritableHdfFile.ROOT_GROUP_ADDRESS;
}

private SuperblockV2V3(FileChannel fc, final long address) {
Expand Down
10 changes: 9 additions & 1 deletion jhdf/src/main/java/io/jhdf/WritableHdfFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
Expand All @@ -36,7 +38,7 @@ public class WritableHdfFile implements WritableGroup, AutoCloseable {

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

private static final long ROOT_GROUP_ADDRESS = 48;
public static final long ROOT_GROUP_ADDRESS = 64;

private final Path path;
private final FileChannel fileChannel;
Expand Down Expand Up @@ -73,6 +75,7 @@ private void flush() {
logger.info("Flushing to disk [{}]...", path.toAbsolutePath());
try {
rootGroup.write(hdfFileChannel, ROOT_GROUP_ADDRESS);
hdfFileChannel.write(getJHdfInfo());
long endOfFile = hdfFileChannel.getFileChannel().size();
hdfFileChannel.write(superblock.toBuffer(endOfFile), 0L);
logger.info("Flushed to disk [{}] file is [{}] bytes", path.toAbsolutePath(), endOfFile);
Expand All @@ -81,6 +84,11 @@ private void flush() {
}
}

private ByteBuffer getJHdfInfo() {
final String info = "jHDF - " + JhdfInfo.VERSION + " - " + JhdfInfo.OS + " - " + JhdfInfo.ARCH + " - " + JhdfInfo.BYTE_ORDER;
return ByteBuffer.wrap(info.getBytes(StandardCharsets.UTF_8));
}

@Override
public WritiableDataset putDataset(String name, Object data) {
return rootGroup.putDataset(name, data);
Expand Down
Loading