File: HDF5.java

package info (click to toggle)
libsis-jhdf5-java 19.04.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,668 kB
  • sloc: java: 79,644; ansic: 18,986; sh: 309; makefile: 49; xml: 12
file content (2301 lines) | stat: -rw-r--r-- 82,341 bytes parent folder | download | duplicates (2)
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
/*
 * Copyright 2007 - 2018 ETH Zuerich, CISD and SIS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ch.systemsx.cisd.hdf5;

import static hdf.hdf5lib.H5.*;
import static ch.systemsx.cisd.hdf5.hdf5lib.HDFHelper.H5Pset_mdc_image_config;
import static ch.systemsx.cisd.hdf5.hdf5lib.HDFHelper.H5Pget_mdc_image_enabled;
import static hdf.hdf5lib.HDF5Constants.H5_INDEX_NAME;
import static hdf.hdf5lib.HDF5Constants.H5_ITER_NATIVE;
import static hdf.hdf5lib.HDF5Constants.H5D_CHUNKED;
import static hdf.hdf5lib.HDF5Constants.H5D_COMPACT;
import static hdf.hdf5lib.HDF5Constants.H5D_FILL_TIME_ALLOC;
import static hdf.hdf5lib.HDF5Constants.H5F_ACC_RDONLY;
import static hdf.hdf5lib.HDF5Constants.H5F_ACC_RDWR;
import static hdf.hdf5lib.HDF5Constants.H5F_ACC_TRUNC;
import static hdf.hdf5lib.HDF5Constants.H5F_SCOPE_GLOBAL;
import static hdf.hdf5lib.HDF5Constants.H5O_TYPE_GROUP;
import static hdf.hdf5lib.HDF5Constants.H5P_ATTRIBUTE_CREATE;
import static hdf.hdf5lib.HDF5Constants.H5P_DATASET_CREATE;
import static hdf.hdf5lib.HDF5Constants.H5P_DEFAULT;
import static hdf.hdf5lib.HDF5Constants.H5P_FILE_ACCESS;
import static hdf.hdf5lib.HDF5Constants.H5P_GROUP_CREATE;
import static hdf.hdf5lib.HDF5Constants.H5P_LINK_CREATE;
import static hdf.hdf5lib.HDF5Constants.H5R_OBJECT;
import static hdf.hdf5lib.HDF5Constants.H5S_ALL;
import static hdf.hdf5lib.HDF5Constants.H5S_MAX_RANK;
import static hdf.hdf5lib.HDF5Constants.H5S_SCALAR;
import static hdf.hdf5lib.HDF5Constants.H5S_SELECT_SET;
import static hdf.hdf5lib.HDF5Constants.H5S_UNLIMITED;
import static hdf.hdf5lib.HDF5Constants.H5T_ARRAY;
import static hdf.hdf5lib.HDF5Constants.H5T_COMPOUND;
import static hdf.hdf5lib.HDF5Constants.H5T_C_S1;
import static hdf.hdf5lib.HDF5Constants.H5T_ENUM;
import static hdf.hdf5lib.HDF5Constants.H5T_FLOAT;
import static hdf.hdf5lib.HDF5Constants.H5T_INTEGER;
import static hdf.hdf5lib.HDF5Constants.H5T_OPAQUE;
import static hdf.hdf5lib.HDF5Constants.H5T_OPAQUE_TAG_MAX;
import static hdf.hdf5lib.HDF5Constants.H5T_SGN_NONE;
import static hdf.hdf5lib.HDF5Constants.H5T_STD_I16LE;
import static hdf.hdf5lib.HDF5Constants.H5T_STD_I32LE;
import static hdf.hdf5lib.HDF5Constants.H5T_STD_I8LE;
import static hdf.hdf5lib.HDF5Constants.H5T_STR_NULLPAD;
import static hdf.hdf5lib.HDF5Constants.H5T_VARIABLE;
import static hdf.hdf5lib.HDF5Constants.H5Z_SO_FLOAT_DSCALE;
import static hdf.hdf5lib.HDF5Constants.H5Z_SO_INT;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import hdf.hdf5lib.H5;
import hdf.hdf5lib.HDF5Constants;
import hdf.hdf5lib.HDFNativeData;
import hdf.hdf5lib.exceptions.HDF5Exception;
import hdf.hdf5lib.exceptions.HDF5JavaException;

import hdf.hdf5lib.structs.H5O_info_t;
import ch.systemsx.cisd.base.convert.NativeData;
import ch.systemsx.cisd.base.mdarray.MDAbstractArray;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator.FileFormatVersion;
import ch.systemsx.cisd.hdf5.IHDF5WriterConfigurator.FileFormatVersionBounds;
import ch.systemsx.cisd.hdf5.cleanup.CleanUpCallable;
import ch.systemsx.cisd.hdf5.cleanup.CleanUpRegistry;
import ch.systemsx.cisd.hdf5.cleanup.ICallableWithCleanUp;
import ch.systemsx.cisd.hdf5.cleanup.ICleanUpRegistry;
import ch.systemsx.cisd.hdf5.exceptions.HDF5SpaceRankMismatch;
import ch.systemsx.cisd.hdf5.hdf5lib.HDFHelper;

/**
 * A wrapper around {@link hdf.hdf5lib.H5General} that handles closing of resources automatically by means of registering clean-up {@link Runnable}s.
 * 
 * @author Bernd Rinn
 */
class HDF5
{

    private final static int MAX_PATH_LENGTH = 16384;

    private final CleanUpCallable runner;

    private final long dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc;

    private final long dataSetCreationPropertyListFillTimeAlloc;

    private final long numericConversionXferPropertyListID;

    private final long lcplCreateIntermediateGroups;

    private final boolean useUTF8CharEncoding;

    private final boolean autoDereference;

    public HDF5(final CleanUpRegistry fileRegistry, final CleanUpCallable runner,
            final boolean performNumericConversions, final boolean useUTF8CharEncoding,
            final boolean autoDereference)
    {
        this.runner = runner;
        this.useUTF8CharEncoding = useUTF8CharEncoding;
        this.autoDereference = autoDereference;
        this.dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc =
                createDataSetCreationPropertyList(fileRegistry);
        H5Pset_layout(dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc, H5D_COMPACT);
        this.dataSetCreationPropertyListFillTimeAlloc =
                createDataSetCreationPropertyList(fileRegistry);
        if (performNumericConversions)
        {
            this.numericConversionXferPropertyListID =
                    createDataSetXferPropertyListAbortOverflow(fileRegistry);
        } else
        {
            this.numericConversionXferPropertyListID =
                    createDataSetXferPropertyListAbort(fileRegistry);
        }
        this.lcplCreateIntermediateGroups = createLinkCreationPropertyList(true, fileRegistry);

    }

    private static void checkMaxLength(String path) throws HDF5JavaException
    {
        if (path.length() > MAX_PATH_LENGTH)
        {
            throw new HDF5JavaException("Path too long (length=" + path.length() + ")");
        }
    }
    
    //
    // Library
    //
    
    public static void resetLibrary()
    {
        synchronized (H5.class)
        {
            H5.H5close();
            H5.H5open();
            H5.H5error_off();
        }
    }

    //
    // File
    //

    public long createFile(String fileName, FileFormatVersionBounds fileFormatVersionBounds, 
            Boolean mdcGenerateImage, ICleanUpRegistry registry)
    {
        final long fileAccessPropertyListId =
                createFileAccessPropertyListId(fileFormatVersionBounds, mdcGenerateImage, registry);
        final long fileId =
                H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, fileAccessPropertyListId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Fclose(fileId);
                }
            });
        return fileId;
    }

    private long createFileAccessPropertyListId(FileFormatVersionBounds fileFormatVersionBounds, 
            boolean mdcGenerateImage, ICleanUpRegistry registry)
    {
        long fileAccessPropertyListId = H5P_DEFAULT;
        // MDC image generation is incompatible with low file format bound EARLIEST, thus raise it to V1_8.
        if (mdcGenerateImage && (fileFormatVersionBounds.getLowBound() == FileFormatVersion.EARLIEST))
        {
            switch (fileFormatVersionBounds)
            {
                case EARLIEST_LATEST:
                    fileFormatVersionBounds = FileFormatVersionBounds.V1_8_LATEST;
                    break;
                case EARLIEST_V1_10:
                    fileFormatVersionBounds = FileFormatVersionBounds.V1_8_V1_10;
                    break;
                case EARLIEST_V1_8:
                    throw new HDF5JavaException("Upper file version bound V1_8 is incompatible with MDC image generation.");
                default:
                    throw new IllegalStateException("Unhandled case switch");
            }
        }
        if (fileFormatVersionBounds != FileFormatVersionBounds.getDefault() || mdcGenerateImage)
        {
            final long fapl = H5Pcreate(H5P_FILE_ACCESS);
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Pclose(fapl);
                    }
                });
            fileAccessPropertyListId = fapl;
            if (fileFormatVersionBounds != FileFormatVersionBounds.getDefault())
            {
                H5Pset_libver_bounds(fileAccessPropertyListId, 
                        fileFormatVersionBounds.getLowBound().getHdf5Constant(), 
                        fileFormatVersionBounds.getHighBound().getHdf5Constant());
            }
            if (mdcGenerateImage)
            {
                H5Pset_mdc_image_config(fileAccessPropertyListId, mdcGenerateImage);
            }
        }
        return fileAccessPropertyListId;
    }
    
    /**
     * @return if the generation of a metadata image is enabled for <code>fileId</code>.
     */
    public boolean isMDCImageGenerationEnabled(long fileId)
    {
        final long fapl = H5Fget_access_plist(fileId);
        return H5Pget_mdc_image_enabled(fapl);
        
    }

    public long openFileReadOnly(String fileName, ICleanUpRegistry registry)
    {
        final long fileId = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Fclose(fileId);
                }
            });
        return fileId;
    }

    public long openFileReadWrite(String fileName, FileFormatVersionBounds fileFormatVersionBounds, 
            Boolean mdcGenerateImage, ICleanUpRegistry registry)
    {
        final long fileAccessPropertyListId = createFileAccessPropertyListId(fileFormatVersionBounds, mdcGenerateImage, registry);
        final File f = new File(fileName);
        if (f.exists() && f.isFile() == false)
        {
            throw new HDF5Exception("An entry with name '" + fileName
                    + "' exists but is not a file.");
        }
        final long fileId = H5Fopen(fileName, H5F_ACC_RDWR, fileAccessPropertyListId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Fclose(fileId);
                }
            });
        return fileId;
    }

    public void flushFile(long fileId)
    {
        H5Fflush(fileId, H5F_SCOPE_GLOBAL);
    }

    //
    // Object
    //

    public long openObject(long fileId, String path, ICleanUpRegistry registry)
    {
        checkMaxLength(path);
        final long objectId =
                isReference(path) ? H5Oopen_by_addr(fileId, Long.parseLong(path.substring(1)))
                        : H5Oopen(fileId, path, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Oclose(objectId);
                }
            });
        return objectId;
    }

    public int deleteObject(long fileId, String path)
    {
        checkMaxLength(path);
        H5Ldelete(fileId, path, H5P_DEFAULT);
        return 0;
    }

    public int copyObject(long srcFileId, String srcPath, long dstFileId, String dstPath)
    {
        checkMaxLength(srcPath);
        checkMaxLength(dstPath);
        final int success = 0;
        H5Ocopy(srcFileId, srcPath, dstFileId, dstPath, H5P_DEFAULT, lcplCreateIntermediateGroups);
        return success;
    }

    public int moveLink(long fileId, String srcLinkPath, String dstLinkPath)
    {
        checkMaxLength(srcLinkPath);
        checkMaxLength(dstLinkPath);
        final int success = 0;
        H5Lmove(fileId, srcLinkPath, fileId, dstLinkPath, lcplCreateIntermediateGroups,
                H5P_DEFAULT);
        return success;
    }

    //
    // Group
    //

    public void createGroup(long fileId, String groupName)
    {
        checkMaxLength(groupName);
        final long groupId =
                H5Gcreate(fileId, groupName, lcplCreateIntermediateGroups, H5P_DEFAULT, H5P_DEFAULT);
        H5Gclose(groupId);
    }

    public void createOldStyleGroup(long fileId, String groupName, int sizeHint,
            ICleanUpRegistry registry)
    {
        checkMaxLength(groupName);
        final long gcplId = H5Pcreate(H5P_GROUP_CREATE);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(gcplId);
                }
            });
        H5Pset_local_heap_size_hint(gcplId, sizeHint);
        final long groupId =
                H5Gcreate(fileId, groupName, lcplCreateIntermediateGroups, gcplId, H5P_DEFAULT);
        H5Gclose(groupId);
    }

    public void createNewStyleGroup(long fileId, String groupName, int maxCompact, int minDense,
            ICleanUpRegistry registry)
    {
        checkMaxLength(groupName);
        final long gcplId = H5Pcreate(H5P_GROUP_CREATE);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(gcplId);
                }
            });
        H5Pset_link_phase_change(gcplId, maxCompact, minDense);
        final long groupId =
                H5Gcreate(fileId, groupName, lcplCreateIntermediateGroups, gcplId, H5P_DEFAULT);
        H5Gclose(groupId);
    }

    public long openGroup(long fileId, String path, ICleanUpRegistry registry)
    {
        checkMaxLength(path);
        final long groupId = isReference(path) ? H5Oopen_by_addr(fileId, Long.parseLong(path.substring(1)))
                : H5Gopen(fileId, path, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Gclose(groupId);
                }
            });
        return groupId;
    }

    public long getNumberOfGroupMembers(long fileId, String path, ICleanUpRegistry registry)
    {
        checkMaxLength(path);
        final long groupId = H5Gopen(fileId, path, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Gclose(groupId);
                }
            });
        return H5Gget_info(groupId).nlinks;
    }

    public boolean existsAttribute(final long objectId, final String attributeName)
    {
        checkMaxLength(attributeName);
        return H5Aexists(objectId, attributeName);
    }

    public boolean exists(final long fileId, final String linkName)
    {
        checkMaxLength(linkName);
        return HDFHelper.H5Lexists(fileId, linkName, H5P_DEFAULT);
    }

    public HDF5LinkInformation getLinkInfo(final long fileId, final String objectName,
            boolean exceptionIfNonExistent)
    {
        checkMaxLength(objectName);
        if ("/".equals(objectName))
        {
            return HDF5LinkInformation.ROOT_LINK_INFO;
        }
        final String[] lname = new String[2];
        final int typeId = HDFHelper.H5Lget_link_info(fileId, objectName, lname, exceptionIfNonExistent);
        return HDF5LinkInformation.create(objectName, typeId, lname[1], lname[0]);
    }

    public HDF5ObjectType getLinkTypeInfo(final long fileId, final String objectName,
            boolean exceptionWhenNonExistent)
    {
        checkMaxLength(objectName);
        if ("/".equals(objectName))
        {
            return HDF5ObjectType.GROUP;
        }
        final int typeId = HDFHelper.H5Lget_link_info(fileId, objectName, null, exceptionWhenNonExistent);
        return HDF5CommonInformation.objectTypeIdToObjectType(typeId);
    }

    public HDF5ObjectInformation getObjectInfo(final long fileId, final String objectName,
            boolean exceptionWhenNonExistent)
    {
        checkMaxLength(objectName);
        final H5O_info_t info = HDFHelper.H5Oget_info_by_name(fileId, objectName, exceptionWhenNonExistent);
        return new HDF5ObjectInformation(objectName,
                HDF5CommonInformation.objectTypeIdToObjectType(info.type), info);
    }

    public int getObjectTypeId(final long fileId, final String objectName,
            boolean exceptionWhenNonExistent)
    {
        checkMaxLength(objectName);
        if ("/".equals(objectName))
        {
            return H5O_TYPE_GROUP;
        }
        return HDFHelper.H5Oget_info_by_name(fileId, objectName, exceptionWhenNonExistent).type;
    }

    public HDF5ObjectType getObjectTypeInfo(final long fileId, final String objectName,
            boolean exceptionWhenNonExistent)
    {
        return HDF5CommonInformation.objectTypeIdToObjectType(getObjectTypeId(fileId, objectName,
                exceptionWhenNonExistent));
    }

    public String[] getGroupMembers(final long fileId, final String groupName)
    {
        checkMaxLength(groupName);
        final ICallableWithCleanUp<String[]> dataDimensionRunnable =
                new ICallableWithCleanUp<String[]>()
                    {
                        @Override
                        public String[] call(ICleanUpRegistry registry)
                        {
                            final long groupId = openGroup(fileId, groupName, registry);
                            final long nLong = H5Gget_info(groupId).nlinks;
                            final int n = (int) nLong;
                            if (n != nLong)
                            {
                                throw new HDF5JavaException(
                                        "Number of group members is too large (n=" + nLong + ")");
                            }
                            final String[] names = new String[n];
                            HDFHelper.H5Lget_link_names_all(groupId, ".", names);
                            return names;
                        }
                    };
        return runner.call(dataDimensionRunnable);
    }

    public List<HDF5LinkInformation> getGroupMemberLinkInfo(final long fileId,
            final String groupName, final boolean includeInternal,
            final String houseKeepingNameSuffix)
    {
        checkMaxLength(groupName);
        final ICallableWithCleanUp<List<HDF5LinkInformation>> dataDimensionRunnable =
                new ICallableWithCleanUp<List<HDF5LinkInformation>>()
                    {
                        @Override
                        public List<HDF5LinkInformation> call(ICleanUpRegistry registry)
                        {
                            final long groupId = openGroup(fileId, groupName, registry);
                            final long nLong = H5Gget_info(groupId).nlinks;
                            final int n = (int) nLong;
                            if (n != nLong)
                            {
                                throw new HDF5JavaException(
                                        "Number of group members is too large (n=" + nLong + ")");
                            }
                            final String[] names = new String[n];
                            final String[] linkFilenames = new String[n];
                            final String[] linkTargets = new String[n];
                            final int[] types = new int[n];
                            HDFHelper.H5Lget_link_info_all(groupId, ".", names, types, linkFilenames, linkTargets);
                            final String superGroupName =
                                    (groupName.equals("/") ? "/" : groupName + "/");
                            final List<HDF5LinkInformation> info =
                                    new LinkedList<HDF5LinkInformation>();
                            for (int i = 0; i < n; ++i)
                            {
                                if (includeInternal
                                        || HDF5Utils.isInternalName(names[i],
                                                houseKeepingNameSuffix) == false)
                                {
                                    info.add(HDF5LinkInformation.create(superGroupName + names[i],
                                            types[i], linkFilenames[i], linkTargets[i]));
                                }
                            }
                            return info;
                        }
                    };
        return runner.call(dataDimensionRunnable);
    }

    public List<HDF5LinkInformation> getGroupMemberTypeInfo(final long fileId,
            final String groupName, final boolean includeInternal,
            final String houseKeepingNameSuffix)
    {
        checkMaxLength(groupName);
        final ICallableWithCleanUp<List<HDF5LinkInformation>> dataDimensionRunnable =
                new ICallableWithCleanUp<List<HDF5LinkInformation>>()
                    {
                        @Override
                        public List<HDF5LinkInformation> call(ICleanUpRegistry registry)
                        {
                            final long groupId = openGroup(fileId, groupName, registry);
                            final long nLong = H5Gget_info(groupId).nlinks;
                            final int n = (int) nLong;
                            if (n != nLong)
                            {
                                throw new HDF5JavaException(
                                        "Number of group members is too large (n=" + nLong + ")");
                            }
                            final String[] names = new String[n];
                            final int[] types = new int[n];
                            HDFHelper.H5Lget_link_info_all(groupId, ".", names, types, null, null);
                            final String superGroupName =
                                    (groupName.equals("/") ? "/" : groupName + "/");
                            final List<HDF5LinkInformation> info =
                                    new LinkedList<HDF5LinkInformation>();
                            for (int i = 0; i < n; ++i)
                            {
                                if (includeInternal
                                        || HDF5Utils.isInternalName(names[i],
                                                houseKeepingNameSuffix) == false)
                                {
                                    info.add(HDF5LinkInformation.create(superGroupName + names[i],
                                            types[i], null, null));
                                }
                            }
                            return info;
                        }
                    };
        return runner.call(dataDimensionRunnable);
    }

    //
    // Link
    //

    public void createHardLink(long fileId, String objectName, String linkName)
    {
        checkMaxLength(objectName);
        checkMaxLength(linkName);
        H5Lcreate_hard(fileId, objectName, fileId, linkName, lcplCreateIntermediateGroups,
                H5P_DEFAULT);
    }

    public void createSoftLink(long fileId, String linkName, String targetPath)
    {
        checkMaxLength(linkName);
        checkMaxLength(targetPath);
        H5Lcreate_soft(targetPath, fileId, linkName, lcplCreateIntermediateGroups, H5P_DEFAULT);
    }

    public void createExternalLink(long fileId, String linkName, String targetFileName,
            String targetPath)
    {
        checkMaxLength(linkName);
        checkMaxLength(targetFileName);
        checkMaxLength(targetPath);
        H5Lcreate_external(targetFileName, targetPath, fileId, linkName,
                lcplCreateIntermediateGroups, H5P_DEFAULT);
    }

    //
    // Data Set
    //

    public void writeStringVL(long dataSetId, long dataTypeId, String[] value)
    {
        H5DwriteVL(dataSetId, dataTypeId, H5S_ALL, H5S_ALL, H5P_DEFAULT, value);
    }

    public void writeStringVL(long dataSetId, long dataTypeId, long memorySpaceId, long fileSpaceId,
            String[] value)
    {
        H5DwriteVL(dataSetId, dataTypeId, memorySpaceId, fileSpaceId, H5P_DEFAULT, value);
    }

    public long createDataSet(long fileId, long[] dimensions, long[] chunkSizeOrNull, long dataTypeId,
            HDF5AbstractStorageFeatures compression, String dataSetName, HDF5StorageLayout layout,
            ICleanUpRegistry registry)
    {
        checkMaxLength(dataSetName);
        final long dataSpaceId =
                H5Screate_simple(dimensions.length, dimensions,
                        createMaxDimensions(dimensions, (layout == HDF5StorageLayout.CHUNKED)));
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        final long dataSetCreationPropertyListId;
        if (layout == HDF5StorageLayout.CHUNKED && chunkSizeOrNull != null)
        {
            dataSetCreationPropertyListId = createDataSetCreationPropertyList(registry);
            setChunkedLayout(dataSetCreationPropertyListId, chunkSizeOrNull);
            if (compression.isScaling())
            {
                final int classTypeId = getClassType(dataTypeId);
                assert compression.isCompatibleWithDataClass(classTypeId);
                if (classTypeId == H5T_INTEGER)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_INT,
                            compression.getScalingFactor());
                } else if (classTypeId == H5T_FLOAT)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_FLOAT_DSCALE,
                            compression.getScalingFactor());
                }
            }
            if (compression.isShuffleBeforeDeflate())
            {
                setShuffle(dataSetCreationPropertyListId);
            }
            if (compression.isDeflating())
            {
                setDeflate(dataSetCreationPropertyListId, compression.getDeflateLevel());
            }
        } else if (layout == HDF5StorageLayout.COMPACT)
        {
            dataSetCreationPropertyListId =
                    dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc;
        } else
        {
            dataSetCreationPropertyListId = dataSetCreationPropertyListFillTimeAlloc;
        }
        final long dataSetId =
                H5Dcreate(fileId, dataSetName, dataTypeId, dataSpaceId,
                        lcplCreateIntermediateGroups, dataSetCreationPropertyListId, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Dclose(dataSetId);
                }
            });

        return dataSetId;
    }

    public HDF5DataSet createDataSetDetached(HDF5BaseWriter baseWriter, long[] dimensions, long[] chunkSizeOrNull, long dataTypeId,
            HDF5AbstractStorageFeatures compression, String dataSetName, HDF5StorageLayout layout,
            ICleanUpRegistry registry)
    {
        checkMaxLength(dataSetName);
        final long[] maxDimensions = createMaxDimensions(dimensions, (layout == HDF5StorageLayout.CHUNKED)); 
        final long dataSpaceId =
                H5Screate_simple(dimensions.length, dimensions, maxDimensions);
        final long dataSetCreationPropertyListId;
        if (layout == HDF5StorageLayout.CHUNKED && chunkSizeOrNull != null)
        {
            dataSetCreationPropertyListId = createDataSetCreationPropertyList(registry);
            setChunkedLayout(dataSetCreationPropertyListId, chunkSizeOrNull);
            if (compression.isScaling())
            {
                final int classTypeId = getClassType(dataTypeId);
                assert compression.isCompatibleWithDataClass(classTypeId);
                if (classTypeId == H5T_INTEGER)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_INT,
                            compression.getScalingFactor());
                } else if (classTypeId == H5T_FLOAT)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_FLOAT_DSCALE,
                            compression.getScalingFactor());
                }
            }
            if (compression.isShuffleBeforeDeflate())
            {
                setShuffle(dataSetCreationPropertyListId);
            }
            if (compression.isDeflating())
            {
                setDeflate(dataSetCreationPropertyListId, compression.getDeflateLevel());
            }
        } else if (layout == HDF5StorageLayout.COMPACT)
        {
            dataSetCreationPropertyListId =
                    dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc;
        } else
        {
            dataSetCreationPropertyListId = dataSetCreationPropertyListFillTimeAlloc;
        }
        final long dataSetId =
                H5Dcreate(baseWriter.fileId, dataSetName, dataTypeId, dataSpaceId,
                        lcplCreateIntermediateGroups, dataSetCreationPropertyListId, H5P_DEFAULT);

        return new HDF5DataSet(baseWriter, dataSetName, dataSetId, dataSpaceId, dimensions, 
                    maxDimensions, layout, true);
    }

    public HDF5DataSetTemplate createDataSetTemplateLowLevel(long fileId, long[] dimensions,
            long[] chunkSizeOrNull, long dataTypeId, HDF5AbstractStorageFeatures compression,
            HDF5StorageLayout layout, FileFormatVersionBounds fileFormat)
    {
        final long[] maxDimensions = createMaxDimensions(dimensions, (layout == HDF5StorageLayout.CHUNKED));
        final long dataSpaceId =
                H5Screate_simple(dimensions.length, dimensions, maxDimensions);
        final long dataSetCreationPropertyListId;
        final boolean closeCreationPropertyListId;
        if (layout == HDF5StorageLayout.CHUNKED && chunkSizeOrNull != null)
        {
            dataSetCreationPropertyListId = createDataSetCreationPropertyList(null);
            closeCreationPropertyListId = true;
            setChunkedLayout(dataSetCreationPropertyListId, chunkSizeOrNull);
            if (compression.isScaling())
            {
                final int classTypeId = getClassType(dataTypeId);
                assert compression.isCompatibleWithDataClass(classTypeId);
                if (classTypeId == H5T_INTEGER)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_INT,
                            compression.getScalingFactor());
                } else if (classTypeId == H5T_FLOAT)
                {
                    H5Pset_scaleoffset(dataSetCreationPropertyListId, H5Z_SO_FLOAT_DSCALE,
                            compression.getScalingFactor());
                }
            }
            if (compression.isShuffleBeforeDeflate())
            {
                setShuffle(dataSetCreationPropertyListId);
            }
            if (compression.isDeflating())
            {
                setDeflate(dataSetCreationPropertyListId, compression.getDeflateLevel());
            }
        } else if (layout == HDF5StorageLayout.COMPACT)
        {
            dataSetCreationPropertyListId =
                    dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc;
            closeCreationPropertyListId = false;
        } else
        {
            dataSetCreationPropertyListId = dataSetCreationPropertyListFillTimeAlloc;
            closeCreationPropertyListId = false;
        }

        return new HDF5DataSetTemplate(dataSpaceId, dataSetCreationPropertyListId,
                closeCreationPropertyListId, dataTypeId, dimensions, maxDimensions, layout);
    }

    public long createDataSetSimple(long fileId, long dataTypeId, long dataSpaceId, 
            long dataSetCreationPropertyListId, 
            String dataSetName, ICleanUpRegistry registryOrNull)
    {
        final long dataSetId =
                H5Dcreate(fileId, dataSetName, dataTypeId, dataSpaceId,
                        lcplCreateIntermediateGroups, dataSetCreationPropertyListId,
                        H5P_DEFAULT);
        if (registryOrNull != null)
        {
            registryOrNull.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Dclose(dataSetId);
                    }
                });
    
        }
        return dataSetId;
    }

    private long createDataSetCreationPropertyList(ICleanUpRegistry registry)
    {
        final long dataSetCreationPropertyListId = H5Pcreate(H5P_DATASET_CREATE);
        if (registry != null)
        {
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Pclose(dataSetCreationPropertyListId);
                    }
                });
        }
        H5Pset_fill_time(dataSetCreationPropertyListId, H5D_FILL_TIME_ALLOC);
        return dataSetCreationPropertyListId;
    }

    /**
     * Returns one of: COMPACT, CHUNKED, CONTIGUOUS.
     */
    public HDF5StorageLayout getLayout(long dataSetId, ICleanUpRegistry registry)
    {
        final long dataSetCreationPropertyListId = getCreationPropertyList(dataSetId, registry);
        final int layoutId = H5Pget_layout(dataSetCreationPropertyListId);
        if (layoutId == H5D_COMPACT)
        {
            return HDF5StorageLayout.COMPACT;
        } else if (layoutId == H5D_CHUNKED)
        {
            return HDF5StorageLayout.CHUNKED;
        } else
        {
            return HDF5StorageLayout.CONTIGUOUS;
        }
    }

    private long getCreationPropertyList(long dataSetId, ICleanUpRegistry registry)
    {
        final long dataSetCreationPropertyListId = H5Dget_create_plist(dataSetId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(dataSetCreationPropertyListId);
                }
            });
        return dataSetCreationPropertyListId;
    }

    private static final long[] createMaxDimensions(long[] dimensions, boolean unlimited)
    {
        if (unlimited == false)
        {
            return dimensions;
        }
        final long[] maxDimensions = new long[dimensions.length];
        Arrays.fill(maxDimensions, H5S_UNLIMITED);
        return maxDimensions;
    }

    private void setChunkedLayout(long dscpId, long[] chunkSize)
    {
        assert dscpId >= 0;

        H5Pset_layout(dscpId, H5D_CHUNKED);
        H5Pset_chunk(dscpId, chunkSize.length, chunkSize);
    }

    private void setShuffle(long dscpId)
    {
        assert dscpId >= 0;

        H5Pset_shuffle(dscpId);
    }

    private void setDeflate(long dscpId, int deflateLevel)
    {
        assert dscpId >= 0;
        assert deflateLevel >= 0;

        H5Pset_deflate(dscpId, deflateLevel);
    }

    public long createScalarDataSet(long fileId, long dataTypeId, String dataSetName,
            boolean compactLayout, ICleanUpRegistry registry)
    {
        checkMaxLength(dataSetName);
        final long dataSpaceId = H5Screate(H5S_SCALAR);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        final long dataSetId =
                H5Dcreate(
                        fileId,
                        dataSetName,
                        dataTypeId,
                        dataSpaceId,
                        lcplCreateIntermediateGroups,
                        compactLayout ? dataSetCreationPropertyListCompactStorageLayoutFileTimeAlloc
                                : dataSetCreationPropertyListFillTimeAlloc,
                        H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Dclose(dataSetId);
                }
            });
        return dataSetId;
    }

    public long openDataSet(long fileId, String path, ICleanUpRegistry registry)
    {
        checkMaxLength(path);
        final long dataSetId = isReference(path) ? H5Oopen_by_addr(fileId, Long.parseLong(path.substring(1)))
                : H5Dopen(fileId, path, H5P_DEFAULT);
        if (registry != null)
        {
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Dclose(dataSetId);
                    }
                });
        }
        return dataSetId;
    }

    boolean isReference(String path)
    {
        return autoDereference && (path.charAt(0) == '\0');
    }

    public long openAndExtendDataSet(long fileId, String path, FileFormatVersionBounds fileFormat,
            long[] newDimensions, boolean overwriteMode, ICleanUpRegistry registry)
            throws HDF5JavaException
    {
        checkMaxLength(path);
        final long dataSetId =
                isReference(path) ? H5Rdereference(fileId, H5P_DEFAULT, H5R_OBJECT, HDFNativeData.longToByte(Long.parseLong(path.substring(1))))
                        : H5Dopen(fileId, path, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Dclose(dataSetId);
                }
            });
        final long dataSpaceId = getDataSpaceForDataSet(dataSetId, registry);
        final int rank = getDataSpaceRank(dataSpaceId);
        final long[][] dimsMaxDims = getDataSpaceDimensionsAndMaxDimensions(dataSpaceId, rank);
        final long[] dataDimensions = dimsMaxDims[0];
        final long[] maxDimensions = dimsMaxDims[1];
        final HDF5StorageLayout layout = getLayout(dataSetId, registry);
        extendDataSet(dataSetId, dataSpaceId, rank, layout, dataDimensions, newDimensions, maxDimensions,
                overwriteMode, registry);
        return dataSetId;
    }

    public boolean extendDataSet(HDF5DataSet dataSet, long[] newDimensions,
            boolean overwriteMode, ICleanUpRegistry registry)
                    throws HDF5SpaceRankMismatch, HDF5JavaException
    {
        return extendDataSet(dataSet.getDataSetId(), dataSet.getDataSpaceId(),
                dataSet.getRank(), dataSet.getLayout(), dataSet.getDimensions(), newDimensions,
                dataSet.getMaxDimensions(), overwriteMode, registry);
    }

    public boolean extendDataSet(long dataSetId, long dataSpaceId, int rank,
            HDF5StorageLayout layout, long[] oldDimensions, long[] newDimensions,
            long[] maxDimensions, boolean overwriteMode, ICleanUpRegistry registry)
                    throws HDF5SpaceRankMismatch, HDF5JavaException
    {
        checkRank(rank, newDimensions.length);
        if (Arrays.equals(oldDimensions, newDimensions) == false)
        {
            if (layout == HDF5StorageLayout.CHUNKED)
            {
                // Safety check. JHDF5 creates CHUNKED data sets always with unlimited max
                // dimensions but we may have to work on a file we haven't created.
                if (areDimensionsInBounds(newDimensions, maxDimensions))
                {
                    setDataSetExtentChunked(dataSetId,
                            computeNewDimensions(oldDimensions, newDimensions, overwriteMode));
                    return true;
                } else
                {
                    throw new HDF5JavaException("New data set dimensions are out of bounds.");
                }
            } else if (overwriteMode)
            {
                throw new HDF5JavaException("Cannot change dimensions on non-extendable data set.");
            } else
            {
                final long dataTypeId = getDataTypeForDataSet(dataSetId, registry);
                if (getClassType(dataTypeId) == H5T_ARRAY)
                {
                    throw new HDF5JavaException("Cannot partially overwrite array type.");
                }
                if (HDF5Utils.isInBounds(oldDimensions, newDimensions) == false)
                {
                    throw new HDF5JavaException("New data set dimensions are out of bounds.");
                }
            }
        }
        return false;
    }

    public boolean extendDataSet(HDF5DataSet dataSet, long[] newDimensions,
            boolean overwriteMode) throws HDF5JavaException
    {
        final long dataSetId = dataSet.getDataSetId();
        final long[] oldDimensions = dataSet.getDimensions();
        if (Arrays.equals(oldDimensions, newDimensions) == false)
        {
            final HDF5StorageLayout layout = dataSet.getLayout();
            final long[] maxDimensions = dataSet.getMaxDimensions();
            if (layout == HDF5StorageLayout.CHUNKED)
            {
                // Safety check. JHDF5 creates CHUNKED data sets always with unlimited max
                // dimensions but we may have to work on a file we haven't created.
                if (areDimensionsInBounds(newDimensions, maxDimensions))
                {
                    setDataSetExtentChunked(dataSetId,
                            computeNewDimensions(oldDimensions, newDimensions, overwriteMode));
                    return true;
                } else
                {
                    throw new HDF5JavaException("New data set dimensions are out of bounds.");
                }
            } else if (overwriteMode)
            {
                throw new HDF5JavaException("Cannot change dimensions on non-extendable data set.");
            } else
            {
                long dataTypeId = dataSet.getDataTypeId();
                if (getClassType(dataTypeId) == H5T_ARRAY)
                {
                    throw new HDF5JavaException("Cannot partially overwrite array type.");
                }
                if (HDF5Utils.isInBounds(oldDimensions, newDimensions) == false)
                {
                    throw new HDF5JavaException("New data set dimensions are out of bounds.");
                }
            }
        }
        return false;
    }

    long[] computeNewDimensions(long[] oldDimensions, long[] newDimensions,
            boolean cutDownExtendIfNecessary)
    {
        if (cutDownExtendIfNecessary)
        {
            return newDimensions;
        } else
        {
            final long[] newUncutDimensions = new long[oldDimensions.length];
            for (int i = 0; i < newUncutDimensions.length; ++i)
            {
                newUncutDimensions[i] = Math.max(oldDimensions[i], newDimensions[i]);
            }
            return newUncutDimensions;
        }
    }

    void checkRank(int rankExpected, int rankFound) throws HDF5SpaceRankMismatch
    {
        assert rankExpected >= 0;
        assert rankFound >= 0;

        if (rankExpected != rankFound)
        {
            throw new HDF5SpaceRankMismatch(rankExpected, rankFound);
        }
    }

    /**
     * Checks whether the given <var>dimensions</var> are in bounds for <var>dataSetId</var>.
     */
    private boolean areDimensionsInBounds(final long[] dimensions, final long[] maxDimensions)
    {
        if (dimensions.length != maxDimensions.length) // Actually an error condition
        {
            return false;
        }

        for (int i = 0; i < dimensions.length; ++i)
        {
            if (maxDimensions[i] != H5S_UNLIMITED && dimensions[i] > maxDimensions[i])
            {
                return false;
            }
        }
        return true;
    }

    public void setDataSetExtentChunked(long dataSetId, long[] dimensions)
    {
        assert dataSetId >= 0;
        assert dimensions != null;

        H5Dset_extent(dataSetId, dimensions);
    }

    public void readDataSetNonNumeric(long dataSetId, long nativeDataTypeId, byte[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    }

    public void readDataSetNonNumeric(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, byte[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId, H5P_DEFAULT, data);
    }

    public void readDataSetString(long dataSetId, long nativeDataTypeId, String[] data)
    {
        H5Dread_string(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
    }

    public void readDataSetString(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, String[] data)
    {
        H5Dread_string(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId, H5P_DEFAULT, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, byte[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, short[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, int[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, float[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, double[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, H5S_ALL, H5S_ALL, numericConversionXferPropertyListID,
                data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, byte[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, short[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, int[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, long[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, float[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSet(long dataSetId, long nativeDataTypeId, long memorySpaceId,
            long fileSpaceId, double[] data)
    {
        H5Dread(dataSetId, nativeDataTypeId, memorySpaceId, fileSpaceId,
                numericConversionXferPropertyListID, data);
    }

    public void readDataSetVL(long dataSetId, long dataTypeId, String[] data)
    {
        H5DreadVL(dataSetId, dataTypeId, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
        replaceNullWithEmptyString(data);
    }

    public void readDataSetVL(long dataSetId, long dataTypeId, long memorySpaceId, long fileSpaceId,
            String[] data)
    {
        H5DreadVL(dataSetId, dataTypeId, memorySpaceId, fileSpaceId, H5P_DEFAULT, data);
        replaceNullWithEmptyString(data);
    }

    // A fixed-length string array returns uninitialized strings as "", a variable-length string as
    // null. We don't want the application programmer to have to be aware of this difference,
    // thus we replace null with "" here.
    private void replaceNullWithEmptyString(String[] data)
    {
        for (int i = 0; i < data.length; ++i)
        {
            if (data[i] == null)
            {
                data[i] = "";
            }
        }
    }

    //
    // Attribute
    //

    public long createAttribute(long locationId, String attributeName, long dataTypeId,
            long dataSpaceIdOrMinusOne, ICleanUpRegistry registry)
    {
        checkMaxLength(attributeName);
        final long dataSpaceId =
                (dataSpaceIdOrMinusOne == -1) ? H5Screate(H5S_SCALAR) : dataSpaceIdOrMinusOne;
        if (dataSpaceIdOrMinusOne == -1)
        {
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Sclose(dataSpaceId);
                    }
                });
        }
        final long attCreationPlistId;
        if (useUTF8CharEncoding)
        {
            attCreationPlistId = H5Pcreate(H5P_ATTRIBUTE_CREATE);
            setCharacterEncodingCreationPropertyList(attCreationPlistId, CharacterEncoding.UTF8);
        } else
        {
            attCreationPlistId = H5P_DEFAULT;
        }
        final long attributeId =
                H5Acreate(locationId, attributeName, dataTypeId, dataSpaceId, attCreationPlistId,
                        H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Aclose(attributeId);
                }
            });
        return attributeId;
    }

    public int deleteAttribute(long locationId, String attributeName)
    {
        checkMaxLength(attributeName);
        final int success = H5Adelete(locationId, attributeName);
        return success;
    }

    public long openAttribute(long locationId, String attributeName, ICleanUpRegistry registry)
    {
        checkMaxLength(attributeName);
        final long attributeId = H5Aopen(locationId, attributeName, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Aclose(attributeId);
                }
            });
        return attributeId;
    }

    public List<String> getAttributeNames(long locationId, ICleanUpRegistry registry)
    {
        final H5O_info_t info = H5Oget_info(locationId);
        final int numberOfAttributes = (int) info.num_attrs;
        final List<String> attributeNames = new LinkedList<String>();
        for (int i = 0; i < numberOfAttributes; ++i)
        {
            final long attributeId =
                    H5Aopen_by_idx(locationId, ".", H5_INDEX_NAME, H5_ITER_NATIVE, (long) i, H5P_DEFAULT, H5P_DEFAULT);
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Aclose(attributeId);
                    }
                });
            attributeNames.add(H5Aget_name(attributeId));
        }
        return attributeNames;
    }

    public byte[] readAttributeAsByteArray(long attributeId, long dataTypeId, int length)
    {
        final byte[] data = new byte[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public short[] readAttributeAsShortArray(long attributeId, long dataTypeId, int length)
    {
        final short[] data = new short[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public int[] readAttributeAsIntArray(long attributeId, long dataTypeId, int length)
    {
        final int[] data = new int[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public long[] readAttributeAsLongArray(long attributeId, long dataTypeId, int length)
    {
        final long[] data = new long[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public float[] readAttributeAsFloatArray(long attributeId, long dataTypeId, int length)
    {
        final float[] data = new float[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public double[] readAttributeAsDoubleArray(long attributeId, long dataTypeId, int length)
    {
        final double[] data = new double[length];
        H5Aread(attributeId, dataTypeId, data);
        return data;
    }

    public void readAttributeVL(long attributeId, long dataTypeId, String[] data)
    {
        H5AreadVL(attributeId, dataTypeId, data);
    }

    public void writeAttribute(long attributeId, long dataTypeId, byte[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttribute(long attributeId, long dataTypeId, short[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttribute(long attributeId, long dataTypeId, int[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttribute(long attributeId, long dataTypeId, long[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttribute(long attributeId, long dataTypeId, float[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttribute(long attributeId, long dataTypeId, double[] value)
    {
        H5Awrite(attributeId, dataTypeId, value);
    }

    public void writeAttributeStringVL(long attributeId, long dataTypeId, String[] value)
    {
        H5AwriteVL(attributeId, dataTypeId, value);
    }

    //
    // Data Type
    //

    public long copyDataType(long dataTypeId, ICleanUpRegistry registry)
    {
        final long copiedDataTypeId = H5Tcopy(dataTypeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(copiedDataTypeId);
                }
            });
        return copiedDataTypeId;
    }

    public long createDataTypeVariableString(ICleanUpRegistry registry)
    {
        final long dataTypeId = createDataTypeStringVariableLength();
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        if (useUTF8CharEncoding)
        {
            setCharacterEncodingDataType(dataTypeId, CharacterEncoding.UTF8);
        }
        return dataTypeId;
    }

    private long createDataTypeStringVariableLength()
    {
        long dataTypeId = H5Tcopy(H5T_C_S1);
        H5Tset_size(dataTypeId, H5T_VARIABLE);
        return dataTypeId;
    }

    public long createDataTypeString(int length, ICleanUpRegistry registry)
    {
        assert length > 0;

        final long dataTypeId = H5Tcopy(H5T_C_S1);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        H5Tset_size(dataTypeId, length);
        H5Tset_strpad(dataTypeId, H5T_STR_NULLPAD);
        if (useUTF8CharEncoding)
        {
            setCharacterEncodingDataType(dataTypeId, CharacterEncoding.UTF8);
        }
        return dataTypeId;
    }

    private void setCharacterEncodingDataType(long dataTypeId, CharacterEncoding encoding)
    {
        H5Tset_cset(dataTypeId, encoding.getCValue());
    }

    public long createArrayType(long baseTypeId, int length, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Tarray_create(baseTypeId, 1, new long[] { length });
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    public long createArrayType(long baseTypeId, int[] dimensions, ICleanUpRegistry registry)
    {
        final long[] ldims = new long[dimensions.length];
        for (int i = 0; i < ldims.length; ++i)
        {
            ldims[i] = (long) dimensions[i];
        }
        final long dataTypeId = H5Tarray_create(baseTypeId, ldims.length, ldims);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    private enum EnumSize
    {
        BYTE8, SHORT16, INT32
    }

    public long createDataTypeEnum(String[] names, ICleanUpRegistry registry)
    {
        for (String name : names)
        {
            checkMaxLength(name);
        }
        final EnumSize size =
                (names.length < Byte.MAX_VALUE) ? EnumSize.BYTE8
                        : (names.length < Short.MAX_VALUE) ? EnumSize.SHORT16 : EnumSize.INT32;
        final long baseDataTypeId;
        switch (size)
        {
            case BYTE8:
                baseDataTypeId = H5T_STD_I8LE;
                break;
            case SHORT16:
                baseDataTypeId = H5T_STD_I16LE;
                break;
            case INT32:
                baseDataTypeId = H5T_STD_I32LE;
                break;
            default:
                throw new InternalError();
        }
        final long dataTypeId = H5Tenum_create(baseDataTypeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        switch (size)
        {
            case BYTE8:
                for (byte i = 0; i < names.length; ++i)
                {
                    insertMemberEnum(dataTypeId, names[i], i);
                }
                break;
            case SHORT16:
            {
                final short[] values = getLittleEndianSuccessiveShortValues(names);
                for (short i = 0; i < names.length; ++i)
                {
                    insertMemberEnum(dataTypeId, names[i], values[i]);
                }
                break;
            }
            case INT32:
            {
                final int[] values = getLittleEndianSuccessiveIntValues(names);
                for (int i = 0; i < names.length; ++i)
                {
                    insertMemberEnum(dataTypeId, names[i], values[i]);
                }
                break;
            }
        }
        return dataTypeId;
    }

    private short[] getLittleEndianSuccessiveShortValues(String[] names)
    {
        final short[] values = new short[names.length];
        final boolean swap = (NativeData.getNativeByteOrder() == NativeData.ByteOrder.BIG_ENDIAN);
        for (short i = 0; i < names.length; ++i)
        {
            values[i] = swap ? NativeData.changeByteOrder(i): i;
        }
        return values;
    }

    private int[] getLittleEndianSuccessiveIntValues(String[] names)
    {
        final int[] values = new int[names.length];
        final boolean swap = (NativeData.getNativeByteOrder() == NativeData.ByteOrder.BIG_ENDIAN);
        for (int i = 0; i < names.length; ++i)
        {
            values[i] = swap ? NativeData.changeByteOrder(i): i;
        }
        return values;
    }

    private void insertMemberEnum(long dataTypeId, String name, byte value)
    {
        assert dataTypeId >= 0;
        assert name != null;

        H5Tenum_insert(dataTypeId, name, value);
    }

    private void insertMemberEnum(long dataTypeId, String name, short value)
    {
        assert dataTypeId >= 0;
        assert name != null;

        H5Tenum_insert(dataTypeId, name, value);
    }

    private void insertMemberEnum(long dataTypeId, String name, int value)
    {
        assert dataTypeId >= 0;
        assert name != null;

        H5Tenum_insert(dataTypeId, name, value);
    }

    /** Returns the number of members of an enum type or a compound type. */
    public int getNumberOfMembers(long dataTypeId)
    {
        return H5Tget_nmembers(dataTypeId);
    }

    /**
     * Returns the name of an enum value or compound member for the given <var>index</var>.
     * <p>
     * Must not be called on a <var>dateTypeId</var> that is not an enum or compound type.
     */
    public String getNameForEnumOrCompoundMemberIndex(long dataTypeId, int index)
    {
        return H5Tget_member_name(dataTypeId, index);
    }

    /**
     * Returns the offset of a compound member for the given <var>index</var>.
     * <p>
     * Must not be called on a <var>dateTypeId</var> that is not a compound type.
     */
    public int getOffsetForCompoundMemberIndex(long dataTypeId, int index)
    {
        return (int) H5Tget_member_offset(dataTypeId, index);
    }

    /**
     * Returns the names of an enum value or compound members.
     * <p>
     * Must not be called on a <var>dateTypeId</var> that is not an enum or compound type.
     */
    public String[] getNamesForEnumOrCompoundMembers(long dataTypeId)
    {
        final int len = getNumberOfMembers(dataTypeId);
        final String[] values = new String[len];
        for (int i = 0; i < len; ++i)
        {
            values[i] = H5Tget_member_name(dataTypeId, i);
        }
        return values;
    }

    /**
     * Returns the index of an enum value or compound member for the given <var>name</var>. Works on enum and compound data types.
     */
    public int getIndexForMemberName(long dataTypeId, String name)
    {
        checkMaxLength(name);
        return H5Tget_member_index(dataTypeId, name);
    }

    /**
     * Returns the data type id for a member of a compound data type, specified by index.
     */
    public long getDataTypeForIndex(long compoundDataTypeId, int index, ICleanUpRegistry registry)
    {
        final long memberTypeId = H5Tget_member_type(compoundDataTypeId, index);
        registry.registerCleanUp(new Runnable()
            {

                @Override
                public void run()
                {
                    H5Tclose(memberTypeId);
                }
            });
        return memberTypeId;
    }

    /**
     * Returns the data type id for a member of a compound data type, specified by name.
     */
    public long getDataTypeForMemberName(long compoundDataTypeId, String memberName)
    {
        checkMaxLength(memberName);
        final int index = H5Tget_member_index(compoundDataTypeId, memberName);
        return H5Tget_member_type(compoundDataTypeId, index);
    }

    public Boolean tryGetBooleanValue(final long dataTypeId, final int intValue)
    {
        if (getClassType(dataTypeId) != H5T_ENUM)
        {
            return null;
        }
        final String value = getNameForEnumOrCompoundMemberIndex(dataTypeId, intValue);
        if ("TRUE".equalsIgnoreCase(value))
        {
            return true;
        } else if ("FALSE".equalsIgnoreCase(value))
        {
            return false;
        } else
        {
            return null;
        }
    }

    public long createDataTypeCompound(int lengthInBytes, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Tcreate(H5T_COMPOUND, lengthInBytes);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    public long createDataTypeOpaque(int lengthInBytes, String tag, ICleanUpRegistry registry)
    {
        checkMaxLength(tag);
        final long dataTypeId = H5Tcreate(H5T_OPAQUE, lengthInBytes);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        H5Tset_tag(dataTypeId,
                tag.length() > H5T_OPAQUE_TAG_MAX ? tag.substring(0, H5T_OPAQUE_TAG_MAX) : tag);
        return dataTypeId;
    }

    public void commitDataType(long fileId, String name, long dataTypeId)
    {
        checkMaxLength(name);
        H5Tcommit(fileId, name, dataTypeId, lcplCreateIntermediateGroups, H5P_DEFAULT, H5P_DEFAULT);
    }

    public long openDataType(long fileId, String name, ICleanUpRegistry registry)
    {
        checkMaxLength(name);
        final long dataTypeId = isReference(name) ? H5Oopen_by_addr(fileId, Long.parseLong(name.substring(1)))
                : H5Topen(fileId, name, H5P_DEFAULT);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    public boolean dataTypesAreEqual(long dataTypeId1, long dataTypeId2)
    {
        return H5Tequal(dataTypeId1, dataTypeId2);
    }

    public long getDataTypeForDataSet(long dataSetId, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Dget_type(dataSetId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    public long getDataTypeForAttribute(long attributeId, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Aget_type(attributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return dataTypeId;
    }

    public String tryGetOpaqueTag(long dataTypeId)
    {
        return H5Tget_tag(dataTypeId);
    }

    public long getNativeDataType(long dataTypeId, ICleanUpRegistry registry)
    {
        final long nativeDataTypeId = H5Tget_native_type(dataTypeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(nativeDataTypeId);
                }
            });
        return nativeDataTypeId;
    }

    public long getNativeDataTypeForDataSet(long dataSetId, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Dget_type(dataSetId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return getNativeDataType(dataTypeId, registry);
    }

    public long getNativeDataTypeForAttribute(long attributeId, ICleanUpRegistry registry)
    {
        final long dataTypeId = H5Aget_type(attributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(dataTypeId);
                }
            });
        return getNativeDataType(dataTypeId, registry);
    }

    public int getDataTypeSize(long dataTypeId)
    {
        return (int) H5Tget_size(dataTypeId);
    }

    public long getDataTypeSizeLong(long dataTypeId) throws HDF5JavaException
    {
        return H5Tget_size(dataTypeId);
    }

    public boolean isVariableLengthString(long dataTypeId)
    {
        return H5Tis_variable_str(dataTypeId);
    }

    public int getClassType(long dataTypeId)
    {
        return H5Tget_class(dataTypeId);
    }

    public CharacterEncoding getCharacterEncoding(long dataTypeId)
    {
        final int cValue = H5Tget_cset(dataTypeId);
        if (cValue == CharacterEncoding.ASCII.getCValue())
        {
            return CharacterEncoding.ASCII;
        } else if (cValue == CharacterEncoding.UTF8.getCValue())
        {
            return CharacterEncoding.UTF8;
        } else
        {
            throw new HDF5JavaException("Unknown character encoding cValue " + cValue);
        }
    }

    public boolean hasClassType(long dataTypeId, int classTypeId)
    {
        return H5Tdetect_class(dataTypeId, classTypeId);
    }

    public long getBaseDataType(long dataTypeId, ICleanUpRegistry registry)
    {
        final long baseDataTypeId = H5Tget_super(dataTypeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Tclose(baseDataTypeId);
                }
            });
        return baseDataTypeId;
    }

    public boolean getSigned(long dataTypeId)
    {
        return H5Tget_sign(dataTypeId) != H5T_SGN_NONE;
    }

    public String tryGetDataTypePath(long dataTypeId)
    {
        if (dataTypeId < 0 || H5Tcommitted(dataTypeId) == false)
        {
            return null;
        }
        return H5Iget_name(dataTypeId);
    }

    /**
     * Reclaims the variable-length data structures from a compound buffer, if any.
     */
    public void reclaimCompoundVL(HDF5CompoundType<?> type, byte[] buf)
    {
        int[] vlMemberIndices = type.getObjectByteifyer().getVLMemberIndices();
        if (vlMemberIndices.length > 0) // This type has variable-length data members
        {
            HDFHelper.freeCompoundVLStr(buf, type.getRecordSizeInMemory(), vlMemberIndices);
        }
    }

    //
    // Data Space
    //

    public long getDataSpaceForDataSet(long dataSetId, ICleanUpRegistry registry)
    {
        final long dataSpaceId = H5Dget_space(dataSetId);
        if (registry != null)
        {
            registry.registerCleanUp(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        H5Sclose(dataSpaceId);
                    }
                });
        }
        return dataSpaceId;
    }

    public long[] getDataDimensionsForAttribute(final long attributeId, ICleanUpRegistry registry)
    {
        final long dataSpaceId = H5Aget_space(attributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        final long[] dimensions = getDataSpaceDimensions(dataSpaceId);
        return dimensions;
    }

    public long[] getDataDimensions(final long dataSetId, ICleanUpRegistry registry)
    {
        final long dataSpaceId = H5Dget_space(dataSetId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        long[] dimensions = getDataSpaceDimensions(dataSpaceId);
        // Ensure backward compatibility with 8.10
        if (HDF5Utils.mightBeEmptyInStorage(dimensions)
                && existsAttribute(dataSetId, HDF5Utils.DATASET_IS_EMPTY_LEGACY_ATTRIBUTE))
        {
            dimensions = new long[dimensions.length];
        }
        return dimensions;
    }

    public int getDataSpaceRank(long dataSpaceId)
    {
        return H5Sget_simple_extent_ndims(dataSpaceId);
    }

    public long[] getDataSpaceDimensions(long dataSpaceId)
    {
        final int rank = H5Sget_simple_extent_ndims(dataSpaceId);
        return getDataSpaceDimensions(dataSpaceId, rank);
    }

    public long[] getDataSpaceDimensions(long dataSpaceId, int rank)
    {
        assert dataSpaceId >= 0;
        assert rank >= 0;

        final long[] dimensions = new long[rank];
        H5Sget_simple_extent_dims(dataSpaceId, dimensions, null);
        return dimensions;
    }

    public long[] getDataSpaceMaxDimensions(long dataSpaceId)
    {
        final int rank = H5Sget_simple_extent_ndims(dataSpaceId);
        return getDataSpaceMaxDimensions(dataSpaceId, rank);
    }

    public long[] getDataSpaceMaxDimensions(long dataSpaceId, int rank)
    {
        assert dataSpaceId >= 0;
        assert rank >= 0;

        final long[] maxDimensions = new long[rank];
        H5Sget_simple_extent_dims(dataSpaceId, null, maxDimensions);
        return maxDimensions;
    }

    public long[][] getDataSpaceDimensionsAndMaxDimensions(long dataSpaceId, int rank)
    {
        final long[][] dimsMaxDims = new long[2][rank];
        H5Sget_simple_extent_dims(dataSpaceId, dimsMaxDims[0], dimsMaxDims[1]);
        return dimsMaxDims;
    }

    /**
     * @param dataSetOrAttributeId The id of either the data set or the attribute to get the rank for.
     * @param isAttribute If <code>true</code>, <var>dataSetOrAttributeId</var> will be interpreted as an attribute, otherwise as a data set.
     */
    public int getRank(final long dataSetOrAttributeId, final boolean isAttribute,
            ICleanUpRegistry registry)
    {
        final long dataSpaceId =
                isAttribute ? H5Aget_space(dataSetOrAttributeId)
                        : H5Dget_space(dataSetOrAttributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        return H5Sget_simple_extent_ndims(dataSpaceId);
    }

    /**
     * @param dataSetOrAttributeId The id of either the data set or the attribute to get the rank for.
     * @param isAttribute If <code>true</code>, <var>dataSetOrAttributeId</var> will be interpreted as an attribute, otherwise as a data set.
     */
    public long[] getDimensions(final long dataSetOrAttributeId, final boolean isAttribute,
            ICleanUpRegistry registry)
    {
        final long dataSpaceId =
                isAttribute ? H5Aget_space(dataSetOrAttributeId)
                        : H5Dget_space(dataSetOrAttributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        final long[] dimensions = new long[H5S_MAX_RANK];
        final int rank = H5Sget_simple_extent_dims(dataSpaceId, dimensions, null);
        final long[] realDimensions = new long[rank];
        System.arraycopy(dimensions, 0, realDimensions, 0, rank);
        return realDimensions;
    }

    /**
     * @param dataSetOrAttributeId The id of either the data set or the attribute to get the dimensions for.
     * @param isAttribute If <code>true</code>, <var>dataSetOrAttributeId</var> will be interpreted as an attribute, otherwise as a data set.
     * @param dataSetInfo The info object to fill.
     */
    public void fillDataDimensions(final long dataSetOrAttributeId, final boolean isAttribute,
            final HDF5DataSetInformation dataSetInfo, ICleanUpRegistry registry)
    {
        final long dataSpaceId =
                isAttribute ? H5Aget_space(dataSetOrAttributeId)
                        : H5Dget_space(dataSetOrAttributeId);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        final long[] dimensions = new long[H5S_MAX_RANK];
        final long[] maxDimensions = new long[H5S_MAX_RANK];
        final int rank = H5Sget_simple_extent_dims(dataSpaceId, dimensions, maxDimensions);
        final long[] realDimensions = new long[rank];
        System.arraycopy(dimensions, 0, realDimensions, 0, rank);
        final long[] realMaxDimensions = new long[rank];
        System.arraycopy(maxDimensions, 0, realMaxDimensions, 0, rank);
        dataSetInfo.setDimensions(realDimensions);
        dataSetInfo.setMaxDimensions(realMaxDimensions);
        if (isAttribute == false)
        {
            final long[] chunkSizes = new long[rank];
            final long creationPropertyList =
                    getCreationPropertyList(dataSetOrAttributeId, registry);
            final HDF5StorageLayout layout =
                    HDF5StorageLayout.fromId(H5Pget_layout(creationPropertyList));
            dataSetInfo.setStorageLayout(layout);
            if (layout == HDF5StorageLayout.CHUNKED)
            {
                H5Pget_chunk(creationPropertyList, rank, chunkSizes);
                dataSetInfo.setChunkSizes(MDAbstractArray.toInt(chunkSizes));
            }
        }
    }

    public int[] getArrayDimensions(long arrayTypeId)
    {
        final int rank = H5Tget_array_ndims(arrayTypeId);
        final long[] dims = new long[rank];
        H5Tget_array_dims(arrayTypeId, dims);
        final int[] result = new int[rank];
        for (int i = 0; i < rank; ++i)
        {
            result[i] = (int) dims[i];
        }
        return result;
    }

    public long createScalarDataSpace()
    {
        return H5Screate(H5S_SCALAR);
    }

    public long createSimpleDataSpace(long[] dimensions, ICleanUpRegistry registry)
    {
        final long dataSpaceId = H5Screate_simple(dimensions.length, dimensions, null);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Sclose(dataSpaceId);
                }
            });
        return dataSpaceId;
    }

    public void setHyperslabBlock(long dataSpaceId, long[] start, long[] count)
    {
        assert dataSpaceId >= 0;
        assert start != null;
        assert count != null;

        H5Sselect_hyperslab(dataSpaceId, H5S_SELECT_SET, start, null, count, null);
    }

    //
    // Properties
    //

    private long createLinkCreationPropertyList(boolean createIntermediateGroups,
            ICleanUpRegistry registry)
    {
        final long linkCreationPropertyList = H5Pcreate(H5P_LINK_CREATE);
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(linkCreationPropertyList);
                }
            });
        if (createIntermediateGroups)
        {
            H5Pset_create_intermediate_group(linkCreationPropertyList, true);
        }
        if (useUTF8CharEncoding)
        {
            setCharacterEncodingCreationPropertyList(linkCreationPropertyList,
                    CharacterEncoding.UTF8);
        }
        return linkCreationPropertyList;
    }

    // Only use with H5P_LINK_CREATE, H5P_ATTRIBUTE_CREATE and H5P_STRING_CREATE property list ids
    private void setCharacterEncodingCreationPropertyList(long creationPropertyList,
            CharacterEncoding encoding)
    {
        H5Pset_char_encoding(creationPropertyList, encoding.getCValue());
    }

    private long createDataSetXferPropertyListAbortOverflow(ICleanUpRegistry registry)
    {
        final long datasetXferPropertyList = HDFHelper.H5Pcreate_xfer_abort_overflow();
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(datasetXferPropertyList);
                }
            });
        return datasetXferPropertyList;
    }

    private long createDataSetXferPropertyListAbort(ICleanUpRegistry registry)
    {
        final long datasetXferPropertyList = HDFHelper.H5Pcreate_xfer_abort();
        registry.registerCleanUp(new Runnable()
            {
                @Override
                public void run()
                {
                    H5Pclose(datasetXferPropertyList);
                }
            });
        return datasetXferPropertyList;
    }

    //
    // References
    //
    
    private final int BUFLEN = 128;

    String getReferencedObjectName(long objectId, byte[] reference)
    {
        final String[] objectName = new String[1];
        H5Rget_name(objectId, HDF5Constants.H5R_OBJECT, reference, objectName, BUFLEN);
        return objectName[0];
    }

    String getReferencedObjectName(long objectId, long reference)
    {
        final String[] objectName = new String[1];
        H5Rget_name(objectId, HDF5Constants.H5R_OBJECT, 
                HDFHelper.longToByte(new long[] { reference }), objectName, BUFLEN);
        return objectName[0];
    }

    String[] getReferencedObjectNames(long objectId, long[] reference)
    {
        final String[] objectNames = new String[reference.length];
        for (int i = 0; i < reference.length; ++i)
        {
            objectNames[i] = getReferencedObjectName(objectId, reference[i]);
        }
        return objectNames;
    }

    String getReferencedObjectName(long objectId, byte[] references, int ofs)
    {
        final byte[] reference = new byte[HDF5BaseReader.REFERENCE_SIZE_IN_BYTES];
        System.arraycopy(references, ofs, reference, 0, HDF5BaseReader.REFERENCE_SIZE_IN_BYTES);
        return getReferencedObjectName(objectId, reference);
    }

    byte[] createObjectReference(long fileId, String objectPath)
    {
        return H5Rcreate(fileId, objectPath, H5R_OBJECT, -1);
    }

    long[] createObjectReferences(long fileId, String[] objectPaths)
    {
        final long[] references = new long[objectPaths.length];
        for (int i = 0; i < objectPaths.length; ++i)
        {
            references[i] = HDFNativeData.byteToLong(createObjectReference(fileId, objectPaths[i]), 0);
        }
        return references;
    }
}