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 /**
43 * Heap for storing variable-length entries in binary tables. FITS binary tables store variable length arrays on a heap,
44 * following the regular array data. The newer implementation of the heap now provides proper random access to the byte
45 * buffer as of version 1.16.
46 */
47 public class FitsHeap implements FitsElement {
48
49 /** The minimum stoprage size to allocate for the heap, from which it can grow as necessary */
50 private static final int MIN_HEAP_CAPACITY = 16384;
51
52 // TODO
53 // AK: In principle we could use ReadWriteAccess interface as the storage, which can be either an in-memory
54 // array or a buffered file region. The latter could support heaps over 2G, and could reduce memory overhead
55 // for heap access in some future release...
56 /** The underlying storage space of the heap */
57 private ByteArrayIO store;
58
59 /** conversion from Java arrays to FITS binary representation */
60 private FitsEncoder encoder;
61
62 /** conversion from FITS binary representation to Java arrays */
63 private FitsDecoder decoder;
64
65 /**
66 * Construct a new uninitialized FITS heap object.
67 */
68 private FitsHeap() {
69 }
70
71 /**
72 * Creates a heap of a given initial size. The new heap is initialized with 0's, and up to the specified number of
73 * bytes are immediately available for reading (as zeroes). The heap can grow as needed if more data is written into
74 * it.
75 *
76 * @throws IllegalArgumentException if the size argument is negative.
77 */
78 FitsHeap(int size) throws IllegalArgumentException {
79 if (size < 0) {
80 throw new IllegalArgumentException("Illegal size for FITS heap: " + size);
81 }
82
83 ByteArrayIO data = new ByteArrayIO(Math.max(size, MIN_HEAP_CAPACITY));
84 data.setLength(Math.max(0, size));
85 setData(data);
86 encoder = new FitsEncoder(store);
87 decoder = new FitsDecoder(store);
88 }
89
90 /**
91 * Sets the underlying data storage for this heap instance. Constructors should call this.
92 *
93 * @param data the new underlying storage object for this heap instance.
94 */
95 protected synchronized void setData(ByteArrayIO data) {
96 store = data;
97 }
98
99 /**
100 * Add a copy constructor to allow us to duplicate a heap. This would be necessary if we wanted to copy an HDU that
101 * included variable length columns.
102 */
103 synchronized FitsHeap copy() {
104 FitsHeap copy = new FitsHeap();
105 synchronized (copy) {
106 copy.setData(store.copy());
107 copy.encoder = new FitsEncoder(copy.store);
108 copy.decoder = new FitsDecoder(copy.store);
109 }
110 return copy;
111 }
112
113 /**
114 * Gets data for a Java array from the heap. The array may be a multi-dimensional array of arrays.
115 *
116 * @param offset the heap byte offset at which the data begins.
117 * @param array The array of primitives to be extracted.
118 *
119 * @throws FitsException if the operation failed
120 */
121 public synchronized void getData(int offset, Object array) throws FitsException {
122 try {
123 store.position(offset);
124 decoder.readArrayFully(array);
125 } catch (Exception e) {
126 throw new FitsException("Error decoding heap area at offset=" + offset + ", size="
127 + FitsEncoder.computeSize(array) + " (heap size " + size() + "): " + e.getMessage(), e);
128 }
129 }
130
131 @Override
132 public long getFileOffset() {
133 throw new IllegalStateException("FitsHeap should only be reset from inside its parent, never alone");
134 }
135
136 @Override
137 public synchronized long getSize() {
138 return size();
139 }
140
141 /**
142 * Puts data to the end of the heap.
143 *
144 * @param data a primitive array object, which may be multidimensional
145 *
146 * @return the number of bytes used by the data.
147 *
148 * @see #putData(Object, long)
149 * @see #getData(int, Object)
150 */
151 synchronized long putData(Object data) throws FitsException {
152 return putData(data, store.length());
153 }
154
155 /**
156 * Puts data onto the heap at a specific heap position.
157 *
158 * @param data a primitive array object, which may be multidimensional
159 * @param pos the byte offset at which the data should begin.
160 *
161 * @return the number of bytes used by the data.
162 *
163 * @see #putData(Object, long)
164 * @see #getData(int, Object)
165 */
166 synchronized long putData(Object data, long pos) throws FitsException {
167 long lsize = pos + FitsEncoder.computeSize(data);
168 if (lsize > Integer.MAX_VALUE) {
169 throw new FitsException("FITS Heap > 2 G");
170 }
171
172 try {
173 store.position(pos);
174 encoder.writeArray(data);
175 } catch (Exception e) {
176 throw new FitsException("Unable to write variable column length data: " + e.getMessage(), e);
177 }
178
179 return store.position() - pos;
180 }
181
182 /**
183 * Copies a segment of data from another heap to the end of this heap
184 *
185 * @param src the heap to source data from
186 * @param offset the byte offset of the data in the source heap
187 * @param len the number of bytes to copy
188 *
189 * @return the position of the copied data in this heap.
190 */
191 synchronized int copyFrom(FitsHeap src, int offset, int len) {
192 int pos = (int) store.length();
193 store.setLength(pos + len);
194 synchronized (src) {
195 System.arraycopy(src.store.getBuffer(), offset, store.getBuffer(), pos, len);
196 }
197 return pos;
198 }
199
200 @Override
201 public synchronized void read(ArrayDataInput str) throws FitsException {
202 if (store.length() == 0) {
203 return;
204 }
205
206 try {
207 str.readFully(store.getBuffer(), 0, (int) store.length());
208 } catch (IOException e) {
209 throw new FitsException("Error reading heap " + e.getMessage(), e);
210 }
211 }
212
213 @Override
214 public boolean reset() {
215 throw new IllegalStateException("FitsHeap should only be reset from inside its parent, never alone");
216 }
217
218 @Override
219 public void rewrite() throws IOException, FitsException {
220 throw new FitsException("FitsHeap should only be rewritten from inside its parent, never alone");
221 }
222
223 @Override
224 public boolean rewriteable() {
225 return false;
226 }
227
228 /**
229 * Returns the current heap size.
230 *
231 * @return the size of the heap in bytes
232 */
233 public synchronized int size() {
234 return (int) store.length();
235 }
236
237 @Override
238 public synchronized void write(ArrayDataOutput str) throws FitsException {
239 try {
240 str.write(store.getBuffer(), 0, (int) store.length());
241 } catch (IOException e) {
242 throw new FitsException("Error writing heap:" + e.getMessage(), e);
243 }
244 }
245
246 }