HDFGroup / HDFGroup/hdf5

Add H5Dread_chunk, H5Dwrite_chunk Java Native Interface Bindings

Open
#2,199 0 comments 0 reactions 2 assignees Assigned to @brtnfld View on GitHub
Component - Wrappers
Dominant language
C
Stars
988
Forks
355
Avg merge
4d 2h
Merged PRs (30d)
12

Description

**Is your feature request related to a problem? Please describe.**
`H5Dread_chunk` and `H5Dwrite_chunk` are absent from the Java Native Interface bindings.

**Describe the solution you'd like**
Create Java Native Interface bindings for `H5Dread_chunk` and `H5Dwrite_chunk`.

**Describe alternatives you've considered**
Obtain the chunk address offsets and read the chunk from the file directly.

**Additional context**
There are two ways to interface Java arrays with native C pointers.
1. [`GetByteArrayRegion`](https://docs.oracle.com/en/java/javase/19/docs/specs/jni/functions.html#getprimitivetypearrayregion-routines) / [`SetByteArrayRegion`](https://docs.oracle.com/en/java/javase/19/docs/specs/jni/functions.html#setprimitivetypearrayregion-routines)
2. [`NewDirectByteBuffer`](https://docs.oracle.com/en/java/javase/19/docs/specs/jni/functions.html#newdirectbytebuffer) / [`GetDirectByteBuffer`](https://docs.oracle.com/en/java/javase/19/docs/specs/jni/functions.html#getdirectbufferaddress) via [`java.nio.ByteBuffer`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/nio/ByteBuffer.html)

I strongly encourage the use of `java.nio.ByteBuffer` and its related JNI functions to implement `H5Dread_chunk` and `H5Dwrite_chunk`. The direct nature of these functions matches the directness of `java.nio.ByteBuffer` as a no-copy interface. The Java user would need to use [`java.nio.ByteBuffer#allocateDirect`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/nio/ByteBuffer.html#allocateDirect(int)) to allocate the buffer. On the C side, we could obtain the pointer to the allocated buffer via [`GetDirectByteBuffer`](https://docs.oracle.com/en/java/javase/19/docs/specs/jni/functions.html#getdirectbufferaddress). The pointer can then be passed directly to the C functions `H5Dread_chunk` and `H5Dwrite_chunk`.

On the Java side, the populated `ByteBuffer` can be copied to a byte array as needed via [`get`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/nio/ByteBuffer.html#get(byte%5B%5D)) or cast to the correct type buffer such as `CharBuffer`, `IntBuffer`, or `DoubleBuffer` as needed.

Also I note that `ByteBuffer` is also used in the new [`java.lang.foreign.MemorySegment#asByteBuffer`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/lang/foreign/MemorySegment.html#asByteBuffer()) meaning that the use of `ByteBuffer` in native interfaces is likely to provide both backward and future compatibility.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.