NeXusJavaBindings  1
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
HDFArray.java
Go to the documentation of this file.
1 /****************************************************************************
2  * NCSA HDF *
3  * National Comptational Science Alliance *
4  * University of Illinois at Urbana-Champaign *
5  * 605 E. Springfield, Champaign IL 61820 *
6  * *
7  * For conditions of distribution and use, see the accompanying *
8  * hdf/COPYING file. *
9  * *
10  ****************************************************************************/
11 
12 package ncsa.hdf.hdflib;
13 
29 public class HDFArray {
30 
31 private Object _theArray = null;
32 private ArrayDescriptor _desc = null;
33 private byte [] _barray = null;
34 
35 public HDFArray(Object anArray) throws HDFException {
36 
37  if (anArray == null) {
38  HDFException ex =
39  (HDFException)new HDFJavaException("HDFArray: array is null?: ");
40 ex.printStackTrace();
41  }
42  Class tc = anArray.getClass();
43  if (tc.isArray() == false) {
44  /* exception: not an array */
45  HDFException ex =
46  (HDFException)new HDFJavaException("HDFArray: not an array?: ");
47 ex.printStackTrace();
48  throw(ex);
49  }
50  _theArray = anArray;
51  _desc = new ArrayDescriptor( _theArray );
52 
53  /* extra error checking -- probably not needed */
54  if (_desc == null ) {
55  HDFException ex =
56  (HDFException)new HDFJavaException("HDFArray: internal error: array description failed?: ");
57  throw(ex);
58  }
59 }
60 
67 public byte[] emptyBytes()
68 throws HDFException
69 {
70  byte[] b = null;
71  if (_desc.dims == 1 && _desc.NT == 'B') {
72  b = (byte [])_theArray;
73  } else {
74  b = new byte[_desc.totalSize];
75  }
76  if (b == null) {
77  System.out.println("Error: HDFArray can't allocate bytes for array");
78  HDFException ex =
79  (HDFException)new HDFJavaException("HDFArray: emptyBytes: allocation failed");
80  throw(ex);
81  }
82  return (b);
83  //return (new byte[_desc.totalSize]);
84 }
85 
94 public byte[] byteify() throws HDFException{
95 
96  if (_barray != null) return _barray;
97  if (_theArray == null) {
98  /* exception: not an array */
99  HDFException ex =
100  (HDFException)new HDFJavaException("HDFArray: byteify not an array?: ");
101  throw(ex);
102  }
103 
104  if (_desc.dims == 1) {
105  /* special case */
106  if (_desc.NT == 'B') {
107  /* really special case! */
108  _barray = (byte [])_theArray;
109  return _barray;
110  } else {
111  try {
112  _barray = new byte[_desc.totalSize];
113 
114  byte [] therow;
115  if (_desc.NT == 'I') {
116  therow = ncsa.hdf.hdflib.HDFNativeData.intToByte(0,_desc.dimlen[1],(int [])_theArray);
117  } else if (_desc.NT == 'S') {
118  therow = ncsa.hdf.hdflib.HDFNativeData.shortToByte(0,_desc.dimlen[1],(short [])_theArray);
119  } else if (_desc.NT == 'F') {
120  therow = ncsa.hdf.hdflib.HDFNativeData.floatToByte(0,_desc.dimlen[1],(float [])_theArray);
121  } else if (_desc.NT == 'J') {
122  therow = ncsa.hdf.hdflib.HDFNativeData.longToByte(0,_desc.dimlen[1],(long [])_theArray);
123  } else if (_desc.NT == 'D') {
124  therow = ncsa.hdf.hdflib.HDFNativeData.doubleToByte(0,_desc.dimlen[1],(double [])_theArray);
125  } else if (_desc.NT == 'L') {
126  if (_desc.className.equals("java.lang.Byte")) {
127  therow = ByteObjToByte((Byte[])_theArray);
128  } else if (_desc.className.equals("java.lang.Integer")) {
129  therow = IntegerToByte((Integer[])_theArray);
130  } else if (_desc.className.equals("java.lang.Short")) {
131  therow = ShortToByte((Short[])_theArray);
132  } else if (_desc.className.equals("java.lang.Float")) {
133  therow = FloatObjToByte((Float[])_theArray);
134  } else if (_desc.className.equals("java.lang.Double")) {
135  therow = DoubleObjToByte((Double[])_theArray);
136  } else if (_desc.className.equals("java.lang.Long")) {
137  therow = LongObjToByte((Long[])_theArray);
138  } else {
139  HDFJavaException ex =
140  new HDFJavaException("HDFArray: unknown type of Object?");
141  throw(ex);
142  }
143  } else {
144  HDFJavaException ex =
145  new HDFJavaException("HDFArray: unknown type of Object?");
146  throw(ex);
147  }
148  System.arraycopy(therow,0,_barray,0,(_desc.dimlen[1] * _desc.NTsize));
149  return _barray;
150  } catch (OutOfMemoryError err) {
151  HDFException ex =
152  (HDFException)new HDFJavaException("HDFArray: byteify array too big?");
153  ex.printStackTrace();
154  throw(ex);
155  }
156  }
157  }
158 
159  try {
160  _barray = new byte[_desc.totalSize];
161  } catch (OutOfMemoryError err) {
162  HDFException ex =
163  (HDFException)new HDFJavaException("HDFArray: byteify array too big?");
164  ex.printStackTrace();
165  throw(ex);
166  }
167 
168 
169  Object oo = _theArray;
170  int n = 0; /* the current byte */
171  int index = 0;
172  int i;
173  while ( n < _desc.totalSize ) {
174  oo = _desc.objs[0];
175  index = n / _desc.bytetoindex[0];
176  index %= _desc.dimlen[0];
177  for (i = 0 ; i < (_desc.dims); i++) {
178  index = n / _desc.bytetoindex[i];
179  index %= _desc.dimlen[i];
180 
181  if (index == _desc.currentindex[i]) {
182  /* then use cached copy */
183  oo = _desc.objs[i];
184  } else {
185  /* check range of index */
186  if (index > (_desc.dimlen[i] - 1)) {
187  System.out.println("out of bounds?");
188  return null;
189  }
190  oo = java.lang.reflect.Array.get((Object) oo,index);
191  _desc.currentindex[i] = index;
192  _desc.objs[i] = oo;
193  }
194  }
195 
196  /* byte-ify */
197  byte arow[];
198  try {
199  if (_desc.NT == 'J') {
200  arow = ncsa.hdf.hdflib.HDFNativeData.longToByte(0,_desc.dimlen[_desc.dims],(long [])_desc.objs[_desc.dims - 1]);
201  arow = ncsa.hdf.hdflib.HDFNativeData.longToByte(0,_desc.dimlen[_desc.dims],(long [])_desc.objs[_desc.dims - 1]);
202  } else if (_desc.NT == 'I') {
203  arow = ncsa.hdf.hdflib.HDFNativeData.intToByte(0,_desc.dimlen[_desc.dims],(int [])_desc.objs[_desc.dims - 1]);
204  } else if (_desc.NT == 'S') {
205  arow = ncsa.hdf.hdflib.HDFNativeData.shortToByte(0,_desc.dimlen[_desc.dims],(short [])_desc.objs[_desc.dims - 1]);
206  } else if (_desc.NT == 'B') {
207  arow = (byte [])_desc.objs[_desc.dims - 1];
208  } else if (_desc.NT == 'F') {
209  /* 32 bit float */
210  arow = ncsa.hdf.hdflib.HDFNativeData.floatToByte(0,_desc.dimlen[_desc.dims],(float [])_desc.objs[_desc.dims - 1]);
211  } else if (_desc.NT == 'D') {
212  /* 64 bit float */
213  arow = ncsa.hdf.hdflib.HDFNativeData.doubleToByte(0,_desc.dimlen[_desc.dims],(double [])_desc.objs[_desc.dims - 1]);
214  } else if (_desc.NT == 'L') {
215  if (_desc.className.equals("java.lang.Byte")) {
216  arow = ByteObjToByte((Byte[])_desc.objs[_desc.dims - 1]);
217  } else if (_desc.className.equals("java.lang.Integer")) {
218  arow = IntegerToByte((Integer[])_desc.objs[_desc.dims - 1]);
219  } else if (_desc.className.equals("java.lang.Short")) {
220  arow = ShortToByte((Short[])_desc.objs[_desc.dims - 1]);
221  } else if (_desc.className.equals("java.lang.Float")) {
222  arow = FloatObjToByte((Float[])_desc.objs[_desc.dims - 1]);
223  } else if (_desc.className.equals("java.lang.Double")) {
224  arow = DoubleObjToByte((Double[])_desc.objs[_desc.dims - 1]);
225  } else if (_desc.className.equals("java.lang.Long")) {
226  arow = LongObjToByte((Long[])_desc.objs[_desc.dims - 1]);
227  } else {
228  HDFJavaException ex =
229  new HDFJavaException("HDFArray: byteify Object type not implemented?");
230  throw(ex);
231  }
232  } else {
233  HDFJavaException ex =
234  new HDFJavaException("HDFArray: byteify Object type not implemented?");
235  throw(ex);
236  }
237  System.arraycopy(arow,0,_barray,n,(_desc.dimlen[_desc.dims] * _desc.NTsize));
238  n += _desc.bytetoindex[_desc.dims - 1];
239  } catch (OutOfMemoryError err) {
240  HDFException ex =
241  (HDFException)new HDFJavaException("HDFArray: byteify array too big?");
242  ex.printStackTrace();
243  throw(ex);
244  }
245  }
246 /* assert: the whole array is completed--currentindex should == len - 1 */
247 
248  /* error checks */
249 
250  if (n < _desc.totalSize) {
251  throw new java.lang.InternalError(
252  new String("HDFArray:::byteify: Panic didn't complete all input data: n= "+n+" size = "+_desc.totalSize));
253  }
254  for (i = 0;i < _desc.dims; i++) {
255  if (_desc.currentindex[i] != _desc.dimlen[i] - 1) {
256  throw new java.lang.InternalError(
257  new String("Panic didn't complete all data: currentindex["+i+"] = "+_desc.currentindex[i]+" (should be "+(_desc.dimlen[i] - 1)+" ?)"));
258  }
259  }
260  return _barray;
261 }
262 
271 public Object arrayify(byte[] bytes) throws HDFException {
272 
273  if (_theArray == null) {
274  /* exception: not an array */
275  HDFException ex =
276  (HDFException)new HDFJavaException("arrayify: not an array?: ");
277  throw(ex);
278  }
279 
280  if (java.lang.reflect.Array.getLength((Object) bytes) != _desc.totalSize) {
281  /* exception: array not right size */
282  HDFException ex =
283  (HDFException)new HDFJavaException("arrayify: array is wrong size?: ");
284  }
285  _barray = bytes; /* hope that the bytes are correct.... */
286  if (_desc.dims == 1) {
287  /* special case */
288  /* 2 data copies here! */
289  try {
290  if (_desc.NT == 'I') {
291  int [] x = (int [])ncsa.hdf.hdflib.HDFNativeData.byteToInt(_barray);
292  System.arraycopy(x,0,_theArray,0,_desc.dimlen[1]);
293  return _theArray;
294  } else if (_desc.NT == 'S') {
295  short [] x = ncsa.hdf.hdflib.HDFNativeData.byteToShort(_barray);
296  System.arraycopy(x,0,_theArray,0,_desc.dimlen[1]);
297  return _theArray;
298  } else if (_desc.NT == 'F') {
299  float x[] = ncsa.hdf.hdflib.HDFNativeData.byteToFloat(_barray);
300  System.arraycopy(x,0,_theArray,0,_desc.dimlen[1]);
301  return _theArray;
302  } else if (_desc.NT == 'J') {
303  long x[] = ncsa.hdf.hdflib.HDFNativeData.byteToLong(_barray);
304  System.arraycopy(x,0,_theArray,0,_desc.dimlen[1]);
305  return _theArray;
306  } else if (_desc.NT == 'D') {
307  double x[] = ncsa.hdf.hdflib.HDFNativeData.byteToDouble(_barray);
308  System.arraycopy(x,0,_theArray,0,_desc.dimlen[1]);
309  return _theArray;
310  } else if (_desc.NT == 'B') {
311  System.arraycopy(_barray,0,_theArray,0,_desc.dimlen[1]);
312  return _theArray;
313  } else if (_desc.NT == 'L') {
314  if (_desc.className.equals("java.lang.Byte")) {
315  Byte I[] = ByteToByteObj(_barray);
316  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
317  return _theArray;
318  } else if (_desc.className.equals("java.lang.Integer")) {
319  Integer I[] = ByteToInteger(_barray);
320  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
321  return _theArray;
322  } else if (_desc.className.equals("java.lang.Short")) {
323  Short I[] = ByteToShort(_barray);
324  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
325  return _theArray;
326  } else if (_desc.className.equals("java.lang.Float")) {
327  Float I[] = ByteToFloatObj(_barray);
328  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
329  return _theArray;
330  } else if (_desc.className.equals("java.lang.Double")) {
331  Double I[] = ByteToDoubleObj(_barray);
332  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
333  return _theArray;
334  } else if (_desc.className.equals("java.lang.Long")) {
335  Long I[] = ByteToLongObj(_barray);
336  System.arraycopy(I,0,_theArray,0,_desc.dimlen[1]);
337  return _theArray;
338  } else {
339  HDFJavaException ex =
340  new HDFJavaException("arrayify: Object type not implemented yet...");
341  throw(ex);
342  }
343  } else {
344  HDFJavaException ex =
345  new HDFJavaException("arrayify: Object type not implemented yet...");
346  throw(ex);
347  }
348  } catch (OutOfMemoryError err) {
349  HDFException ex =
350  (HDFException)new HDFJavaException("HDFArray: arrayify array too big?");
351  ex.printStackTrace();
352  throw(ex);
353  }
354  }
355  /* Assert dims >= 2 */
356 
357  Object oo = _theArray;
358  int n = 0; /* the current byte */
359  int index = 0;
360  int i;
361  while ( n < _desc.totalSize ) {
362  oo = _desc.objs[0];
363  index = n / _desc.bytetoindex[0];
364  index %= _desc.dimlen[0];
365  for (i = 0 ; i < (_desc.dims); i++) {
366  index = n / _desc.bytetoindex[i];
367  index %= _desc.dimlen[i];
368 
369  if (index == _desc.currentindex[i]) {
370  /* then use cached copy */
371  oo = _desc.objs[i];
372  } else {
373  /* check range of index */
374  if (index > (_desc.dimlen[i] - 1)) {
375  System.out.println("out of bounds?");
376  return null;
377  }
378  oo = java.lang.reflect.Array.get((Object) oo,index);
379  _desc.currentindex[i] = index;
380  _desc.objs[i] = oo;
381  }
382  }
383 
384  /* array-ify */
385  try {
386  if (_desc.NT == 'J') {
387  long [] arow = ncsa.hdf.hdflib.HDFNativeData.byteToLong(n,_desc.dimlen[_desc.dims],_barray);
388  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
389  (_desc.currentindex[_desc.dims - 1]), (Object)arow);
390  n += _desc.bytetoindex[_desc.dims - 1];
391  _desc.currentindex[_desc.dims - 1]++;
392  } else if (_desc.NT == 'I') {
393  int [] arow = ncsa.hdf.hdflib.HDFNativeData.byteToInt(n,_desc.dimlen[_desc.dims],_barray);
394  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
395  (_desc.currentindex[_desc.dims - 1]), (Object)arow);
396 
397  n += _desc.bytetoindex[_desc.dims - 1];
398  _desc.currentindex[_desc.dims - 1]++;
399  } else if (_desc.NT == 'S') {
400  short [] arow = ncsa.hdf.hdflib.HDFNativeData.byteToShort(n,_desc.dimlen[_desc.dims],_barray);
401  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
402  (_desc.currentindex[_desc.dims - 1]), (Object)arow);
403 
404  n += _desc.bytetoindex[_desc.dims - 1];
405  _desc.currentindex[_desc.dims - 1]++;
406  } else if (_desc.NT == 'B') {
407  System.arraycopy( _barray, n, _desc.objs[_desc.dims - 1], 0, _desc.dimlen[_desc.dims]);
408  n += _desc.bytetoindex[_desc.dims - 1];
409  } else if (_desc.NT == 'F') {
410  float arow[] = ncsa.hdf.hdflib.HDFNativeData.byteToFloat(n,_desc.dimlen[_desc.dims],_barray);
411  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
412  (_desc.currentindex[_desc.dims - 1]), (Object)arow);
413 
414  n += _desc.bytetoindex[_desc.dims - 1];
415  _desc.currentindex[_desc.dims - 1]++;
416  } else if (_desc.NT == 'D') {
417  double [] arow = ncsa.hdf.hdflib.HDFNativeData.byteToDouble(n,_desc.dimlen[_desc.dims],_barray);
418  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
419  (_desc.currentindex[_desc.dims - 1]), (Object)arow);
420 
421  n += _desc.bytetoindex[_desc.dims - 1];
422  _desc.currentindex[_desc.dims - 1]++;
423  } else if (_desc.NT == 'L') {
424  if (_desc.className.equals("java.lang.Byte")) {
425  Byte I[] = ByteToByteObj(n,_desc.dimlen[_desc.dims],_barray);
426  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
427  (_desc.currentindex[_desc.dims - 1]),
428  (Object)I);
429 
430  n += _desc.bytetoindex[_desc.dims - 1];
431  _desc.currentindex[_desc.dims - 1]++;
432  } else if (_desc.className.equals("java.lang.Integer")) {
433  Integer I[] = ByteToInteger(n,_desc.dimlen[_desc.dims],_barray);
434  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
435  (_desc.currentindex[_desc.dims - 1]),
436  (Object)I);
437 
438  n += _desc.bytetoindex[_desc.dims - 1];
439  _desc.currentindex[_desc.dims - 1]++;
440  } else if (_desc.className.equals("java.lang.Short")) {
441  Short I[] = ByteToShort(n,_desc.dimlen[_desc.dims],_barray);
442  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
443  (_desc.currentindex[_desc.dims - 1]),
444  (Object)I);
445 
446  n += _desc.bytetoindex[_desc.dims - 1];
447  _desc.currentindex[_desc.dims - 1]++;
448  } else if (_desc.className.equals("java.lang.Float")) {
449  Float I[] = ByteToFloatObj(n,_desc.dimlen[_desc.dims],_barray);
450  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
451  (_desc.currentindex[_desc.dims - 1]),
452  (Object)I);
453 
454  n += _desc.bytetoindex[_desc.dims - 1];
455  _desc.currentindex[_desc.dims - 1]++;
456  } else if (_desc.className.equals("java.lang.Double")) {
457  Double I[] = ByteToDoubleObj(n,_desc.dimlen[_desc.dims],_barray);
458  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
459  (_desc.currentindex[_desc.dims - 1]),
460  (Object)I);
461 
462  n += _desc.bytetoindex[_desc.dims - 1];
463  _desc.currentindex[_desc.dims - 1]++;
464  } else if (_desc.className.equals("java.lang.Long")) {
465  Long I[] = ByteToLongObj(n,_desc.dimlen[_desc.dims],_barray);
466  java.lang.reflect.Array.set(_desc.objs[_desc.dims - 2] ,
467  (_desc.currentindex[_desc.dims - 1]),
468  (Object)I);
469 
470  n += _desc.bytetoindex[_desc.dims - 1];
471  _desc.currentindex[_desc.dims - 1]++;
472  } else {
473  HDFJavaException ex =
474  new HDFJavaException("HDFArray: unsupported Object type: "+_desc.NT);
475  throw(ex);
476  }
477  } else {
478  HDFJavaException ex =
479  new HDFJavaException("HDFArray: unsupported Object type: "+_desc.NT);
480  throw(ex);
481  }
482  } catch (OutOfMemoryError err) {
483  HDFException ex =
484  (HDFException)new HDFJavaException("HDFArray: arrayify array too big?");
485  ex.printStackTrace();
486  throw(ex);
487  }
488 
489  }
490 
491 /* assert: the whole array is completed--currentindex should == len - 1 */
492 
493  /* error checks */
494 
495  if (n < _desc.totalSize) {
496  throw new java.lang.InternalError(
497  new String("HDFArray::arrayify Panic didn't complete all input data: n= "+n+" size = "+_desc.totalSize));
498  }
499  for (i = 0;i <= _desc.dims-2; i++) {
500  if (_desc.currentindex[i] != _desc.dimlen[i] - 1) {
501  throw new java.lang.InternalError(
502  new String("HDFArray::arrayify Panic didn't complete all data: currentindex["+i+"] = "+_desc.currentindex[i]+" (should be "+(_desc.dimlen[i] - 1)+"?"));
503  }
504  }
505  if (_desc.NT != 'B') {
506  if (_desc.currentindex[_desc.dims - 1] != _desc.dimlen[_desc.dims - 1]) {
507  throw new java.lang.InternalError(
508  new String("HDFArray::arrayify Panic didn't complete all data: currentindex["+i+"] = "+_desc.currentindex[i]+" (should be "+(_desc.dimlen[i])+"?"));
509  }
510  } else {
511  if (_desc.currentindex[_desc.dims - 1] != (_desc.dimlen[_desc.dims - 1] - 1)) {
512  throw new java.lang.InternalError(
513  new String("HDFArray::arrayify Panic didn't complete all data: currentindex["+i+"] = "+_desc.currentindex[i]+" (should be "+(_desc.dimlen[i] - 1)+"?"));
514  }
515  }
516 
517  return _theArray;
518 }
519 
520 private byte[] IntegerToByte( Integer in[] ) {
521  int nelems = java.lang.reflect.Array.getLength((Object)in);
522  int[] out = new int[nelems];
523 
524  for (int i = 0; i < nelems; i++) {
525  out[i] = in[i].intValue();
526  }
527  return HDFNativeData.intToByte(0,nelems,out);
528 }
529 
530 private Integer[] ByteToInteger( byte[] bin ) {
531  int in[] = (int [])HDFNativeData.byteToInt(bin);
532  int nelems = java.lang.reflect.Array.getLength((Object)in);
533  Integer[] out = new Integer[nelems];
534 
535  for (int i = 0; i < nelems; i++) {
536  out[i] = new Integer(in[i]);
537  }
538  return out;
539 }
540 private Integer[] ByteToInteger( int start, int len, byte[] bin ) {
541  int in[] = (int [])HDFNativeData.byteToInt(start,len,bin);
542  int nelems = java.lang.reflect.Array.getLength((Object)in);
543  Integer[] out = new Integer[nelems];
544 
545  for (int i = 0; i < nelems; i++) {
546  out[i] = new Integer(in[i]);
547  }
548  return out;
549 }
550 
551 
552 private byte[] ShortToByte( Short in[] ) {
553  int nelems = java.lang.reflect.Array.getLength((Object)in);
554  short[] out = new short[nelems];
555 
556  for (int i = 0; i < nelems; i++) {
557  out[i] = in[i].shortValue();
558  }
559  return HDFNativeData.shortToByte(0,nelems,out);
560 }
561 
562 private Short[] ByteToShort( byte[] bin ) {
563  short in[] = (short [])HDFNativeData.byteToShort(bin);
564  int nelems = java.lang.reflect.Array.getLength((Object)in);
565  Short[] out = new Short[nelems];
566 
567  for (int i = 0; i < nelems; i++) {
568  out[i] = new Short(in[i]);
569  }
570  return out;
571 }
572 
573 private Short[] ByteToShort( int start, int len, byte[] bin ) {
574  short in[] = (short [])HDFNativeData.byteToShort(start,len,bin);
575  int nelems = java.lang.reflect.Array.getLength((Object)in);
576  Short[] out = new Short[nelems];
577 
578  for (int i = 0; i < nelems; i++) {
579  out[i] = new Short(in[i]);
580  }
581  return out;
582 }
583 
584 private byte[] ByteObjToByte( Byte in[] ) {
585  int nelems = java.lang.reflect.Array.getLength((Object)in);
586  byte[] out = new byte[nelems];
587 
588  for (int i = 0; i < nelems; i++) {
589  out[i] = in[i].byteValue();
590  }
591  return out;
592 }
593 
594 private Byte[] ByteToByteObj( byte[] bin ) {
595  int nelems = java.lang.reflect.Array.getLength((Object)bin);
596  Byte[] out = new Byte[nelems];
597 
598  for (int i = 0; i < nelems; i++) {
599  out[i] = new Byte(bin[i]);
600  }
601  return out;
602 }
603 
604 private Byte[] ByteToByteObj( int start, int len, byte[] bin ) {
605  Byte[] out = new Byte[len];
606 
607  for (int i = 0; i < len; i++) {
608  out[i] = new Byte(bin[i]);
609  }
610  return out;
611 }
612 
613 private byte[] FloatObjToByte( Float in[] ) {
614  int nelems = java.lang.reflect.Array.getLength((Object)in);
615  float[] out = new float[nelems];
616 
617  for (int i = 0; i < nelems; i++) {
618  out[i] = in[i].floatValue();
619  }
620  return HDFNativeData.floatToByte(0,nelems,out);
621 }
622 
623 private Float[] ByteToFloatObj( byte[] bin ) {
624  float in[] = (float [])HDFNativeData.byteToFloat(bin);
625  int nelems = java.lang.reflect.Array.getLength((Object)in);
626  Float[] out = new Float[nelems];
627 
628  for (int i = 0; i < nelems; i++) {
629  out[i] = new Float(in[i]);
630  }
631  return out;
632 }
633 
634 private Float[] ByteToFloatObj( int start, int len, byte[] bin ) {
635  float in[] = (float [])HDFNativeData.byteToFloat(start,len,bin);
636  int nelems = java.lang.reflect.Array.getLength((Object)in);
637  Float[] out = new Float[nelems];
638 
639  for (int i = 0; i < nelems; i++) {
640  out[i] = new Float(in[i]);
641  }
642  return out;
643 }
644 
645 private byte[] DoubleObjToByte( Double in[] ) {
646  int nelems = java.lang.reflect.Array.getLength((Object)in);
647  double[] out = new double[nelems];
648 
649  for (int i = 0; i < nelems; i++) {
650  out[i] = in[i].doubleValue();
651  }
652  return HDFNativeData.doubleToByte(0,nelems,out);
653 }
654 
655 private Double[] ByteToDoubleObj( byte[] bin ) {
656  double in[] = (double [])HDFNativeData.byteToDouble(bin);
657  int nelems = java.lang.reflect.Array.getLength((Object)in);
658  Double[] out = new Double[nelems];
659 
660  for (int i = 0; i < nelems; i++) {
661  out[i] = new Double(in[i]);
662  }
663  return out;
664 }
665 
666 private Double[] ByteToDoubleObj( int start, int len, byte[] bin ) {
667  double in[] = (double [])HDFNativeData.byteToDouble(start,len,bin);
668  int nelems = java.lang.reflect.Array.getLength((Object)in);
669  Double[] out = new Double[nelems];
670 
671  for (int i = 0; i < nelems; i++) {
672  out[i] = new Double(in[i]);
673  }
674  return out;
675 }
676 
677 private byte[] LongObjToByte( Long in[] ) {
678  int nelems = java.lang.reflect.Array.getLength((Object)in);
679  long[] out = new long[nelems];
680 
681  for (int i = 0; i < nelems; i++) {
682  out[i] = in[i].longValue();
683  }
684  return HDFNativeData.longToByte(0,nelems,out);
685 }
686 
687 private Long[] ByteToLongObj( byte[] bin ) {
688  long in[] = (long [])HDFNativeData.byteToLong(bin);
689  int nelems = java.lang.reflect.Array.getLength((Object)in);
690  Long[] out = new Long[nelems];
691 
692  for (int i = 0; i < nelems; i++) {
693  out[i] = new Long(in[i]);
694  }
695  return out;
696 }
697 
698 private Long[] ByteToLongObj( int start, int len, byte[] bin ) {
699  long in[] = (long [])HDFNativeData.byteToLong(start,len,bin);
700  int nelems = java.lang.reflect.Array.getLength((Object)in);
701  Long[] out = new Long[nelems];
702 
703  for (int i = 0; i < nelems; i++) {
704  out[i] = new Long(in[i]);
705  }
706  return out;
707 }
708 
709 }
710 
711 
717 class ArrayDescriptor {
718 
719  static String theType = "";
720  static Class theClass = null;
721  static int [] dimlen = null;
722  static int [] dimstart = null;
723  static int [] currentindex = null;
724  static int [] bytetoindex = null;
725  static int totalSize = 0;
726  static Object [] objs = null;
727  static char NT = ' '; /* must be B,S,I,L,F,D, else error */
728  static int NTsize = 0;
729  static int dims = 0;
730  static String className;
731 
732  public ArrayDescriptor ( Object anArray ) throws HDFException {
733 
734  Class tc = anArray.getClass();
735  if (tc.isArray() == false) {
736  /* exception: not an array */
737  HDFException ex =
738  (HDFException)new HDFJavaException("ArrayDescriptor: not an array?: ");
739  throw(ex);
740  }
741 
742  theClass = tc;
743 
744  /* parse the type descriptor to discover the
745  shape of the array */
746  String ss = tc.toString();
747  theType = ss;
748  int n = 6;
749  dims = 0;
750  char c = ' ';
751  while (n < ss.length()) {
752  c = ss.charAt(n);
753  n++;
754  if (c == '[') {
755  dims++;
756  }
757  }
758 
759  String css = ss.substring(ss.lastIndexOf('[')+1);
760  Class compC = tc.getComponentType();
761  String cs = compC.toString();
762  NT = c;
763  if (NT == 'B') {
764  NTsize = 1;
765  } else if (NT == 'S') {
766  NTsize = 2;
767  } else if ((NT == 'I') || (NT == 'F')) {
768  NTsize = 4;
769  } else if ((NT == 'J') || (NT == 'D')){
770  NTsize = 8;
771  } else if (css.startsWith("Ljava.lang.Byte")) {
772  NT='L';
773  className = "java.lang.Byte";
774  NTsize = 1;
775  } else if (css.startsWith("Ljava.lang.Short")) {
776  NT='L';
777  className = "java.lang.Short";
778  NTsize = 2;
779  } else if (css.startsWith("Ljava.lang.Integer")) {
780  NT='L';
781  className = "java.lang.Integer";
782  NTsize = 4;
783  } else if (css.startsWith("Ljava.lang.Float")) {
784  NT='L';
785  className = "java.lang.Float";
786  NTsize = 4;
787  } else if (css.startsWith("Ljava.lang.Double")) {
788  NT='L';
789  className = "java.lang.Double";
790  NTsize = 8;
791  } else if (css.startsWith("Ljava.lang.Long")) {
792  NT='L';
793  className = "java.lang.Long";
794  NTsize = 8;
795  } else if (css.startsWith("Ljava.lang.String")) {
796 throw new HDFJavaException(new String("ArrayDesciptor: Error: String array not supported yet"));
797  } else {
798  /* exception: not a numeric type */
799 throw new HDFJavaException(new String("Error: array is not numeric? (type is "+css+")"));
800  }
801 
802  /* fill in the table */
803  dimlen = new int [dims+1];
804  dimstart = new int [dims+1];
805  currentindex = new int [dims+1];
806  bytetoindex = new int [dims+1];
807  objs = new Object [dims+1];
808 
809  Object o = anArray;
810  objs[0] = o;
811  dimlen[0]= 1;
812  dimstart[0] = 0;
813  currentindex[0] = 0;
814  int i;
815  for ( i = 1; i <= dims; i++) {
816  dimlen[i]= java.lang.reflect.Array.getLength((Object) o);
817  o = java.lang.reflect.Array.get((Object) o,0);
818  objs [i] = o;
819  dimstart[i] = 0;
820  currentindex[i] = 0;
821  }
822 
823  int j;
824  int dd;
825  bytetoindex[dims] = NTsize;
826  for ( i = dims; i >= 0; i--) {
827  dd = NTsize;
828  for (j = i; j < dims; j++) {
829  dd *= dimlen[j + 1];
830  }
831  bytetoindex[i] = dd;
832  }
833 
834  totalSize = bytetoindex[0];
835  }
836 
837  public void dumpInfo()
838  {
839  System.out.println("Type: "+theType);
840  System.out.println("Class: "+theClass);
841  System.out.println("NT: "+NT+" NTsize: "+NTsize);
842  System.out.println("Array has "+dims+" dimensions ("+totalSize+" bytes)");
843  int i;
844  for (i = 0; i <= dims; i++) {
845  Class tc = objs[i].getClass();
846  String ss = tc.toString();
847  System.out.println(i+": start "+dimstart[i]+": len "+dimlen[i]+" current "+currentindex[i]+" bytetoindex "+bytetoindex[i]+" object "+objs[i]+" otype "+ss);
848  }
849  }
850 }