3 /* Copyright: Thomas McGlynn 1997-1999.
4 * This code may be used for any purpose, non-commercial
5 * or commercial so long as this copyright notice is retained
6 * in the source code or included in or referred to in any
8 * Many thanks to David Glowacki (U. Wisconsin) for substantial
9 * improvements, enhancements and bug fixes.
12 import nom.tam.util.*;
14 /** This class provides methods to access the data segment of an
17 public abstract class Data implements FitsElement {
19 /** This is the object which contains the actual data for the HDU.
21 * <li> For images and primary data this is a simple (but possibly
22 * multi-dimensional) primitive array. When group data is
23 * supported it will be a possibly multidimensional array
25 * <li> For ASCII data it is a two dimensional Object array where
26 * each of the constituent objects is a primitive array of length 1.
27 * <li> For Binary data it is a two dimensional Object array where
28 * each of the constituent objects is a primitive array of arbitrary
29 * (more or less) dimensionality.
32 /** The starting location of the data when last read */
33 protected long fileOffset = -1;
34 /** The size of the data when last read */
35 protected long dataSize;
36 /** The inputstream used. */
37 protected RandomAccess input;
39 /** Get the file offset */
40 public long getFileOffset() {
44 /** Set the fields needed for a re-read */
45 protected void setFileOffset(Object o) {
46 if (o instanceof RandomAccess) {
47 fileOffset = FitsUtil.findOffset(o);
48 dataSize = getTrueSize();
49 input = (RandomAccess) o;
53 /** Write the data -- including any buffering needed
54 * @param o The output stream on which to write the data.
56 public abstract void write(ArrayDataOutput o) throws FitsException;
58 /** Read a data array into the current object and if needed position
59 * to the beginning of the next FITS block.
60 * @param i The input data stream
62 public abstract void read(ArrayDataInput i) throws FitsException;
64 public void rewrite() throws FitsException {
67 throw new FitsException("Illegal attempt to rewrite data");
70 FitsUtil.reposition(input, fileOffset);
71 write((ArrayDataOutput) input);
73 ((ArrayDataOutput) input).flush();
74 } catch (IOException e) {
75 throw new FitsException("Error in rewrite flush: " + e);
79 public boolean reset() {
81 FitsUtil.reposition(input, fileOffset);
83 } catch (Exception e) {
88 public boolean rewriteable() {
91 || (getTrueSize() + 2879) / 2880 != (dataSize + 2879) / 2880) {
98 abstract long getTrueSize();
100 /** Get the size of the data element in bytes */
101 public long getSize() {
102 return FitsUtil.addPadding(getTrueSize());
105 /** Return the data array object.
107 public abstract Object getData() throws FitsException;
109 /** Return the non-FITS data object */
110 public Object getKernel() throws FitsException {
114 /** Modify a header to point to this data
116 abstract void fillHeader(Header head) throws FitsException;