summaryrefslogtreecommitdiffstats
path: root/java/src/hdf/hdf5lib/HDFNativeData.java
blob: c4970431878ad047a6836eda22761d853857c1d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package hdf.hdf5lib;

import hdf.hdf5lib.exceptions.HDF5Exception;
import hdf.hdf5lib.exceptions.HDF5JavaException;

/**
 * This class encapsulates native methods to deal with arrays of numbers,
 * converting from numbers to bytes and bytes to numbers.
 * <p>
 * These routines are used by class <b>HDFArray</b> to pass data to and from the
 * HDF-5 library.
 * <p>
 * Methods xxxToByte() convert a Java array of primitive numbers (int, short,
 * ...) to a Java array of bytes. Methods byteToXxx() convert from a Java array
 * of bytes into a Java array of primitive numbers (int, short, ...)
 * <p>
 * Variant interfaces convert a section of an array, and also can convert to
 * sub-classes of Java <b>Number</b>.
 * <P>
 * <b>See also:</b> hdf.hdf5lib.HDFArray.
 */

public class HDFNativeData {

    /**
     * Convert an array of bytes into an array of ints
     *
     * @param data
     *            The input array of bytes
     * @return an array of int
     */
    public synchronized static native int[] byteToInt(byte[] data);

    /**
     * Convert an array of bytes into an array of floats
     *
     * @param data
     *            The input array of bytes
     * @return an array of float
     */
    public synchronized static native float[] byteToFloat(byte[] data);

    /**
     * Convert an array of bytes into an array of shorts
     *
     * @param data
     *            The input array of bytes
     * @return an array of short
     */
    public synchronized static native short[] byteToShort(byte[] data);

    /**
     * Convert an array of bytes into an array of long
     *
     * @param data
     *            The input array of bytes
     * @return an array of long
     */
    /*
     * does this really work? C 'long' is 32 bits, Java 'long' is 64-bits. What
     * does this routine actually do?
     */
    public synchronized static native long[] byteToLong(byte[] data);

    /**
     * Convert an array of bytes into an array of double
     *
     * @param data
     *            The input array of bytes
     * @return an array of double
     */
    public synchronized static native double[] byteToDouble(byte[] data);

    /**
     * Convert a range from an array of bytes into an array of int
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param len
     *            The number of 'int' to convert
     * @param data
     *            The input array of bytes
     * @return an array of 'len' int
     */
    public synchronized static native int[] byteToInt(int start, int len, byte[] data);

    /**
     * Convert 4 bytes from an array of bytes into a single int
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param data
     *            The input array of bytes
     * @return The integer value of the bytes.
     */
    public synchronized static int byteToInt(byte[] data, int start)
    {
        int[] ival = new int[1];
        ival       = byteToInt(start, 1, data);
        return (ival[0]);
    }

    /**
     * Convert a range from an array of bytes into an array of short
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param len
     *            The number of 'short' to convert
     * @param data
     *            The input array of bytes
     * @return an array of 'len' short
     */
    public synchronized static native short[] byteToShort(int start, int len, byte[] data);

    /**
     * Convert 2 bytes from an array of bytes into a single short
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param data
     *            The input array of bytes
     * @return The short value of the bytes.
     */
    public synchronized static short byteToShort(byte[] data, int start)
    {
        short[] sval = new short[1];
        sval         = byteToShort(start, 1, data);
        return (sval[0]);
    }

    /**
     * Convert a range from an array of bytes into an array of float
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param len
     *            The number of 'float' to convert
     * @param data
     *            The input array of bytes
     * @return an array of 'len' float
     */
    public synchronized static native float[] byteToFloat(int start, int len, byte[] data);

    /**
     * Convert 4 bytes from an array of bytes into a single float
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param data
     *            The input array of bytes
     * @return The float value of the bytes.
     */
    public synchronized static float byteToFloat(byte[] data, int start)
    {
        float[] fval = new float[1];
        fval         = byteToFloat(start, 1, data);
        return (fval[0]);
    }

    /**
     * Convert a range from an array of bytes into an array of long
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param len
     *            The number of 'long' to convert
     * @param data
     *            The input array of bytes
     * @return an array of 'len' long
     */
    public synchronized static native long[] byteToLong(int start, int len, byte[] data);

    /**
     * Convert 8 bytes from an array of bytes into a single long
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param data
     *            The input array of bytes
     * @return The long value of the bytes.
     */
    public synchronized static long byteToLong(byte[] data, int start)
    {
        long[] lval = new long[1];
        lval        = byteToLong(start, 1, data);
        return (lval[0]);
    }

    /**
     * Convert a range from an array of bytes into an array of double
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param len
     *            The number of 'double' to convert
     * @param data
     *            The input array of bytes
     * @return an array of 'len' double
     */
    public synchronized static native double[] byteToDouble(int start, int len, byte[] data);

    /**
     * Convert 8 bytes from an array of bytes into a single double
     *
     * @param start
     *            The position in the input array of bytes to start
     * @param data
     *            The input array of bytes
     * @return The double value of the bytes.
     */
    public synchronized static double byteToDouble(byte[] data, int start)
    {
        double[] dval = new double[1];
        dval          = byteToDouble(start, 1, data);
        return (dval[0]);
    }

    /**
     * Convert a range from an array of int into an array of bytes.
     *
     * @param start
     *            The position in the input array of int to start
     * @param len
     *            The number of 'int' to convert
     * @param data
     *            The input array of int
     * @return an array of bytes
     */
    public synchronized static native byte[] intToByte(int start, int len, int[] data);

    /**
     * Convert a range from an array of short into an array of bytes.
     *
     * @param start
     *            The position in the input array of int to start
     * @param len
     *            The number of 'short' to convert
     * @param data
     *            The input array of short
     * @return an array of bytes
     */
    public synchronized static native byte[] shortToByte(int start, int len, short[] data);

    /**
     * Convert a range from an array of float into an array of bytes.
     *
     * @param start
     *            The position in the input array of int to start
     * @param len
     *            The number of 'float' to convert
     * @param data
     *            The input array of float
     * @return an array of bytes
     */
    public synchronized static native byte[] floatToByte(int start, int len, float[] data);

    /**
     * Convert a range from an array of long into an array of bytes.
     *
     * @param start
     *            The position in the input array of int to start
     * @param len
     *            The number of 'long' to convert
     * @param data
     *            The input array of long
     * @return an array of bytes
     */
    public synchronized static native byte[] longToByte(int start, int len, long[] data);

    /**
     * Convert a range from an array of double into an array of bytes.
     *
     * @param start
     *            The position in the input array of double to start
     * @param len
     *            The number of 'double' to convert
     * @param data
     *            The input array of double
     * @return an array of bytes
     */
    public synchronized static native byte[] doubleToByte(int start, int len, double[] data);

    /**
     * Convert a single byte into an array of one byte.
     * <p>
     * (This is a trivial method.)
     *
     * @param data
     *            The input byte
     * @return an array of bytes
     */
    public synchronized static native byte[] byteToByte(byte data);

    /**
     * Convert a single Byte object into an array of one byte.
     * <p>
     * (This is an almost trivial method.)
     *
     * @param data
     *            The input Byte
     * @return an array of bytes
     */
    public synchronized static byte[] byteToByte(Byte data) { return byteToByte(data.byteValue()); }

    /**
     * Convert a single int into an array of 4 bytes.
     *
     * @param data
     *            The input int
     * @return an array of bytes
     */
    public synchronized static native byte[] intToByte(int data);

    /**
     * Convert a single Integer object into an array of 4 bytes.
     *
     * @param data
     *            The input Integer
     * @return an array of bytes
     */
    public synchronized static byte[] intToByte(Integer data) { return intToByte(data.intValue()); }

    /**
     * Convert a single short into an array of 2 bytes.
     *
     * @param data
     *            The input short
     * @return an array of bytes
     */
    public synchronized static native byte[] shortToByte(short data);

    /**
     * Convert a single Short object into an array of 2 bytes.
     *
     * @param data
     *            The input Short
     * @return an array of bytes
     */
    public synchronized static byte[] shortToByte(Short data) { return shortToByte(data.shortValue()); }

    /**
     * Convert a single float into an array of 4 bytes.
     *
     * @param data
     *            The input float
     * @return an array of bytes
     */
    public synchronized static native byte[] floatToByte(float data);

    /**
     * Convert a single Float object into an array of 4 bytes.
     *
     * @param data
     *            The input Float
     * @return an array of bytes
     */
    public synchronized static byte[] floatToByte(Float data) { return floatToByte(data.floatValue()); };

    /**
     * Convert a single long into an array of 8 bytes.
     *
     * @param data
     *            The input long
     * @return an array of bytes
     */
    public synchronized static native byte[] longToByte(long data);

    /**
     * Convert a single Long object into an array of 8 bytes.
     *
     * @param data
     *            The input Long
     * @return an array of bytes
     */
    public synchronized static byte[] longToByte(Long data) { return longToByte(data.longValue()); }

    /**
     * Convert a single double into an array of 8 bytes.
     *
     * @param data
     *            The input double
     * @return an array of bytes
     */
    public synchronized static native byte[] doubleToByte(double data);

    /**
     * Convert a single Double object into an array of 8 bytes.
     *
     * @param data
     *            The input Double
     * @return an array of bytes
     */
    public synchronized static byte[] doubleToByte(Double data) { return doubleToByte(data.doubleValue()); }

    /**
     * Create a Number object from an array of bytes.
     *
     * @param barray
     *            The bytes to be converted
     * @param obj
     *            Input object of the desired output class. Must be a sub-class
     *            of Number.
     * @return A Object of the type of obj.
     *
     * @exception HDF5Exception
     *                - Error unsupported type.
     */
    public synchronized static Object byteToNumber(byte[] barray, Object obj) throws HDF5Exception
    {
        Class theClass = obj.getClass();
        String type    = theClass.getName();
        Object retobj  = null;

        if (type.equals("java.lang.Integer")) {
            int[] i = hdf.hdf5lib.HDFNativeData.byteToInt(0, 1, barray);
            retobj  = Integer.valueOf(i[0]);
        }
        else if (type.equals("java.lang.Byte")) {
            retobj = Byte.valueOf(barray[0]);
        }
        else if (type.equals("java.lang.Short")) {
            short[] f = hdf.hdf5lib.HDFNativeData.byteToShort(0, 1, barray);
            retobj    = Short.valueOf(f[0]);
        }
        else if (type.equals("java.lang.Float")) {
            float[] f = hdf.hdf5lib.HDFNativeData.byteToFloat(0, 1, barray);
            retobj    = Float.valueOf(f[0]);
        }
        else if (type.equals("java.lang.Long")) {
            long[] f = hdf.hdf5lib.HDFNativeData.byteToLong(0, 1, barray);
            retobj   = Long.valueOf(f[0]);
        }
        else if (type.equals("java.lang.Double")) {
            double[] f = hdf.hdf5lib.HDFNativeData.byteToDouble(0, 1, barray);
            retobj     = Double.valueOf(f[0]);
        }
        else {
            /* exception: unsupported type */
            HDF5Exception ex = new HDF5JavaException("byteToNumber: setfield bad type: " + obj + " " + type);
            throw(ex);
        }
        return (retobj);
    }
}