NeXusJavaBindings  1
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
NexusFile.java
Go to the documentation of this file.
1 
16 package org.nexusformat;
17 
18 import java.util.Hashtable;
19 import java.io.File;
20 import ncsa.hdf.hdflib.HDFArray;
21 import ncsa.hdf.hdflib.HDFException;
22 import ncsa.hdf.hdflib.HDFConstants;
23 
24 public class NexusFile implements NeXusFileInterface {
25 
26  // constants
30  public final static int NXACC_READ = 1;
31  public final static int NXACC_RDWR = 2;
32  public final static int NXACC_CREATE = 3;
33  public final static int NXACC_CREATE4 = 4;
34  public final static int NXACC_CREATE5 = 5;
35  public final static int NXACC_CREATEXML = 6;
36  public final static int NXACC_NOSTRIP = 128;
37 
41  public final static int NX_UNLIMITED = -1;
42 
47  public final static int NX_FLOAT32 = 5;
48  public final static int NX_FLOAT64 = 6;
49  public final static int NX_INT8 = 20;
50  public final static int NX_BINARY = 20;
51  public final static int NX_UINT8 = 21;
52  public final static int NX_BOOLEAN = 21;
53  public final static int NX_INT16 = 22;
54  public final static int NX_UINT16 = 23;
55  public final static int NX_INT32 = 24;
56  public final static int NX_UINT32 = 25;
57  public final static int NX_INT64 = 26;
58  public final static int NX_UINT64 = 27;
59  public final static int NX_CHAR = 4;
60 
64  public final static int NX_COMP_NONE = 100;
65  public final static int NX_COMP_LZW = 200;
66  public final static int NX_COMP_RLE = 300;
67  public final static int NX_COMP_HUF = 400;
68 
72  protected final static int MAXNAMELEN = 64;
73 
74  /*
75  This code takes care of loading the static library required for
76  this class to work properly. The algorithm first looks for a
77  property org.nexusformat.JNEXUSLIB and loads that file if available,
78  else it tries to locate the library in the system shared library
79  path.
80  */
81  static
82  {
83  String filename = null;
84  filename = System.getProperty("org.nexusformat.JNEXUSLIB",null);
85  if ((filename != null) && (filename.length() > 0))
86  {
87  File hdfdll = new File(filename);
88  if (hdfdll.exists() && hdfdll.canRead() && hdfdll.isFile())
89  {
90  System.load(filename);
91  } else {
92  throw (new UnsatisfiedLinkError("Invalid JNEXUS library"));
93  }
94  }
95  else {
96  System.loadLibrary("jnexus");
97  }
98  }
99 
103  protected int handle;
104 
105  // Construction
106  // native methods for this section
107  protected native int init(String filename, int access);
108  protected native void close(int handle);
109  protected native int nxflush(int handle);
110 
130  public NexusFile(String filename, int access) throws NexusException {
131  checkForNull(filename);
132 
133  handle = init(filename,access);
134  if(handle < 0){
135  throw new NexusException("Failed to open " + filename);
136  }
137  }
138 
142  public void flush() throws NexusException {
143  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
144  handle = nxflush(handle);
145  }
146 
151  public void close() throws NexusException {
152  if(handle >= 0) {
153  close(handle);
154  handle = -1;
155  }
156  }
157 
168  public void finalize() throws Throwable {
169  close();
170  }
171 
172 
173  // group functions
174  //native methods for this section
175  protected native void nxmakegroup(int handle, String name, String nxclass);
176  protected native void nxopengroup(int handle, String name, String nxclass);
177  protected native void nxopenpath(int handle, String path);
178  protected native void nxopengrouppath(int handle, String path);
179  protected native void nxclosegroup(int handle);
180  protected native String nxgetpath(int handle);
181 
182  public void makegroup(String name, String nxclass) throws NexusException {
183  checkForNull(name, nxclass);
184  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
185  nxmakegroup(handle, name, nxclass);
186  }
187 
188  public void opengroup(String name, String nxclass) throws NexusException {
189  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
190  checkForNull(name, nxclass);
191  nxopengroup(handle, name, nxclass);
192  }
193 
194  public void openpath(String path) throws NexusException {
195  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
196  checkForNull(path);
197  nxopenpath(handle,path);
198  }
199 
200  public void opengrouppath(String path) throws NexusException {
201  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
202  checkForNull(path);
203  nxopengrouppath(handle,path);
204  }
205 
206  public String getpath() throws NexusException {
207  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
208  return nxgetpath(handle);
209  }
210 
211  public void closegroup() throws NexusException {
212  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
214  }
215 
216  // data set handling
217  // native methods for this section
218  protected native void nxmakedata(int handle, String name, int type, int rank, int dim[]);
219  protected native void nxmakedata64(int handle, String name, int type, int rank, long dim[]);
220  protected native void nxmakecompdata(int handle, String name, int type, int rank, int dim[], int iCompress, int iChunk[]);
221  protected native void nxmakecompdata64(int handle, String name, int type, int rank, long dim[], int iCompress, long iChunk[]);
222  protected native void nxopendata(int handle, String name);
223  protected native void nxclosedata(int handle);
224  protected native void nxcompress(int handle, int compression_type);
225 
226  public void compmakedata(String name, int type, int rank, int dim[],
227  int compression_type, int iChunk[]) throws NexusException {
228  if (handle < 0) throw new NexusException("NAPI-ERROR: File not open");
229  checkType(type);
230  checkForNull(name, rank, iChunk);
231  checkForNegInArray(true, dim, iChunk);
232  switch(compression_type) {
233  case NexusFile.NX_COMP_NONE:
234  case NexusFile.NX_COMP_LZW:
235  break;
236  default:
237  throw new NexusException("Invalid compression code requested");
238 
239  }
240  nxmakecompdata(handle, name, type, rank, dim, compression_type, iChunk);
241  }
242 
243  public void compmakedata(String name, int type, int rank, long dim[],
244  int compression_type, long iChunk[]) throws NexusException {
245  if (handle < 0) throw new NexusException("NAPI-ERROR: File not open");
246  checkType(type);
247  checkForNull(name, rank, iChunk);
248  checkForNegInArray(true, dim, iChunk);
249  switch(compression_type) {
250  case NexusFile.NX_COMP_NONE:
251  case NexusFile.NX_COMP_LZW:
252  break;
253  default:
254  throw new NexusException("Invalid compression code requested");
255 
256  }
257  nxmakecompdata64(handle, name, type, rank, dim, compression_type, iChunk);
258  }
259 
260  public void makedata(String name, int type, int rank, int dim[]) throws
262  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
263  checkType(type);
264  checkForNull(name, dim);
265  checkForNegInArray(true, dim);
266  nxmakedata(handle, name, type, rank, dim);
267  }
268 
269  public void makedata(String name, int type, int rank, long dim[]) throws
271  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
272  checkType(type);
273  checkForNull(name, dim);
274  checkForNegInArray(true, dim);
275  nxmakedata64(handle, name, type, rank, dim);
276  }
277 
278  public void opendata(String name) throws NexusException {
279  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
280  checkForNull(name);
281  nxopendata(handle,name);
282  }
283 
284  public void closedata() throws NexusException {
285  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
287  }
288 
289  public void compress(int compression_type) throws NexusException {
290  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
291  switch(compression_type) {
292  case NexusFile.NX_COMP_NONE:
293  case NexusFile.NX_COMP_LZW:
294  case NexusFile.NX_COMP_RLE:
295  case NexusFile.NX_COMP_HUF:
296  break;
297  default:
298  throw new NexusException("Invalid compression code requested");
299 
300  }
301  nxcompress(handle,compression_type);
302  }
303 
304  // data set reading
305  // native methods in this section
306  protected native void nxgetdata(int handle, byte bdata[]);
307  protected native void nxgetslab(int handle, int Start[], int size[], byte bdata[]);
308  protected native void nxgetslab64(int handle, long Start[], long size[], byte bdata[]);
309  protected native void nxgetattr(int handle, String name, byte bdata[], int args[]);
310 
311  public void getdata(Object array) throws NexusException {
312  byte bdata[];
313  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
314  checkForNull(array);
315  try{
316  HDFArray ha = new HDFArray(array);
317  bdata = ha.emptyBytes();
318  nxgetdata(handle,bdata);
319  array = ha.arrayify(bdata);
320  }catch(HDFException he) {
321  throw new NexusException(he.getMessage());
322  }
323  }
324 
325  public void getslab(int start[], int size[], Object array) throws NexusException {
326  byte bdata[];
327  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
328  checkForNull(start, size, array);
329  checkForNegInArray(false, start, size);
330  try{
331  HDFArray ha = new HDFArray(array);
332  bdata = ha.emptyBytes();
333  nxgetslab(handle,start,size,bdata);
334  array = ha.arrayify(bdata);
335  }catch(HDFException he) {
336  throw new NexusException(he.getMessage());
337  }
338  }
339 
340  public void getslab(long start[], long size[], Object array) throws NexusException {
341  byte bdata[];
342  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
343  checkForNull(start, size, array);
344  checkForNegInArray(false, start, size);
345  try{
346  HDFArray ha = new HDFArray(array);
347  bdata = ha.emptyBytes();
348  nxgetslab64(handle,start,size,bdata);
349  array = ha.arrayify(bdata);
350  }catch(HDFException he) {
351  throw new NexusException(he.getMessage());
352  }
353  }
354 
355  public void getattr(String name, Object array, int args[]) throws NexusException {
356  byte bdata[];
357  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
358  checkType(args[1]);
359  checkForNull(name, array);
360  try{
361  HDFArray ha = new HDFArray(array);
362  bdata = ha.emptyBytes();
363  nxgetattr(handle, name, bdata,args);
364  array = ha.arrayify(bdata);
365  }catch(HDFException he) {
366  throw new NexusException(he.getMessage());
367  }
368  }
369 
370  // data set writing
371  // native methods for this section
372  protected native void nxputdata(int handle, byte array[]);
373  protected native void nxputslab(int handle, byte array[], int start[], int size[]);
374  protected native void nxputslab64(int handle, byte array[], long start[], long size[]);
375  protected native void nxputattr(int handle, String name, byte array[], int type);
376 
377  public void putdata(Object array) throws NexusException {
378  byte data[];
379 
380  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
381  checkForNull(array);
382 
383  try {
384  HDFArray ha = new HDFArray(array);
385  data = ha.byteify();
386  ha = null;
387  } catch (HDFException he) {
388  throw new NexusException(he.getMessage());
389  }
390  nxputdata(handle,data);
391  data = null;
392  }
393 
394  public void putslab(Object array, int start[], int size[]) throws NexusException {
395  byte data[];
396 
397  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
398  checkForNull(array, start, size);
399  checkForNegInArray(false, start, size);
400  try {
401  HDFArray ha = new HDFArray(array);
402  data = ha.byteify();
403  ha = null;
404  } catch(HDFException he) {
405  throw new NexusException(he.getMessage());
406  }
407  nxputslab(handle,data,start,size);
408  data = null;
409  }
410 
411  public void putslab(Object array, long start[], long size[]) throws NexusException {
412  byte data[];
413 
414  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
415  checkForNull(array, start, size);
416  checkForNegInArray(false, start, size);
417  try {
418  HDFArray ha = new HDFArray(array);
419  data = ha.byteify();
420  ha = null;
421  } catch(HDFException he) {
422  throw new NexusException(he.getMessage());
423  }
424  nxputslab64(handle,data,start,size);
425  data = null;
426  }
427 
428  public void putattr(String name, Object array, int iType) throws NexusException {
429  byte data[];
430 
431  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
432  checkType(iType);
433  checkForNull(name, array);
434  try{
435  HDFArray ha = new HDFArray(array);
436  data = ha.byteify();
437  ha = null;
438  }catch(HDFException he) {
439  throw new NexusException(he.getMessage());
440  }
441  nxputattr(handle,name,data,iType);
442  data = null;
443  }
444 
445  // inquiry
446  //native methods for this section
447  protected native void nxgetinfo(int handle, int iDim[], int args[]);
448  protected native void nxgetinfo64(int handle, long iDim[], int args[]);
449  protected native void nxsetnumberformat(int handle, int type,
450  String format);
451  protected native int nextentry(int handle, String names[]);
452  protected native int nextattr(int handle, String names[], int args[]);
453  protected native void initattrdir(int handle);
454  protected native void initgroupdir(int handle);
455 
456  public void setnumberformat(int type, String format) throws NexusException {
457  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
458  checkType(type);
459  checkForNull(format);
460  nxsetnumberformat(handle,type,format);
461  }
462 
463  public void getinfo(int iDim[], int args[]) throws NexusException {
464  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
465  nxgetinfo(handle,iDim,args);
466  }
467 
468  public void getinfo(long iDim[], int args[]) throws NexusException {
469  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
470  nxgetinfo64(handle,iDim,args);
471  }
472 
473  public Hashtable groupdir() throws NexusException {
474  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
475  Hashtable h = new Hashtable();
476  String names[] = new String[2];
477 
479  while(nextentry(handle,names) != -1) {
480  h.put(names[0],names[1]);
481  }
482  return h;
483  }
484 
485  public Hashtable attrdir()throws NexusException {
486  int args[] = new int[2];
487  AttributeEntry at;
488  String names[] = new String[1];
489 
490  Hashtable h = new Hashtable();
491  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
493  while(nextattr(handle,names,args) != -1)
494  {
495  at = new AttributeEntry();
496  at.length = args[0];
497  at.type = args[1];
498  h.put(names[0], at);
499  }
500  return h;
501  }
502 
503  // linking
504  // native methods for this section
505  protected native void nxgetgroupid(int handle, NXlink link);
506  protected native void nxgetdataid(int handle, NXlink link);
507  protected native void nxmakelink(int handle, NXlink target);
508  protected native void nxmakenamedlink(int handle, String name, NXlink target);
509  protected native void nxopensourcepath(int handle);
510 
511  public NXlink getgroupID() throws NexusException {
512  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
513  NXlink l = new NXlink();
514  nxgetgroupid(handle,l);
515  return l;
516  }
517 
518  public NXlink getdataID()throws NexusException {
519  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
520  NXlink l = new NXlink();
521  nxgetdataid(handle,l);
522  return l;
523  }
524 
525  public void makelink(NXlink target) throws NexusException {
526  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
527  checkForNull(target);
528  nxmakelink(handle, target);
529  }
530 
531  public void makenamedlink(String name, NXlink target) throws NexusException {
532  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
533  checkForNull(name, target);
534  nxmakenamedlink(handle, name, target);
535  }
536 
537  public void opensourcepath() throws NexusException {
538  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
540  }
541 
546  private void checkForNull(Object... args) {
547  for (Object o : args)
548  if (o==null) throw new NullPointerException();
549  }
550 
555  private void checkForNegInArray(boolean allowUnlimited, int[]... args) {
556  for (int[] array : args)
557  for (int value: array) {
558  if (value<0)
559  if (value == this.NX_UNLIMITED && allowUnlimited) {
560  // all ok this time
561  } else
562  throw new IllegalArgumentException("negative dimension received");
563  }
564  }
565 
570  private void checkForNegInArray(boolean allowUnlimited, long[]... args) {
571  for (long[] array : args)
572  for (long value: array) {
573  if (value<0)
574  if (value == this.NX_UNLIMITED && allowUnlimited) {
575  // all ok this time
576  } else
577  throw new IllegalArgumentException("negative dimension received");
578  }
579  }
580 
587  private void checkType(int type) throws NexusException {
588  switch(type) {
589  case NexusFile.NX_FLOAT32:
590  case NexusFile.NX_FLOAT64:
591  case NexusFile.NX_INT8:
592  case NexusFile.NX_UINT8:
593  case NexusFile.NX_INT16:
594  case NexusFile.NX_UINT16:
595  case NexusFile.NX_INT32:
596  case NexusFile.NX_UINT32:
597  case NexusFile.NX_INT64:
598  case NexusFile.NX_UINT64:
599  case NexusFile.NX_CHAR:
600  break;
601  default:
602  throw new NexusException("Illegal number type requested");
603  }
604  }
605 
606  // external file interface
607  // native methods for this section
608  protected native void nxinquirefile(int handle, String names[]);
609  protected native void nxlinkexternal(int handle, String name, String nxclass, String nxurl);
610  protected native void nxlinkexternaldataset(int handle, String name, String nxurl);
611  protected native int nxisexternalgroup(int handle, String name, String nxclass, String nxurl[]);
612  protected native int nxisexternaldataset(int handle, String name, String nxurl[]);
613 
614  public String inquirefile() throws NexusException {
615  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
616  String names[] = new String[1];
617  nxinquirefile(handle,names);
618  return names[0];
619  }
620 
621  public void linkexternal(String name, String nxclass, String nxurl) throws NexusException {
622  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
623  checkForNull(name, nxclass, nxurl);
624  nxlinkexternal(handle,name,nxclass,nxurl);
625  }
626 
627  public void linkexternaldataset(String name, String nxurl) throws NexusException {
628  if(handle < 0) throw new NexusException("NAPI-ERROR: File not open");
629  checkForNull(name, nxurl);
630  nxlinkexternaldataset(handle,name,nxurl);
631  }
632 
633  public String isexternalgroup(String name, String nxclass) throws NexusException {
634  if (handle < 0) throw new NexusException("NAPI-ERROR: File not open");
635  checkForNull(name, nxclass);
636  String nxurl[] = new String[1];
637 
638  int status = nxisexternalgroup(handle,name,nxclass,nxurl);
639  if (status == 1) {
640  return nxurl[0];
641  } else {
642  return null;
643  }
644  }
645 
646  public String isexternaldataset(String name) throws NexusException {
647  if (handle < 0) throw new NexusException("NAPI-ERROR: File not open");
648  checkForNull(name);
649  String nxurl[] = new String[1];
650 
651  int status = nxisexternaldataset(handle,name,nxurl);
652  if (status == 1) {
653  return nxurl[0];
654  } else {
655  return null;
656  }
657  }
658 
668  public native void debugstop();
669 }