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
35 import java.io.DataInput;
36 import java.io.DataInputStream;
37 import java.io.EOFException;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.nio.ByteBuffer;
41
42 import nom.tam.fits.FitsFactory;
43 import nom.tam.fits.utilities.FitsCheckSum;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 @SuppressWarnings("deprecation")
64 public class FitsInputStream extends ArrayInputStream implements ArrayDataInput {
65
66
67 private ByteBuffer check;
68
69
70 private long sum;
71
72
73 private DataInput data;
74
75
76
77
78
79
80
81 public FitsInputStream(InputStream i, int bufLength) {
82 super(i, bufLength);
83 data = new DataInputStream(this);
84 check = ByteBuffer.allocate(FitsFactory.FITS_BLOCK_SIZE);
85 check.limit(check.capacity());
86 setDecoder(new FitsDecoder(this));
87 }
88
89
90
91
92
93
94 public FitsInputStream(InputStream i) {
95 this(i, FitsIO.DEFAULT_BUFFER_SIZE);
96 }
97
98 @Override
99 protected FitsDecoder getDecoder() {
100 return (FitsDecoder) super.getDecoder();
101 }
102
103 @Override
104 public int read() throws IOException {
105 int i = super.read();
106 if (i >= 0) {
107 check.put((byte) i);
108 if (check.remaining() <= 0) {
109 aggregate();
110 }
111 }
112 return i;
113 }
114
115 @Override
116 public int read(byte[] b, int from, int len) throws IOException {
117 int n = super.read(b, from, len);
118 for (int i = 0; i < n;) {
119 int l = Math.min(n - i, check.remaining());
120 check.put(b, from + i, l);
121 if (check.remaining() <= 0) {
122 aggregate();
123 }
124 i += l;
125 }
126 return n;
127 }
128
129 private void aggregate() {
130 sum = FitsCheckSum.sumOf(sum, FitsCheckSum.checksum(check));
131 check.position(0);
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146 public final long nextChecksum() {
147 long ret = sum;
148 sum = 0;
149 return ret;
150 }
151
152 @Override
153 public void readFully(byte[] b) throws IOException {
154 readFully(b, 0, b.length);
155 }
156
157 @Override
158 public void readFully(byte[] b, int off, int len) throws IOException {
159 getDecoder().readFully(b, off, len);
160 }
161
162 @Override
163 public int read(boolean[] b, int start, int length) throws IOException {
164 return getDecoder().read(b, start, length);
165 }
166
167 @Override
168 public int read(Boolean[] b, int start, int length) throws IOException {
169 return getDecoder().read(b, start, length);
170 }
171
172 @Override
173 public int read(char[] c, int start, int length) throws IOException {
174 return getDecoder().read(c, start, length);
175 }
176
177 @Override
178 public int read(short[] s, int start, int length) throws IOException {
179 return getDecoder().read(s, start, length);
180 }
181
182 @Override
183 public int read(int[] i, int start, int length) throws IOException {
184 return getDecoder().read(i, start, length);
185 }
186
187 @Override
188 public int read(long[] l, int start, int length) throws IOException {
189 return getDecoder().read(l, start, length);
190 }
191
192 @Override
193 public int read(float[] f, int start, int length) throws IOException {
194 return getDecoder().read(f, start, length);
195 }
196
197 @Override
198 public int read(double[] d, int start, int length) throws IOException {
199 return getDecoder().read(d, start, length);
200 }
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 @Deprecated
216 public final int readPrimitiveArray(Object o) throws IOException {
217 return (int) readLArray(o);
218 }
219
220 @Override
221 public long skip(long n) throws IOException {
222 byte[] b = new byte[FitsFactory.FITS_BLOCK_SIZE];
223
224
225 long skipped = 0;
226 while (skipped < n) {
227 int got = read(b, 0, (int) Math.min(n - skipped, b.length));
228 if (got < 0) {
229 break;
230 }
231 skipped += got;
232 }
233 return skipped;
234 }
235
236 @Override
237 public int skipBytes(int n) throws IOException {
238 return (int) super.skip(n);
239 }
240
241 @Override
242 public void skipAllBytes(long toSkip) throws EOFException, IOException {
243 long got = 0;
244
245 while (got < toSkip) {
246 long n = skip(toSkip - got);
247 if (n <= 0) {
248 break;
249 }
250 got += n;
251 }
252
253 if (got != toSkip) {
254 throw new EOFException("Reached end-of-stream after skipping " + got + " of " + toSkip);
255 }
256 }
257
258 @Override
259 public boolean readBoolean() throws IOException {
260 return getDecoder().readBoolean();
261 }
262
263 @Override
264 public int readUnsignedByte() throws IOException {
265 return getDecoder().readUnsignedByte();
266 }
267
268 @Override
269 public byte readByte() throws IOException {
270 return getDecoder().readByte();
271 }
272
273 @Override
274 public char readChar() throws IOException {
275 return getDecoder().readChar();
276 }
277
278 @Override
279 public int readUnsignedShort() throws IOException {
280 return getDecoder().readUnsignedShort();
281 }
282
283 @Override
284 public short readShort() throws IOException {
285 return getDecoder().readShort();
286 }
287
288 @Override
289 public int readInt() throws IOException {
290 return getDecoder().readInt();
291 }
292
293 @Override
294 public long readLong() throws IOException {
295 return getDecoder().readLong();
296 }
297
298 @Override
299 public float readFloat() throws IOException {
300 return getDecoder().readFloat();
301 }
302
303 @Override
304 public double readDouble() throws IOException {
305 return getDecoder().readDouble();
306 }
307
308 @Override
309 public String readUTF() throws IOException {
310 return data.readUTF();
311 }
312
313 @Override
314 public final String readLine() throws IOException {
315 return getDecoder().readAsciiLine();
316 }
317
318 @Override
319 public String toString() {
320 return super.toString() + "[count=" + count + ",pos=" + pos + "]";
321 }
322
323 }