1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package nom.tam.util;
33
34 import java.io.EOFException;
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.logging.Level;
38 import java.util.logging.Logger;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 @SuppressWarnings("deprecation")
74 public class FitsFile extends ArrayDataFile implements FitsOutput, RandomAccess {
75
76 private static final Logger LOG = Logger.getLogger(FitsFile.class.getName());
77
78
79
80
81 private long marker;
82
83
84
85
86
87
88
89
90 public FitsFile(File file) throws IOException {
91 this(file, "r", FitsIO.DEFAULT_BUFFER_SIZE);
92 }
93
94
95
96
97
98
99
100
101
102 public FitsFile(File file, String mode) throws IOException {
103 this(file, mode, FitsIO.DEFAULT_BUFFER_SIZE);
104 }
105
106
107
108
109
110
111
112
113
114
115 public FitsFile(File file, String mode, int bufferSize) throws IOException {
116 super(file, mode, bufferSize);
117 setDecoder(new FitsDecoder(this));
118 setEncoder(new FitsEncoder(this));
119 }
120
121
122
123
124
125
126
127
128
129 public FitsFile(RandomAccessFileIO src, int bufferSize) throws IOException {
130 super(src, bufferSize);
131 setDecoder(new FitsDecoder(this));
132 setEncoder(new FitsEncoder(this));
133 }
134
135
136
137
138
139
140
141
142 public FitsFile(String filename) throws IOException {
143 this(filename, "r", FitsIO.DEFAULT_BUFFER_SIZE);
144 }
145
146
147
148
149
150
151
152
153
154 public FitsFile(String filename, String mode) throws IOException {
155 this(filename, mode, FitsIO.DEFAULT_BUFFER_SIZE);
156 }
157
158
159
160
161
162
163
164
165
166
167
168 public FitsFile(String filename, String mode, int bufferSize) throws IOException {
169 this(new File(filename), mode, bufferSize);
170 }
171
172 @Override
173 public FitsEncoder getEncoder() {
174 return (FitsEncoder) super.getEncoder();
175 }
176
177 @Override
178 public FitsDecoder getDecoder() {
179 return (FitsDecoder) super.getDecoder();
180 }
181
182 @Override
183 public boolean isAtStart() {
184 return getFilePointer() == 0;
185 }
186
187 @Override
188 public final int readUnsignedByte() throws IOException {
189 return getDecoder().readUnsignedByte();
190 }
191
192 @Override
193 public final byte readByte() throws IOException {
194 return getDecoder().readByte();
195 }
196
197 @Override
198 public boolean readBoolean() throws IOException {
199 return getDecoder().readBoolean();
200 }
201
202 @Override
203 public char readChar() throws IOException {
204 return getDecoder().readChar();
205 }
206
207 @Override
208 public final int readUnsignedShort() throws IOException {
209 return getDecoder().readUnsignedShort();
210 }
211
212 @Override
213 public final short readShort() throws IOException {
214 return getDecoder().readShort();
215 }
216
217 @Override
218 public final int readInt() throws IOException {
219 return getDecoder().readInt();
220 }
221
222 @Override
223 public final long readLong() throws IOException {
224 return getDecoder().readLong();
225 }
226
227 @Override
228 public final float readFloat() throws IOException {
229 return getDecoder().readFloat();
230 }
231
232 @Override
233 public final double readDouble() throws IOException {
234 return getDecoder().readDouble();
235 }
236
237 @Override
238 public String readLine() throws IOException {
239 return getDecoder().readAsciiLine();
240 }
241
242 @Override
243 public int read(boolean[] b, int start, int length) throws IOException {
244 return getDecoder().read(b, start, length);
245 }
246
247 @Override
248 public int read(Boolean[] buf, int offset, int size) throws IOException {
249 return getDecoder().read(buf, offset, size);
250 }
251
252 @Override
253 public int read(char[] c, int start, int length) throws IOException {
254 return getDecoder().read(c, start, length);
255 }
256
257 @Override
258 public int read(short[] s, int start, int length) throws IOException {
259 return getDecoder().read(s, start, length);
260 }
261
262 @Override
263 public int read(int[] i, int start, int length) throws IOException {
264 return getDecoder().read(i, start, length);
265 }
266
267 @Override
268 public int read(long[] l, int start, int length) throws IOException {
269 return getDecoder().read(l, start, length);
270 }
271
272 @Override
273 public int read(float[] f, int start, int length) throws IOException {
274 return getDecoder().read(f, start, length);
275 }
276
277 @Override
278 public int read(double[] d, int start, int length) throws IOException {
279 return getDecoder().read(d, start, length);
280 }
281
282 @Deprecated
283 @Override
284 public final int readArray(Object o) throws IOException {
285 return (int) readLArray(o);
286 }
287
288 @Override
289 public boolean markSupported() {
290 return true;
291 }
292
293 @Override
294 public void mark(int readlimit) throws IOException {
295 marker = getFilePointer();
296 if (!hasAvailable(readlimit)) {
297 FitsFile.LOG.log(Level.FINE, "mark over file limit, so read as far as possible.");
298 }
299 }
300
301 @Override
302 public void reset() throws IOException {
303 seek(marker);
304 }
305
306 @Override
307 public int skipBytes(int toSkip) throws IOException {
308 return (int) skip(toSkip);
309 }
310
311 @Override
312 public void skipAllBytes(long toSkip) throws EOFException, IOException {
313 long n = skip(toSkip);
314
315
316 if (n != toSkip) {
317 throw new EOFException("Skip reached file boundary at " + n + " of " + toSkip);
318 }
319 }
320
321 @Override
322 public final void writeByte(int v) throws IOException {
323 getEncoder().writeByte(v);
324 }
325
326 @Override
327 public void writeBoolean(boolean v) throws IOException {
328 getEncoder().writeBoolean(v);
329 }
330
331 @Override
332 public void writeChar(int v) throws IOException {
333 getEncoder().writeChar(v);
334 }
335
336 @Override
337 public final void writeShort(int v) throws IOException {
338 getEncoder().writeShort(v);
339 }
340
341 @Override
342 public final void writeInt(int v) throws IOException {
343 getEncoder().writeInt(v);
344 }
345
346 @Override
347 public final void writeLong(long v) throws IOException {
348 getEncoder().writeLong(v);
349 }
350
351 @Override
352 public final void writeFloat(float v) throws IOException {
353 getEncoder().writeFloat(v);
354 }
355
356 @Override
357 public final void writeDouble(double v) throws IOException {
358 getEncoder().writeDouble(v);
359 }
360
361 @Override
362 public final void writeBytes(String s) throws IOException {
363 getEncoder().writeBytes(s);
364 }
365
366 @Override
367 public final void writeChars(String s) throws IOException {
368 getEncoder().writeChars(s);
369 }
370
371 @Override
372 public void write(boolean[] b, int start, int length) throws IOException {
373 getEncoder().write(b, start, length);
374 }
375
376 @Override
377 public void write(Boolean[] buf, int offset, int size) throws IOException {
378 getEncoder().write(buf, offset, size);
379 }
380
381 @Override
382 public void write(char[] c, int start, int length) throws IOException {
383 getEncoder().write(c, start, length);
384 }
385
386 @Override
387 public void write(short[] s, int start, int length) throws IOException {
388 getEncoder().write(s, start, length);
389 }
390
391 @Override
392 public void write(int[] i, int start, int length) throws IOException {
393 getEncoder().write(i, start, length);
394 }
395
396 @Override
397 public void write(long[] l, int start, int length) throws IOException {
398 getEncoder().write(l, start, length);
399 }
400
401 @Override
402 public void write(float[] f, int start, int length) throws IOException {
403 getEncoder().write(f, start, length);
404 }
405
406 @Override
407 public void write(double[] d, int start, int length) throws IOException {
408 getEncoder().write(d, start, length);
409 }
410
411 @Override
412 public void write(String[] s, int start, int length) throws IOException {
413 getEncoder().write(s, start, length);
414 }
415 }