Help us improve by taking our short survey: https://www.hdfgroup.org/website-survey/

| HDF5 Last Updated on 2025-11-11 The HDF5 Field Guide

- Main Page
- Getting started
- User Guide
- Reference Manual
- Cookbook
- Technical Notes
- RFCs
- Specifications
- Glossary
- Full-Text Search
- About

Loading...
Searching...
No Matches
HDF5 Java API Package

This class is the Java interface for the HDF5 library.

This code is the called by Java programs to access the entry points of the HDF5 library. Each routine wraps a single HDF5 entry point, generally with the arguments and return codes analogous to the C interface.

For details of the HDF5 library,

See also
  HDF5 Reference Manual

Mapping of arguments for Java

In general, arguments to the HDF Java API are straightforward translations from the 'C' API described in the HDF5 Reference Manual.

HDF5 C types to Java types
HDF5 | Java
H5T_NATIVE_INT | int, Integer
H5T_NATIVE_SHORT | short, Short
H5T_NATIVE_FLOAT | float, Float
H5T_NATIVE_DOUBLE | double, Double
H5T_NATIVE_CHAR | byte, Byte
H5T_C_S1 | java.lang.String
void * (i.e., pointer to ‘Any’) | Special – see Java Array Conversion

General Rules for Passing Arguments and Results

In general, arguments passed IN to Java are the analogous basic types, as above. The exception is for arrays, which are discussed below.

The return value of Java methods is also the analogous type, as above. A major exception to that rule is that all HDF Java functions will raise an exception upon failure in the Java version, rather than just return int as in the C. Functions that return a value are declared equivalent to the C function. However, in most cases the Java method will raise an exception instead of returning an error code.

See also
  Errors and Exceptions.

Java does not support pass by reference of arguments, so arguments that are returned through OUT parameters must be wrapped in an object or array. The Java API for HDF consistently wraps arguments in arrays. Where possible the Java function may return the OUT parameter as an object or basic type.

For instance, a function that returns two integers declared as:

          h_err_t HDF5dummy( int *a1, int *a2)

For the Java interface, this would be declared:

    public synchronized static native int HDF5dummy(int args[]);

OR

    public synchronized static native int[] HDF5dummy();

where a1 is args[0] and a2 is args[1], and would be invoked:

    H5.HDF5dummy(a);

OR

    a = H5.HDF5dummy();

All the routines where this convention is used will have specific documentation of the details, given below.

Java Array Conversion

HDF5 needs to read and write multi-dimensional arrays of any number type (and records). The HDF5 API describes the layout of the source and destination, and the data for the array passed as a block of bytes, for instance,

herr_t H5Dread(long fid, long filetype, long memtype, long memspace, void *data);
herr_t
int herr_t
Definition H5public.h:252
H5Dread
herr_t H5Dread(hid_t dset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t dxpl_id, void *buf)
Reads raw data from a dataset into a provided buffer.

where `void *'' means that the data may be any valid numeric type, and is a contiguous block of bytes that is the data for a multi-dimensional array. The other parameters describe the dimensions, rank, and datatype of the array ondisk (source) and in memory (destination). <p> For Java, this`ANY'' is a problem, as the type of data must always be declared. Furthermore, multidimensional arrays are definitely not laid out contiguously in memory. It would be infeasible to declare a separate routine for every combination of number type and dimensionality. For that reason, the Java Array Conversion HDFArray class is used to discover the type, shape, and size of the data array at run time, and to convert to and from a contiguous array of bytes in synchronized static native C order.

The upshot is that any Java array of numbers (either primitive or sub-classes of type Number) can be passed as an `‘Object’', and the Java API will translate to and from the appropriate packed array of bytes needed by the C library. So the function above would be declared:

public synchronized static int H5Dread(long dataset_id, long mem_type_id, long mem_space_id,
long file_space_id, long xfer_plist_id, Object obj,
boolean isCriticalPinning)
throws HDF5Exception, HDF5LibraryException, NullPointerException;

and the parameter data can be any multi-dimensional array of numbers, such as float[][], or int[][][], or Double[][].

Constants and Enumerated Types

The HDF5 API defines a set of constants and enumerated values. Most of these values are available to Java programs via the class Constants and Enumerated Types HDF5Constants. For example, the parameters for the h5open() call include two numeric values, HDFConstants.H5F_ACC_RDWR and HDF5Constants.H5P_DEFAULT. As would be expected, these numbers correspond to the C constants H5F_ACC_RDWR and H5P_DEFAULT.

The HDF5 API defines a set of values that describe number types and sizes, such as "H5T_NATIVE_INT" and "hsize_t". These values are determined at run time by the HDF5 C library. To support these parameters, the Java HDFConstants class looks up the values when initiated. The values can be accessed as public variables of the Java class, such as:

long data_type = HDFConstants.H5T_NATIVE_INT;

The Java application uses both types of constants the same way, the only difference is that the HDFConstants may have different values on different platforms.

Errors and Exceptions

The HDF5 error API (Error Handling (H5E)) manages the behavior of the error stack in the HDF5 library. This API is omitted from the JHI5. Errors are converted into Java exceptions. This is totally different from the C interface, but is very natural for Java programming.

The exceptions of the JHI5 are organized as sub-classes of the class Errors and Exceptions HDF5Exception. There are two subclasses of HDF5Exception, HDF5 Library Errors and Exceptions HDF5LibraryException and Java Wrapper Errors and Exceptions HDF5JavaException. The sub-classes of the former represent errors from the HDF5 C library, while sub-classes of the latter represent errors in the JHI5 wrapper and support code.

The super-class HDF5LibraryException implements the method 'printStackTrace()', which prints out the HDF5 error stack, as described in the HDF5 C API H5Eprint(). This may be used by Java exception handlers to print out the HDF5 error stack.

Version
  HDF5 2.0.0
  See also: Java Array Conversion hdf.hdf5lib.HDFArray
  Constants and Enumerated Types hdf.hdf5lib.HDF5Constants
  Errors and Exceptions hdf.hdf5lib.HDF5Exception
  HDF5

For details of the HDF5 library,

See also
  HDF5 Reference Manual