Begin versioning.
[fits.git] / src / nom / tam / fits / UndefinedData.java
1 package nom.tam.fits;
2
3 import nom.tam.util.*;
4 import java.io.*;
5
6
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
11  * derived software.
12  *
13  * Many thanks to David Glowacki (U. Wisconsin) for substantial
14  * improvements, enhancements and bug fixes.
15  */
16 /** This class provides a simple holder for data which is
17  * not handled by other classes.
18  */
19 public class UndefinedData extends Data {
20
21     /** The size of the data */
22     long byteSize;
23     byte[] data;
24
25     public UndefinedData(Header h) throws FitsException {
26
27         /** Just get a byte buffer to hold the data.
28          */
29         // Bug fix by Vincenzo Forzi.
30         int naxis = h.getIntValue("NAXIS");
31
32         int size = naxis > 0 ? 1 : 0;
33         for (int i = 0; i < naxis; i += 1) {
34             size *= h.getIntValue("NAXIS" + (i + 1));
35         }
36         size += h.getIntValue("PCOUNT");
37         if (h.getIntValue("GCOUNT") > 1) {
38             size *= h.getIntValue("GCOUNT");
39         }
40         size *= Math.abs(h.getIntValue("BITPIX") / 8);
41
42         data = new byte[size];
43         byteSize = size;
44     }
45
46     /** Create an UndefinedData object using the specified object.
47      */
48     public UndefinedData(Object x) {
49
50         byteSize = ArrayFuncs.computeLSize(x);
51         data = new byte[(int) byteSize];
52     }
53
54     /** Fill header with keywords that describe data.
55      * @param head The FITS header
56      */
57     protected void fillHeader(Header head) {
58
59         try {
60             head.setXtension("UNKNOWN");
61             head.setBitpix(8);
62             head.setNaxes(1);
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);
69         }
70
71     }
72
73     public void read(ArrayDataInput i) throws FitsException {
74         setFileOffset(i);
75
76         if (i instanceof RandomAccess) {
77             try {
78                 i.skipBytes(byteSize);
79             } catch (IOException e) {
80                 throw new FitsException("Unable to skip over data:" + e);
81             }
82
83         } else {
84             try {
85                 i.readFully(data);
86             } catch (IOException e) {
87                 throw new FitsException("Unable to read unknown data:" + e);
88             }
89
90         }
91
92         int pad = FitsUtil.padding(getTrueSize());
93         try {
94             i.skipBytes(pad);
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");
99         }
100     }
101
102     public void write(ArrayDataOutput o) throws FitsException {
103
104         if (data == null) {
105             getData();
106         }
107
108         if (data == null) {
109             throw new FitsException("Null unknown data");
110         }
111
112         try {
113             o.write(data);
114         } catch (IOException e) {
115             throw new FitsException("IO Error on unknown data write" + e);
116         }
117
118         FitsUtil.pad(o, getTrueSize());
119
120     }
121
122     /** Get the size in bytes of the data */
123     protected long getTrueSize() {
124         return byteSize;
125     }
126
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.
133      */
134     public Object getData() {
135
136         if (data == null) {
137
138             try {
139                 FitsUtil.reposition(input, fileOffset);
140                 input.read(data);
141             } catch (Exception e) {
142                 return null;
143             }
144         }
145
146         return data;
147     }
148 }