View Javadoc
1   package nom.tam.fits;
2   
3   /*
4    * #%L
5    * nom.tam FITS library
6    * %%
7    * Copyright (C) 2004 - 2024 nom-tam-fits
8    * %%
9    * This is free and unencumbered software released into the public domain.
10   *
11   * Anyone is free to copy, modify, publish, use, compile, sell, or
12   * distribute this software, either in source code form or as a compiled
13   * binary, for any purpose, commercial or non-commercial, and by any
14   * means.
15   *
16   * In jurisdictions that recognize copyright laws, the author or authors
17   * of this software dedicate any and all copyright interest in the
18   * software to the public domain. We make this dedication for the benefit
19   * of the public at large and to the detriment of our heirs and
20   * successors. We intend this dedication to be an overt act of
21   * relinquishment in perpetuity of all present and future rights to this
22   * software under copyright law.
23   *
24   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30   * OTHER DEALINGS IN THE SOFTWARE.
31   * #L%
32   */
33  
34  import java.io.IOException;
35  
36  import nom.tam.util.ArrayDataInput;
37  import nom.tam.util.ArrayDataOutput;
38  import nom.tam.util.ByteArrayIO;
39  import nom.tam.util.FitsDecoder;
40  import nom.tam.util.FitsEncoder;
41  
42  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
43  
44  /**
45   * Heap for storing variable-length entries in binary tables. FITS binary tables store variable length arrays on a heap,
46   * following the regular array data. The newer implementation of the heap now provides proper random access to the byte
47   * buffer as of version 1.16.
48   */
49  public class FitsHeap implements FitsElement {
50  
51      /** The minimum stoprage size to allocate for the heap, from which it can grow as necessary */
52      private static final int MIN_HEAP_CAPACITY = 16384;
53  
54      // TODO
55      // AK: In principle we could use ReadWriteAccess interface as the storage, which can be either an in-memory
56      // array or a buffered file region. The latter could support heaps over 2G, and could reduce memory overhead
57      // for heap access in some future release...
58      /** The underlying storage space of the heap */
59      private ByteArrayIO store;
60  
61      /** conversion from Java arrays to FITS binary representation */
62      private FitsEncoder encoder;
63  
64      /** conversion from FITS binary representation to Java arrays */
65      private FitsDecoder decoder;
66  
67      /**
68       * Construct a new uninitialized FITS heap object.
69       */
70      private FitsHeap() {
71      }
72  
73      /**
74       * Creates a heap of a given initial size. The new heap is initialized with 0's, and up to the specified number of
75       * bytes are immediately available for reading (as zeroes). The heap can grow as needed if more data is written into
76       * it.
77       *
78       * @throws IllegalArgumentException if the size argument is negative.
79       */
80      FitsHeap(int size) throws IllegalArgumentException {
81          if (size < 0) {
82              throw new IllegalArgumentException("Illegal size for FITS heap: " + size);
83          }
84  
85          ByteArrayIO data = new ByteArrayIO(Math.max(size, MIN_HEAP_CAPACITY));
86          data.setLength(Math.max(0, size));
87          setData(data);
88          encoder = new FitsEncoder(store);
89          decoder = new FitsDecoder(store);
90      }
91  
92      /**
93       * Sets the underlying data storage for this heap instance. Constructors should call this.
94       *
95       * @param data the new underlying storage object for this heap instance.
96       */
97      protected synchronized void setData(ByteArrayIO data) {
98          store = data;
99      }
100 
101     /**
102      * Add a copy constructor to allow us to duplicate a heap. This would be necessary if we wanted to copy an HDU that
103      * included variable length columns.
104      */
105     synchronized FitsHeap copy() {
106         FitsHeap copy = new FitsHeap();
107         synchronized (copy) {
108             copy.setData(store.copy());
109             copy.encoder = new FitsEncoder(copy.store);
110             copy.decoder = new FitsDecoder(copy.store);
111         }
112         return copy;
113     }
114 
115     /**
116      * Gets data for a Java array from the heap. The array may be a multi-dimensional array of arrays.
117      *
118      * @param  offset        the heap byte offset at which the data begins.
119      * @param  array         The array of primitives to be extracted.
120      *
121      * @throws FitsException if the operation failed
122      */
123     public synchronized void getData(int offset, Object array) throws FitsException {
124         try {
125             store.position(offset);
126             decoder.readArrayFully(array);
127         } catch (Exception e) {
128             throw new FitsException("Error decoding heap area at offset=" + offset + ", size="
129                     + FitsEncoder.computeSize(array) + " (heap size " + size() + "): " + e.getMessage(), e);
130         }
131     }
132 
133     @Override
134     public long getFileOffset() {
135         throw new IllegalStateException("FitsHeap should only be reset from inside its parent, never alone");
136     }
137 
138     @Override
139     public synchronized long getSize() {
140         return size();
141     }
142 
143     /**
144      * Puts data to the end of the heap.
145      * 
146      * @param  data a primitive array object, which may be multidimensional
147      * 
148      * @return      the number of bytes used by the data.
149      * 
150      * @see         #putData(Object, long)
151      * @see         #getData(int, Object)
152      */
153     synchronized long putData(Object data) throws FitsException {
154         return putData(data, store.length());
155     }
156 
157     /**
158      * Puts data onto the heap at a specific heap position.
159      * 
160      * @param  data a primitive array object, which may be multidimensional
161      * @param  pos  the byte offset at which the data should begin.
162      * 
163      * @return      the number of bytes used by the data.
164      * 
165      * @see         #putData(Object, long)
166      * @see         #getData(int, Object)
167      */
168     synchronized long putData(Object data, long pos) throws FitsException {
169         long lsize = pos + FitsEncoder.computeSize(data);
170         if (lsize > Integer.MAX_VALUE) {
171             throw new FitsException("FITS Heap > 2 G");
172         }
173 
174         try {
175             store.position(pos);
176             encoder.writeArray(data);
177         } catch (Exception e) {
178             throw new FitsException("Unable to write variable column length data: " + e.getMessage(), e);
179         }
180 
181         return store.position() - pos;
182     }
183 
184     /**
185      * Copies a segment of data from another heap to the end of this heap
186      * 
187      * @param  src    the heap to source data from
188      * @param  offset the byte offset of the data in the source heap
189      * @param  len    the number of bytes to copy
190      * 
191      * @return        the position of the copied data in this heap.
192      */
193     synchronized int copyFrom(FitsHeap src, int offset, int len) {
194         int pos = (int) store.length();
195         store.setLength(pos + len);
196         synchronized (src) {
197             System.arraycopy(src.store.getBuffer(), offset, store.getBuffer(), pos, len);
198         }
199         return pos;
200     }
201 
202     @SuppressFBWarnings(value = "RR_NOT_CHECKED", justification = "this read will never return less than the requested length")
203     @Override
204     public synchronized void read(ArrayDataInput str) throws FitsException {
205         if (store.length() == 0) {
206             return;
207         }
208 
209         try {
210             str.readFully(store.getBuffer(), 0, (int) store.length());
211         } catch (IOException e) {
212             throw new FitsException("Error reading heap " + e.getMessage(), e);
213         }
214     }
215 
216     @Override
217     public boolean reset() {
218         throw new IllegalStateException("FitsHeap should only be reset from inside its parent, never alone");
219     }
220 
221     @Override
222     public void rewrite() throws IOException, FitsException {
223         throw new FitsException("FitsHeap should only be rewritten from inside its parent, never alone");
224     }
225 
226     @Override
227     public boolean rewriteable() {
228         return false;
229     }
230 
231     /**
232      * Returns the current heap size.
233      *
234      * @return the size of the heap in bytes
235      */
236     public synchronized int size() {
237         return (int) store.length();
238     }
239 
240     @Override
241     public synchronized void write(ArrayDataOutput str) throws FitsException {
242         try {
243             str.write(store.getBuffer(), 0, (int) store.length());
244         } catch (IOException e) {
245             throw new FitsException("Error writing heap:" + e.getMessage(), e);
246         }
247     }
248 
249 }