7 /* Copyright: Thomas McGlynn 1997-1999.
8 * This code may be used for any purpose, non-commercial
9 * or commercial so long as this copyright notice is retained
10 * in the source code or included in or referred to in any
13 * Many thanks to David Glowacki (U. Wisconsin) for substantial
14 * improvements, enhancements and bug fixes.
16 /** This class provides a simple holder for data which is
17 * not handled by other classes.
19 public class UndefinedData extends Data {
21 /** The size of the data */
25 public UndefinedData(Header h) throws FitsException {
27 /** Just get a byte buffer to hold the data.
29 // Bug fix by Vincenzo Forzi.
30 int naxis = h.getIntValue("NAXIS");
32 int size = naxis > 0 ? 1 : 0;
33 for (int i = 0; i < naxis; i += 1) {
34 size *= h.getIntValue("NAXIS" + (i + 1));
36 size += h.getIntValue("PCOUNT");
37 if (h.getIntValue("GCOUNT") > 1) {
38 size *= h.getIntValue("GCOUNT");
40 size *= Math.abs(h.getIntValue("BITPIX") / 8);
42 data = new byte[size];
46 /** Create an UndefinedData object using the specified object.
48 public UndefinedData(Object x) {
50 byteSize = ArrayFuncs.computeLSize(x);
51 data = new byte[(int) byteSize];
54 /** Fill header with keywords that describe data.
55 * @param head The FITS header
57 protected void fillHeader(Header head) {
60 head.setXtension("UNKNOWN");
63 head.addValue("NAXIS1", byteSize,"ntf::undefineddata:naxis1:1");
64 head.addValue("PCOUNT", 0, "ntf::undefineddata:pcount:1");
65 head.addValue("GCOUNT", 1, "ntf::undefineddata:gcount:1");
66 head.addValue("EXTEND", true, "ntf::undefineddata:extend:1"); // Just in case!
67 } catch (HeaderCardException e) {
68 System.err.println("Unable to create unknown header:" + e);
73 public void read(ArrayDataInput i) throws FitsException {
76 if (i instanceof RandomAccess) {
78 i.skipBytes(byteSize);
79 } catch (IOException e) {
80 throw new FitsException("Unable to skip over data:" + e);
86 } catch (IOException e) {
87 throw new FitsException("Unable to read unknown data:" + e);
92 int pad = FitsUtil.padding(getTrueSize());
95 } catch (EOFException e) {
96 throw new PaddingException("EOF skipping padding in undefined data", this);
97 } catch (IOException e) {
98 throw new FitsException("Error skipping padding in undefined data");
102 public void write(ArrayDataOutput o) throws FitsException {
109 throw new FitsException("Null unknown data");
114 } catch (IOException e) {
115 throw new FitsException("IO Error on unknown data write" + e);
118 FitsUtil.pad(o, getTrueSize());
122 /** Get the size in bytes of the data */
123 protected long getTrueSize() {
127 /** Return the actual data.
128 * Note that this may return a null when
129 * the data is not readable. It might be better
130 * to throw a FitsException, but this is
131 * a very commonly called method and we prefered
132 * not to change how users must invoke it.
134 public Object getData() {
139 FitsUtil.reposition(input, fileOffset);
141 } catch (Exception e) {