Repository navigation
Expand file tree
/
Copy pathFileFormat.java
More file actions
2048 lines (1897 loc) · 99.4 KB
/
Copy pathFileFormat.java
File metadata and controls
2048 lines (1897 loc) · 99.4 KB
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
/*****************************************************************************
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of the HDF Java Products distribution. *
* The full copyright notice, including terms governing use, modification, *
* and redistribution, is contained in the files COPYING and Copyright.html. *
* COPYING can be found at the root of the source code distribution tree. *
* Or, see http://hdfgroup.org/products/hdf-java/doc/Copyright.html. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
****************************************************************************/
package ncsa.hdf.object;
import java.io.File;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeNode;
/**
* FileFormat defines general interfaces for working with files whose data is
* organized according to a supported format.
* <p>
* FileFormat is a pluggable component. New implementing classes of FileFormat
* can be added to the list of supported file formats. Current implementing
* classes include H5File and H4File. By default, H5File and H4File are added to
* the list of supported file formats maintained by the static FileFormat
* instance.
*
* <pre>
* FileFormat
* _________________|_________________
* | | |
* H5File H4File Other...
* </pre>
* <p>
* A FileFormat instance may exist without being associated with a given file. A
* FileFormat instance may be associated with a file that is not open for
* access. Most typically, a FileFormat instance is used to open the associated
* file and perform operations such as retrieval and manipulation (if the file
* access is read-write) of the file structure and objects.
*
* @author Peter X. Cao
* @version 2.4 9/4/2007
*/
public abstract class FileFormat extends File {
/***************************************************************************
* File access flags used in calls to createInstance( String, flag );
**************************************************************************/
/**
*
*/
private static final long serialVersionUID = -4700692313888420796L;
private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FileFormat.class);
/**
* File first time access flag for open file. With this access flag, added
* to the regular value, indicates this file has no existing state.
*
*/
public static final int OPEN_NEW = 1;
/**
* File access flag for read-only permission. With this access flag,
* modifications to the file will not be allowed.
*
* @see #createInstance(String, int )
*/
public static final int READ = 2;
/**
* File access flag for read/write permission. With this access flag,
* modifications to the file will be allowed. Behavior if the file does not
* exist or cannot be opened for read/write access depends on the
* implementing class.
*
* @see #createInstance(String, int )
*/
public static final int WRITE = 4;
/**
* File access flag for creating/truncating with read-write permission. If
* the file already exists, it will be truncated when opened. With this
* access flag, modifications to the file will be allowed. Behavior if file
* can't be created, or if it exists but can't be opened for read/write
* access, depends on the implementing class.
*
* @see #createInstance(String, int )
*/
public static final int CREATE = 8;
/***************************************************************************
* File creation flags used in calls to createFile( String, flag );
**************************************************************************/
/**
* Flag for creating/truncating a file. If the file already exists, it will
* be truncated when opened. If the file does not exist, it will be created.
* Modifications to the file will be allowed.
*
* @see #createFile(String, int )
*/
public static final int FILE_CREATE_DELETE = 10;
/**
* Flag for creating/opening a file. If the file already exists, it will be
* opened without changing the existing contents. If the file does not
* exist, it will be created. Modifications to the file will be allowed.
*
* @see #createFile(String, int )
*/
public static final int FILE_CREATE_OPEN = 20;
/**
* Flag to indicate if the earliest version of library is used when creating
* a new file.
*
* @see #createFile(String, int )
*/
public static final int FILE_CREATE_EARLY_LIB = 40;
/***************************************************************************
* Keys and fields related to supported file formats.
**************************************************************************/
/** Key for HDF4 file format. */
public static final String FILE_TYPE_HDF4 = "HDF4";
/** Key for HDF5 file format. */
public static final String FILE_TYPE_HDF5 = "HDF5";
/**
* A separator that separates file name and object name.
*
* @see ncsa.hdf.object.FileFormat#getHObject(String)
*/
public static final String FILE_OBJ_SEP = "://";
/**
* FileList keeps a list of supported FileFormats. This list can be updated
* and queried at runtime.
*
* @see #addFileFormat(String,FileFormat)
* @see #getFileFormat(String)
* @see #getFileFormatKeys()
* @see #getFileFormats()
* @see #removeFileFormat(String)
*/
private static final Map<String, FileFormat> FileList = new Hashtable<String, FileFormat>(10);
/**
* A list of file extensions for the supported file formats. This list of
* file extensions is not integrated with the supported file formats kept in
* FileList, but is provided as a convenience for applications who may
* choose to process only those files with recognized extensions.
*/
private static String extensions = "hdf, h4, hdf5, h5, nc, fits";
/***************************************************************************
* Sizing information and class metadata
**************************************************************************/
/**
* Current Java applications, such as HDFView, cannot handle files with
* large numbers of objects due to JVM memory limitations. For example,
* 1,000,000 objects is too many. max_members is defined so that
* applications such as HDFView will load up to <i>max_members</i> objects
* starting with the <i>start_members</i> -th object. The implementing class
* has freedom in its interpretation of how to "count" objects in the file.
*/
private int max_members = 10000; // 10,000
// by
// default
private int start_members = 0; // 0
// by
// default
/**
* File identifier. -1 indicates the file is not open.
*/
protected int fid = -1;
/**
* The absolute pathname (path+name) of the file.
*/
protected String fullFileName = null;
/**
* Flag indicating if the file access is read-only.
*/
protected boolean isReadOnly = false;
/***************************************************************************
* Class initialization method
**************************************************************************/
/**
* By default, HDF4 and HDF5 file formats are added to the supported formats
* list.
*/
static {
// add HDF4 to default modules
if (FileFormat.getFileFormat(FILE_TYPE_HDF4) == null) {
try {
Class fileclass = Class.forName("ncsa.hdf.object.h4.H4File");
FileFormat fileformat = (FileFormat) fileclass.newInstance();
if (fileformat != null) {
FileFormat.addFileFormat(FILE_TYPE_HDF4, fileformat);
log.debug("FILE_TYPE_HDF4 file format added");
}
}
catch (Throwable err) {
log.debug("FILE_TYPE_HDF4 instance failure: ", err);
}
}
// add HDF5 to default modules
if (FileFormat.getFileFormat(FILE_TYPE_HDF5) == null) {
try {
Class fileclass = Class.forName("ncsa.hdf.object.h5.H5File");
FileFormat fileformat = (FileFormat) fileclass.newInstance();
if (fileformat != null) {
FileFormat.addFileFormat(FILE_TYPE_HDF5, fileformat);
log.debug("FILE_TYPE_HDF5 file format added");
}
}
catch (Throwable err) {
log.debug("FILE_TYPE_HDF5 instance failure: ", err);
}
}
// add NetCDF to default modules
if (FileFormat.getFileFormat("NetCDF") == null) {
try {
Class fileclass = Class.forName("ncsa.hdf.object.nc2.NC2File");
FileFormat fileformat = (FileFormat) fileclass.newInstance();
if (fileformat != null) {
FileFormat.addFileFormat("NetCDF", fileformat);
log.debug("NetCDF file format added");
}
}
catch (Throwable err) {
log.debug("NetCDF instance failure: ", err);
}
}
// add Fits to default modules
if (FileFormat.getFileFormat("Fits") == null) {
try {
Class fileclass = Class.forName("ncsa.hdf.object.fits.FitsFile");
FileFormat fileformat = (FileFormat) fileclass.newInstance();
if (fileformat != null) {
FileFormat.addFileFormat("Fits", fileformat);
log.debug("Fits file format added");
}
}
catch (Throwable err) {
log.debug("FITS instance failure: ", err);
}
}
}
/***************************************************************************
* Constructor
**************************************************************************/
/**
* Creates a new FileFormat instance with the given filename.
* <p>
* The filename in this method call is equivalent to the pathname in the
* java.io.File class. The filename is converted into an abstract pathname
* by the File class.
* <p>
* Typically this constructor is not called directly, but is called by a
* constructor of an implementing class. Applications most frequently use
* the <i>createFile()</i>, <i>createInstance()</i>, or <i>getInstance()</i>
* methods to generate a FileFormat instance with an associated filename.
* <p>
* The file is not opened by this call. The read-only flag is set to false
* by this call.
*
* @param filename
* The filename; a pathname string.
* @throws NullPointerException
* If the <code>filename</code> argument is <code>null</code>.
* @see java.io.File#File(String)
* @see #createFile(String, int)
* @see #createInstance(String, int)
* @see #getInstance(String)
*/
public FileFormat(String filename) {
super(filename);
fullFileName = filename;
if ((filename != null) && (filename.length() > 0)) {
try {
fullFileName = this.getAbsolutePath();
}
catch (Exception ex) {
log.debug("File {} getAbsolutePath failure: ", filename, ex);
}
}
isReadOnly = false;
}
/***************************************************************************
* Class methods
**************************************************************************/
/**
* Adds a FileFormat with specified key to the list of supported formats.
* <p>
* This method allows a new FileFormat, tagged with an identifying key, to
* be added dynamically to the list of supported File Formats. Using it,
* applications can add new File Formats at runtime.
* <p>
* For example, to add a new File Format with the key "xyz" that is
* implemented by the class xyzFile in the package companyC.files, an
* application would make the following calls:
*
* <pre>
* Class fileClass = Class.forName( "companyC.files.xyzFile" );
* FileFormat ff = (FileFormat) fileClass.newInstance();
* if ( ff != null ) {
* ff.addFileFormat ("xyz", ff )
* }
* </pre>
* <p>
* If either <code>key</code> or <code>fileformat</code> are
* <code>null</code>, or if <code>key</code> is already in use, the method
* returns without updating the list of supported File Formats.
*
* @param key
* A string that identifies the FileFormat.
* @param fileformat
* An instance of the FileFormat to be added.
* @see #getFileFormat(String)
* @see #getFileFormatKeys()
* @see #getFileFormats()
* @see #removeFileFormat(String)
*/
public static final void addFileFormat(String key, FileFormat fileformat) {
if ((fileformat == null) || (key == null)) {
return;
}
key = key.trim();
if (!FileList.containsKey(key)) {
FileList.put(key, fileformat);
}
}
/**
* Returns the FileFormat with specified key from the list of supported
* formats.
* <p>
* This method returns a FileFormat instance, as identified by an
* identifying key, from the list of supported File Formats.
* <p>
* If the specified key is in the list of supported formats, the instance of
* the associated FileFormat object is returned. If the specified key is not
* in the list of supported formats, <code>null</code> is returned.
*
* @param key
* A string that identifies the FileFormat.
* @return The FileFormat that matches the given key, or <code>null</code>
* if the key is not found in the list of supported File Formats.
* @see #addFileFormat(String,FileFormat)
* @see #getFileFormatKeys()
* @see #getFileFormats()
* @see #removeFileFormat(String)
*/
public static final FileFormat getFileFormat(String key) {
return FileList.get(key);
}
/**
* Returns an Enumeration of keys for all supported formats.
* <p>
* This method returns an Enumeration containing the unique keys (Strings)
* for the all File Formats in the list of supported File Formats.
*
* @return An Enumeration of keys that are in the list of supported formats.
* @see #addFileFormat(String,FileFormat)
* @see #getFileFormat(String)
* @see #getFileFormats()
* @see #removeFileFormat(String)
*/
public static final Enumeration getFileFormatKeys() {
return ((Hashtable) FileList).keys();
}
/**
* Returns an array of supported FileFormat instances.
* <p>
* This method returns an array of FileFormat instances that appear in the
* list of supported File Formats.
* <p>
* If the list of supported formats is empty, <code>null</code> is returned.
*
* @return An array of all FileFormat instances in the list of supported
* File Formats, or <code>null</code> if the list is empty.
* @see #addFileFormat(String,FileFormat)
* @see #getFileFormat(String)
* @see #getFileFormatKeys()
* @see #removeFileFormat(String)
*/
public static final FileFormat[] getFileFormats() {
int n = FileList.size();
if (n <= 0) {
return null;
}
int i = 0;
FileFormat[] fileformats = new FileFormat[n];
Enumeration local_enum = ((Hashtable) FileList).elements();
while (local_enum.hasMoreElements()) {
fileformats[i++] = (FileFormat) local_enum.nextElement();
}
return fileformats;
}
/**
* Removes a FileFormat from the list of supported formats.
* <p>
* This method removes a FileFormat, as identified by the specified key,
* from the list of supported File Formats.
* <p>
* If the specified key is in the list of supported formats, the instance of
* the FileFormat object that is being removed from the list is returned. If
* the key is not in the list of supported formats, <code>null</code> is
* returned.
*
* @param key
* A string that identifies the FileFormat to be removed.
* @return The FileFormat that is removed, or <code>null</code> if the key
* is not found in the list of supported File Formats.
* @see #addFileFormat(String,FileFormat)
* @see #getFileFormat(String)
* @see #getFileFormatKeys()
* @see #getFileFormats()
*/
public static final FileFormat removeFileFormat(String key) {
return FileList.remove(key);
}
/**
* Adds file extension(s) to the list of file extensions for supported file
* formats.
* <p>
* Multiple extensions can be included in the single parameter if they are
* separated by commas.
* <p>
* The list of file extensions updated by this call is not linked with
* supported formats that implement FileFormat objects. The file extension
* list is maintained for the benefit of applications that may choose to
* recognize only those files with extensions that appear in the list of
* file extensions for supported file formats.
* <p>
* By default, the file extensions list includes: "hdf, h4, hdf5, h5"
*
* @param extension
* The file extension(s) to add.
* @see #addFileFormat(String,FileFormat)
* @see #getFileExtensions()
*/
public static final void addFileExtension(String extension) {
if ((extensions == null) || (extensions.length() <= 0)) {
extensions = extension;
}
StringTokenizer currentExt = new StringTokenizer(extensions, ",");
Vector<String> tokens = new Vector<String>(currentExt.countTokens() + 5);
while (currentExt.hasMoreTokens()) {
tokens.add(currentExt.nextToken().trim().toLowerCase());
}
currentExt = new StringTokenizer(extension, ",");
String ext = null;
while (currentExt.hasMoreTokens()) {
ext = currentExt.nextToken().trim().toLowerCase();
if (tokens.contains(ext)) {
continue;
}
extensions = extensions + ", " + ext;
}
tokens.setSize(0);
}
/**
* Returns a list of file extensions for all supported file formats.
* <p>
* The extensions in the returned String are separates by commas:
* "hdf, h4, hdf5, h5"
* <p>
* It is the responsibility of the application to update the file extension
* list using {@link #addFileExtension(String)} when new FileFormat
* implementations are added.
*
* @return A list of file extensions for all supported file formats.
* @see #addFileExtension(String)
*/
public static final String getFileExtensions() {
return extensions;
}
/**
* Creates a FileFormat instance for the specified file.
* <p>
* This method checks the list of supported file formats to find one that
* matches the format of the specified file. If a match is found, the method
* returns an instance of the associated FileFormat object. If no match is
* found, <code>null</code> is returned.
* <p>
* For example, if "test_hdf5.h5" is an HDF5 file,
* FileFormat.getInstance("test_hdf5.h5") will return an instance of H5File.
* <p>
* The file is not opened as part of this call. Read/write file access is
* associated with the FileFormat instance if the matching file format
* supports read/write access. Some file formats only support read access.
*
* @param filename
* A valid file name, with a relative or absolute path.
* @return An instance of the matched FileFormat; <code>null</code> if no
* match.
* @throws IllegalArgumentException
* If the <code>filename</code> argument is <code>null</code> or
* does not specify an existing file.
* @throws Exception
* If there are problems creating the new instance.
* @see #createFile(String, int)
* @see #createInstance(String, int)
* @see #getFileFormats()
*/
public static final FileFormat getInstance(String filename) throws Exception {
if ((filename == null) || (filename.length() <= 0)) {
throw new IllegalArgumentException("Invalid file name: " + filename);
}
if (!(new File(filename)).exists()) {
throw new IllegalArgumentException("File " + filename + " does not exist.");
}
FileFormat fileFormat = null;
FileFormat knownFormat = null;
Enumeration elms = ((Hashtable) FileList).elements();
while (elms.hasMoreElements()) {
knownFormat = (FileFormat) elms.nextElement();
if (knownFormat.isThisType(filename)) {
try {
fileFormat = knownFormat.createInstance(filename, WRITE);
}
catch (Exception ex) {
log.debug("File {} createInstance failure: ", filename, ex);
}
break;
}
}
return fileFormat;
}
/***************************************************************************
* Implementation Class methods. These methods are related to the
* implementing FileFormat class, but not to a particular instance of that
* class. Since we can't override class methods (they can only be shadowed
* in Java), these are instance methods.
*
* The non-abstract methods just throw an exception indicating that the
* implementing class doesn't support the functionality.
**************************************************************************/
/**
* Returns the version of the library for the implementing FileFormat class.
* <p>
* The implementing FileFormat classes have freedom in how they obtain or
* generate the version number that is returned by this method. The H5File
* and H4File implementations query the underlying HDF libraries and return
* the reported version numbers. Other implementing classes may generate the
* version string directly within the called method.
*
* @return The library version.
*/
public abstract String getLibversion();
/**
* Checks if the class implements the specified FileFormat.
* <p>
* The Java "instanceof" operation is unable to check if an object is an
* instance of a FileFormat that is loaded at runtime. This method provides
* the "instanceof" functionality, and works for implementing classes that
* are loaded at runtime.
* <p>
* This method lets applications that only access the abstract object layer
* determine the format of a given instance of the abstract class.
* <p>
* For example, HDFView uses the following code to determine if a file is an
* HDF5 file:
*
* <pre>
* FileFormat h5F = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
* HObject hObject = viewer.getTreeView()
* .getCurrentObject();
* FileFormat thisF = hObject
* .getFileFormat();
* boolean isH5 = h5F.isThisType(thisF);
* </pre>
*
* @param fileFormat
* The FileFormat to be checked.
* @return True if this instance implements the specified FileFormat;
* otherwise returns false.
* @see #isThisType(String)
*/
public abstract boolean isThisType(FileFormat fileFormat);
/**
* Checks if the implementing FileFormat class matches the format of the
* specified file.
* <p>
* For example, if "test.h5" is an HDF5 file, the first call to isThisType()
* in the code fragment shown will return <code>false</code>, and the second
* call will return <code>true</code>.
*
* <pre>
* FileFormat h4F = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF4);
* FileFormat h5F = FileFormat
* .getFileFormat(FileFormat.FILE_TYPE_HDF5);
* boolean isH4 = h4F.isThisType("test.h5"); // false
* boolean isH5 = h5F.isThisType("test.h5"); // true
* </pre>
*
* @param filename
* The name of the file to be checked.
* @return True if the format of the file matches the format of this
* instance; otherwise returns false.
* @see #isThisType(FileFormat)
*/
public abstract boolean isThisType(String filename);
/**
* Creates a file with the specified name and returns a new FileFormat
* implementation instance associated with the file.
* <p>
* This method creates a file whose format is the same as that of the
* implementing class. An instance of the FileFormat implementing class is
* created and associated with the file. That instance is returned by the
* method.
* <p>
* The filename in this method call is equivalent to the pathname in the
* java.io.File class. The filename is converted into an abstract pathname
* by the File class.
* <p>
* A flag controls the behavior if the named file already exists. The flag
* values and corresponding behaviors are:
* <ul>
* <li>FILE_CREATE_DELETE: Create a new file or truncate an existing one.
* <li>FILE_CREATE_OPEN: Create a new file or open an existing one.
* </ul>
* <p>
* If the flag is FILE_CREATE_DELETE, the method will create a new file or
* truncate an existing file. If the flag is FILE_CREATE_OPEN and the file
* does not exist, the method will create a new file.
* <p>
* This method does not open the file for access, nor does it confirm that
* the file can later be opened read/write. The file open is carried out by
* the <i>open()</i> call.
*
* @param filename
* The filename; a pathname string.
* @param createFlag
* The creation flag, which determines behavior when the file
* already exists. Acceptable values are
* <code>FILE_CREATE_DELETE</code> and
* <code>FILE_CREATE_OPEN</code>.
* @throws NullPointerException
* If the <code>filename</code> argument is <code>null</code>.
* @throws UnsupportedOperationException
* If the implementing class does not support the file creation
* operation.
* @throws Exception
* If the file cannot be created or if the creation flag has an
* unexpected value. The exceptions thrown vary depending on the
* implementing class.
* @see #createInstance(String, int)
* @see #getInstance(String)
* @see #open()
*/
public FileFormat createFile(String filename, int createFlag) throws Exception {
// If the implementing subclass doesn't have this method then that
// format doesn't support File Creation and we throw an exception.
throw new UnsupportedOperationException("FileFormat FileFormat.createFile(...) is not implemented.");
}
/**
* Creates a FileFormat implementation instance with specified filename and
* access.
* <p>
* This method creates an instance of the FileFormat implementing class and
* sets the filename and file access parameters.
* <p>
* The filename in this method call is equivalent to the pathname in the
* java.io.File class. The filename is converted into an abstract pathname
* by the File class.
* <p>
* The access parameter values and corresponding behaviors at file open:
* <ul>
* <li>READ: Read-only access. Fail if file doesn't exist.
* <li>WRITE: Read/Write access. Behavior if file doesn't exist or can't be
* opened for read/write access depends on the implementing class.
* <li>CREATE: Read/Write access. Create a new file or truncate an existing
* one. Behavior if file can't be created, or if it exists but can't be
* opened read/write depends on the implementing class.
* </ul>
* <p>
* Some FileFormat implementing classes may only support READ access and
* will use READ regardless of the value specified in the call. Refer to the
* implementing class documentation for details.
* <p>
* This method does not open the file for access, nor does it confirm that
* the file can later be opened read/write or created. The file open is
* carried out by the <i>open()</i> call.
* <p>
* Example (without exception handling):
*
* <pre>
* // Request the implementing class of FileFormat: H5File
* FileFormat h5file = FileFormat.getFileFormat(FileFormat.FILE_TYPE_HDF5);
*
* // Create
* // an
* // instance
* // of
* // H5File
* // object
* // with
* // read/write
* // access
* H5File test1 = (H5File) h5file.createInstance(
* "test_hdf5.h5",
* FileFormat.WRITE);
* // Open
* // the
* // file
* // and
* // load
* // the
* // file
* // structure;
* // file
* // id
* // is
* // returned.
* int fid = test1.open();
* </pre>
*
* @param filename
* The filename; a pathname string.
* @param access
* The file access flag, which determines behavior when file is
* opened. Acceptable values are <code> READ, WRITE, </code> and
* <code>CREATE</code>.
* @throws NullPointerException
* If the <code>filename</code> argument is <code>null</code>.
* @throws Exception
* If the instance cannot be created or if the access flag has
* an unexpected value. The exceptions thrown vary depending on
* the implementing class.
* @see #createFile(String, int)
* @see #getInstance(String)
* @see #open()
*/
public abstract FileFormat createInstance(String filename, int access) throws Exception;
// REVIEW DOCS for createInstance()
// What if READ ONLY in implementation? What if file already open?
// Can we doc exceptions better or in implementation methods?
/***************************************************************************
* Final instance methods
*
* Related to a given instance of the class, but at the FileFormat level,
* not at the implementing class level.
**************************************************************************/
/**
* Returns the absolute path for the file.
* <p>
* For example, "/samples/hdf5_test.h5". If there is no file associated with
* this FileFormat instance, <code>null</code> is returned.
*
* @return The full path (file path + file name) of the associated file, or
* <code>null</code> if there is no associated file.
*/
public final String getFilePath() {
return fullFileName;
}
/**
* Returns file identifier of open file associated with this instance.
*
* @return The file identifer, or -1 if there is no file open.
*/
public final int getFID() {
return fid;
}
/**
* Returns true if the file access is read-only.
* <p>
* This method returns true if the file access is read-only. If the file
* access is read-write, or if there is no file associated with the
* FileFormat instance, false will be returned.
* <p>
* Note that this method may return true even if the file is not open for
* access when the method is called. The file access is set by the
* <i>createFile()</i>, <i>createInstance()</i>, or <i>getInstance()</i>
* call, and the file is opened for access by the <i>open()</i> call.
*
* @return True if the file access is read-only, otherwise returns false.
* @see #createFile(String, int)
* @see #createInstance(String, int)
* @see #getInstance(String)
* @see #open()
*/
public final boolean isReadOnly() {
return isReadOnly;
}
/**
* Sets the maximum number of objects to be loaded into memory.
* <p>
* Current Java applications, such as HDFView, cannot handle files with
* large numbers of objects due to JVM memory limitations. The maximum
* number limits the number of objects that will be loaded for a given
* FileFormat instance.
* <p>
* The implementing FileFormat class has freedom in how it interprets the
* maximum number. H5File, for example, will load the maximum number of
* objects for each group in the file.
*
* @param n
* The maximum number of objects to be loaded into memory.
* @see #getMaxMembers()
* @see #setStartMembers(int)
*/
public final void setMaxMembers(int n) {
max_members = n;
}
/**
* Returns the maximum number of objects that can be loaded into memory.
*
* @return The maximum number of objects that can be loaded into memory.
* @see #setMaxMembers(int)
*/
public final int getMaxMembers() {
if (max_members<0)
return Integer.MAX_VALUE; // load the whole file
return max_members;
}
/**
* Sets the starting index of objects to be loaded into memory.
* <p>
* The implementing FileFormat class has freedom in how it indexes objects
* in the file.
*
* @param idx
* The starting index of the object to be loaded into memory
* @see #getStartMembers()
* @see #setMaxMembers(int)
*/
public final void setStartMembers(int idx) {
start_members = idx;
}
/**
* Returns the index of the starting object to be loaded into memory.
*
* @return The index of the starting object to be loaded into memory.
* @see #setStartMembers(int)
*/
public final int getStartMembers() {
return start_members;
}
/**
* Returns the number of objects in memory.
* <p>
* This method returns the total number of objects loaded into memory for
* this FileFormat instance. The method counts the objects that are loaded,
* which can take some time for a large number of objects.
* <p>
* It is worth noting that the total number of objects in memory may be
* different than the total number of objects in the file.
* <p>
* Since implementing classes have freedom in how they interpret and use the
* maximum number of members value, there may be differing numbers of
* objects in memory in different implementation instances, even with the
* same "use case".
* <p>
* For example, say the use case is a file that contains 20,000 objects, the
* maximum number of members for an instance is 10,000, and the start member
* index is 1. There are 2 groups in the file. The root group contains
* 10,500 objects and the group "/g1" contains 9,500 objects.
* <p>
* In an implementation that limits the total number of objects loaded to
* the maximum number of members, this method will return 10,000.
* <p>
* In contrast, the H5File implementation loads up to the maximum number of
* members objects for each group in the file. So, with our use case 10,000
* objects will be loaded in the root group and 9,500 objects will be loaded
* into group "/g1". This method will return the value 19,500, which exceeds
* the maximum number of members value.
*
* @return The number of objects in memory.
* @see #getMaxMembers()
* @see #setMaxMembers(int)
* @see #getStartMembers()
* @see #setStartMembers(int)
*/
public final int getNumberOfMembers() {
int n_members = 0;
TreeNode rootNode = getRootNode();
if (rootNode != null) {
Enumeration local_enum = ((DefaultMutableTreeNode) rootNode).depthFirstEnumeration();
while (local_enum.hasMoreElements()) {
local_enum.nextElement();
n_members++;
}
}
return n_members;
}
/***************************************************************************
* Abstract Instance methods
*
* These methods are related to the Implementing FileFormat class and to
* particular instances of objects with those classes.
**************************************************************************/
/**
* Opens file and returns a file identifier.
* <p>
* This method uses the <code>filename</code> and <code>access</code>
* parameters specified in the <i>createFile()</i>, <i>createInstance()</i>,
* or </i>getInstance()</i> call to open the file. It returns the file
* identifier if successful, or a negative value in case of failure.
* <p>
* The method also loads the file structure and basic information (name,
* type) for data objects in the file into the FileFormat instance. It does
* not load the contents of any data object.
* <p>
* The structure of the file is stored in a tree starting from the root
* node.
*
* @return File identifier if successful; otherwise -1.
* @throws Exception
* If the file cannot be opened. The exceptions thrown vary
* depending on the implementing class.
* @see #createFile(String, int)
* @see #createInstance(String, int)
* @see #getInstance(String)
* @see #getRootNode()
*/
public abstract int open() throws Exception;
/**
* Closes file associated with this instance.
* <p>
* This method closes the file associated with this FileFormat instance, as
* well as all objects associated with the file.
*
* @throws Exception
* If the file or associated objects cannot be closed. The
* exceptions thrown vary depending on the implementing class.
* @see #open()