Class NativeData


  • public class NativeData
    extends java.lang.Object
    This class encapsulates native methods to deal with arrays of numbers, converting from numbers to bytes and bytes to numbers.

    These routines are used by class HDFArray to pass data to and from the HDF5 library.

    Methods copyXxxToByte() convert a Java array of primitive numbers (int, short, ...) to a Java array of bytes. Methods copyByteToXxx() convert from a Java array of bytes into a Java array of primitive numbers (int, short, ...)

    Variant interfaces convert only a sub-array.

    The class has optimized methods using jni-libraries for some common platforms and a pure-java implementation (called javamode if the jni-libraries are not available). If you want to enforce javamode, you need to pass the property nativedata.javamode=true to the JRE.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  NativeData.ByteOrder
      Byte Order enumeration.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int CHAR_SIZE
      Size of a char value in bytes.
      static int DOUBLE_SIZE
      Size of a double value in bytes.
      static int FLOAT_SIZE
      Size of a float value in bytes.
      static int INT_SIZE
      Size of an int value in bytes.
      static int LONG_SIZE
      Size of a long value in bytes.
      static int SHORT_SIZE
      Size of a short value in bytes.
    • Constructor Summary

      Constructors 
      Constructor Description
      NativeData()  
    • Field Detail

      • SHORT_SIZE

        public static final int SHORT_SIZE
        Size of a short value in bytes.
        See Also:
        Constant Field Values
      • CHAR_SIZE

        public static final int CHAR_SIZE
        Size of a char value in bytes.
        See Also:
        Constant Field Values
      • INT_SIZE

        public static final int INT_SIZE
        Size of an int value in bytes.
        See Also:
        Constant Field Values
      • LONG_SIZE

        public static final int LONG_SIZE
        Size of a long value in bytes.
        See Also:
        Constant Field Values
      • FLOAT_SIZE

        public static final int FLOAT_SIZE
        Size of a float value in bytes.
        See Also:
        Constant Field Values
      • DOUBLE_SIZE

        public static final int DOUBLE_SIZE
        Size of a double value in bytes.
        See Also:
        Constant Field Values
    • Constructor Detail

      • NativeData

        public NativeData()
    • Method Detail

      • ensureNativeLibIsLoaded

        public static void ensureNativeLibIsLoaded()
        Call to ensure that the native library is loaded.
      • isUseNativeLib

        public static boolean isUseNativeLib()
        Returns true, if this class uses the native library and false otherwise.
      • getNativeByteOrder

        public static NativeData.ByteOrder getNativeByteOrder()
        Returns the native byte order of the host running this JRE.
      • changeByteOrder

        public static short changeByteOrder​(short s)
        Changes the byte order of the bytes constituting s.
      • changeByteOrder

        public static char changeByteOrder​(char c)
        Changes the byte order of the bytes constituting c.
      • changeByteOrder

        public static int changeByteOrder​(int i)
        Changes the byte order of the bytes constituting i.
      • changeByteOrder

        public static float changeByteOrder​(float f)
        Changes the byte order of the bytes constituting f.
      • changeByteOrder

        public static long changeByteOrder​(long l)
        Changes the byte order of the bytes constituting l.
      • changeByteOrder

        public static double changeByteOrder​(double d)
        Changes the byte order of the bytes constituting d.
      • copyIntToByte

        public static void copyIntToByte​(int[] inData,
                                         int inStart,
                                         byte[] outData,
                                         int outStart,
                                         int len,
                                         NativeData.ByteOrder byteOrder)
        Copies a range from an array of int into an array of byte.
        Parameters:
        inData - The input array of int values.
        inStart - The position in the input array inData of int to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of int to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToInt

        public static void copyByteToInt​(byte[] inData,
                                         int inStart,
                                         int[] outData,
                                         int outStart,
                                         int len,
                                         NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of int.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of int values.
        outStart - The start in the output array byteData of int to start
        len - The number of int to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyLongToByte

        public static void copyLongToByte​(long[] inData,
                                          int inStart,
                                          byte[] outData,
                                          int outStart,
                                          int len,
                                          NativeData.ByteOrder byteOrder)
        Copies a range from an array of long into an array of byte.
        Parameters:
        inData - The input array of long values.
        inStart - The position in the input array inData of long to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of long to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToLong

        public static void copyByteToLong​(byte[] inData,
                                          int inStart,
                                          long[] outData,
                                          int outStart,
                                          int len,
                                          NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of long.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of long values.
        outStart - The start in the output array byteData of long to start
        len - The number of long to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyShortToByte

        public static void copyShortToByte​(short[] inData,
                                           int inStart,
                                           byte[] outData,
                                           int outStart,
                                           int len,
                                           NativeData.ByteOrder byteOrder)
        Copies a range from an array of short into an array of byte.
        Parameters:
        inData - The input array of short values.
        inStart - The position in the input array inData of short to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of short to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyCharToByte

        public static void copyCharToByte​(char[] inData,
                                          int inStart,
                                          byte[] outData,
                                          int outStart,
                                          int len,
                                          NativeData.ByteOrder byteOrder)
        Copies a range from an array of char into an array of byte.
        Parameters:
        inData - The input array of char values.
        inStart - The position in the input array inData of char to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of char to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToShort

        public static void copyByteToShort​(byte[] inData,
                                           int inStart,
                                           short[] outData,
                                           int outStart,
                                           int len,
                                           NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of short.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of short values.
        outStart - The start in the output array byteData of short to start
        len - The number of short to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToChar

        public static void copyByteToChar​(byte[] inData,
                                          int inStart,
                                          char[] outData,
                                          int outStart,
                                          int len,
                                          NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of char.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of short values.
        outStart - The start in the output array byteData of short to start
        len - The number of short to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyFloatToByte

        public static void copyFloatToByte​(float[] inData,
                                           int inStart,
                                           byte[] outData,
                                           int outStart,
                                           int len,
                                           NativeData.ByteOrder byteOrder)
        Copies a range from an array of float into an array of byte.
        Parameters:
        inData - The input array of float values.
        inStart - The position in the input array inData of float to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of float to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToFloat

        public static void copyByteToFloat​(byte[] inData,
                                           int inStart,
                                           float[] outData,
                                           int outStart,
                                           int len,
                                           NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of float.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of float values.
        outStart - The start in the output array byteData of float to start
        len - The number of float to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyDoubleToByte

        public static void copyDoubleToByte​(double[] inData,
                                            int inStart,
                                            byte[] outData,
                                            int outStart,
                                            int len,
                                            NativeData.ByteOrder byteOrder)
        Copies a range from an array of double into an array of byte.
        Parameters:
        inData - The input array of double values.
        inStart - The position in the input array inData of double to start
        outData - The output array of byte values.
        outStart - The start in the output array byteData of byte to start
        len - The number of double to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • copyByteToDouble

        public static void copyByteToDouble​(byte[] inData,
                                            int inStart,
                                            double[] outData,
                                            int outStart,
                                            int len,
                                            NativeData.ByteOrder byteOrder)
        Copies a range from an array of byte into an array of double.
        Parameters:
        inData - The input array of byte values.
        inStart - The position in the input array inData of byte to start
        outData - The output array of double values.
        outStart - The start in the output array byteData of double to start
        len - The number of double to copy
        byteOrder - The NativeData.ByteOrder, encoding what byte order the outData should be in.
      • byteToChar

        public static char[] byteToChar​(byte[] byteArr,
                                        NativeData.ByteOrder byteOrder,
                                        int start,
                                        int len)
        Converts a byte[] array into a char[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of short values to convert.
        Returns:
        The char[] array.
      • byteToChar

        public static char[] byteToChar​(byte[] byteArr,
                                        NativeData.ByteOrder byteOrder)
        Converts a byte[] array into a char[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The char[] array.
      • byteToShort

        public static short[] byteToShort​(byte[] byteArr,
                                          NativeData.ByteOrder byteOrder,
                                          int start,
                                          int len)
        Converts a byte[] array into a short[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of short values to convert.
        Returns:
        The short[] array.
      • byteToShort

        public static short[] byteToShort​(byte[] byteArr,
                                          NativeData.ByteOrder byteOrder)
        Converts a byte[] array into a short[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The short[] array.
      • shortToByte

        public static byte[] shortToByte​(short[] data,
                                         NativeData.ByteOrder byteOrder,
                                         int start,
                                         int len)
        Converts a short[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of short values to convert.
        Returns:
        The converted byte[] array.
      • shortToByte

        public static byte[] shortToByte​(short[] data,
                                         NativeData.ByteOrder byteOrder)
        Converts a short[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.
      • charToByte

        public static byte[] charToByte​(char[] data,
                                        NativeData.ByteOrder byteOrder,
                                        int start,
                                        int len)
        Converts a char[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of char values to convert.
        Returns:
        The converted byte[] array.
      • charToByte

        public static byte[] charToByte​(char[] data,
                                        NativeData.ByteOrder byteOrder)
        Converts a char[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.
      • byteToInt

        public static int[] byteToInt​(byte[] byteArr,
                                      NativeData.ByteOrder byteOrder,
                                      int start,
                                      int len)
        Converts a byte[] array into an int[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of int values to convert.
        Returns:
        The int[] array.
      • byteToInt

        public static int[] byteToInt​(byte[] byteArr,
                                      NativeData.ByteOrder byteOrder)
        Converts a byte[] array into an int[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The int[] array.
      • intToByte

        public static byte[] intToByte​(int[] data,
                                       NativeData.ByteOrder byteOrder,
                                       int start,
                                       int len)
        Converts a int[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of int values to convert.
        Returns:
        The converted byte[] array.
      • intToByte

        public static byte[] intToByte​(int[] data,
                                       NativeData.ByteOrder byteOrder)
        Converts a int[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.
      • byteToLong

        public static long[] byteToLong​(byte[] byteArr,
                                        NativeData.ByteOrder byteOrder,
                                        int start,
                                        int len)
        Converts a byte[] array into a long[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of long values to convert.
        Returns:
        The long[] array.
      • byteToLong

        public static long[] byteToLong​(byte[] byteArr,
                                        NativeData.ByteOrder byteOrder)
        Converts a byte[] array into a long[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The long[] array.
      • longToByte

        public static byte[] longToByte​(long[] data,
                                        NativeData.ByteOrder byteOrder,
                                        int start,
                                        int len)
        Converts a long[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of long values to convert.
        Returns:
        The converted byte[] array.
      • longToByte

        public static byte[] longToByte​(long[] data,
                                        NativeData.ByteOrder byteOrder)
        Converts a long[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.
      • byteToFloat

        public static float[] byteToFloat​(byte[] byteArr,
                                          NativeData.ByteOrder byteOrder,
                                          int start,
                                          int len)
        Converts a byte[] array into a float[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of float values to convert.
        Returns:
        The float[] array.
      • byteToFloat

        public static float[] byteToFloat​(byte[] byteArr,
                                          NativeData.ByteOrder byteOrder)
        Converts a byte[] array into a float[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The float[] array.
      • floatToByte

        public static byte[] floatToByte​(float[] data,
                                         NativeData.ByteOrder byteOrder,
                                         int start,
                                         int len)
        Converts a float[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of float values to convert.
        Returns:
        The converted byte[] array.
      • floatToByte

        public static byte[] floatToByte​(float[] data,
                                         NativeData.ByteOrder byteOrder)
        Converts a float[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.
      • byteToDouble

        public static double[] byteToDouble​(byte[] byteArr,
                                            NativeData.ByteOrder byteOrder,
                                            int start,
                                            int len)
        Converts a byte[] array into a double[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        start - The position in the byteArr to start the conversion.
        len - The number of double values to convert.
        Returns:
        The double[] array.
      • byteToDouble

        public static double[] byteToDouble​(byte[] byteArr,
                                            NativeData.ByteOrder byteOrder)
        Converts a byte[] array into a double[] array.
        Parameters:
        byteArr - The byte[] to convert.
        byteOrder - The byte order of byteArr.
        Returns:
        The double[] array.
      • doubleToByte

        public static byte[] doubleToByte​(double[] data,
                                          NativeData.ByteOrder byteOrder,
                                          int start,
                                          int len)
        Converts a double[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        start - The position in data to start the conversion.
        len - The number of double values to convert.
        Returns:
        The converted byte[] array.
      • doubleToByte

        public static byte[] doubleToByte​(double[] data,
                                          NativeData.ByteOrder byteOrder)
        Converts a double[] array to a byte[] array.
        Parameters:
        data - The array to convert.
        byteOrder - The byte order of the returned byte[].
        Returns:
        The converted byte[] array.