Skip to content

Commit

Permalink
Add jHDF info to written files
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmudd committed May 11, 2024
1 parent 43d5234 commit 53d8fc3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
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
18 changes: 18 additions & 0 deletions jhdf/src/main/java/io/jhdf/JhdfInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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

0 comments on commit 53d8fc3

Please sign in to comment.