blob: 83ac13a9c986d60b492071fec438f01fb3372b58 [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/**
18 * @author Rustem V. Rafikov
19 * @version $Revision: 1.3 $
20 */
21package javax.imageio.stream;
22
23import java.io.EOFException;
24import java.io.IOException;
25import java.nio.ByteOrder;
26
27/**
28 * The ImageInputStreamImpl abstract class implements
29 * the ImageInputStream interface.
30 */
31public abstract class ImageInputStreamImpl implements ImageInputStream {
32
33 /** The byte order. */
34 protected ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
35
36 /** The stream position. */
37 protected long streamPos = 0;
38
39 /** The flushed position. */
40 protected long flushedPos = 0;
41
42 /** The bit offset. */
43 protected int bitOffset = 0;
44
45 /** The closed. */
46 private boolean closed = false;
47
48 /** The position stack. */
49 private final PositionStack posStack = new PositionStack();
50
51 /**
52 * Instantiates a new ImageInputStreamImpl.
53 */
54 public ImageInputStreamImpl() {}
55
56 /**
57 * Check if the stream is closed and if true, throws an IOException.
58 *
59 * @throws IOException Signals that the stream is closed.
60 */
61 protected final void checkClosed() throws IOException {
62 if (closed) {
63 throw new IOException("stream is closed");
64 }
65 }
66
67 public void setByteOrder(ByteOrder byteOrder) {
68 this.byteOrder = byteOrder;
69 }
70
71 public ByteOrder getByteOrder() {
72 return byteOrder;
73 }
74
75 public abstract int read() throws IOException;
76
77 public int read(byte[] b) throws IOException {
78 return read(b, 0, b.length);
79 }
80
81 public abstract int read(byte[] b, int off, int len) throws IOException;
82
83 public void readBytes(IIOByteBuffer buf, int len) throws IOException {
84 if (buf == null) {
85 throw new NullPointerException("buffer is NULL");
86 }
87
88 byte[] b = new byte[len];
89 len = read(b, 0, b.length);
90
91 buf.setData(b);
92 buf.setOffset(0);
93 buf.setLength(len);
94 }
95
96 public boolean readBoolean() throws IOException {
97 int b = read();
98 if (b < 0) {
99 throw new EOFException("EOF reached");
100 }
101 return b != 0;
102 }
103
104 public byte readByte() throws IOException {
105 int b = read();
106 if (b < 0) {
107 throw new EOFException("EOF reached");
108 }
109 return (byte) b;
110 }
111
112 public int readUnsignedByte() throws IOException {
113 int b = read();
114 if (b < 0) {
115 throw new EOFException("EOF reached");
116 }
117 return b;
118 }
119
120 public short readShort() throws IOException {
121 int b1 = read();
122 int b2 = read();
123
124 if (b1 < 0 || b2 < 0) {
125 throw new EOFException("EOF reached");
126 }
127
128 return byteOrder == ByteOrder.BIG_ENDIAN ?
129 (short) ((b1 << 8) | (b2 & 0xff)) :
130 (short) ((b2 << 8) | (b1 & 0xff));
131 }
132
133 public int readUnsignedShort() throws IOException {
134 //-- TODO implement
135 throw new UnsupportedOperationException("Not implemented yet");
136 }
137
138 public char readChar() throws IOException {
139 //-- TODO implement
140 throw new UnsupportedOperationException("Not implemented yet");
141 }
142
143 public int readInt() throws IOException {
144 //-- TODO implement
145 throw new UnsupportedOperationException("Not implemented yet");
146 }
147
148 public long readUnsignedInt() throws IOException {
149 //-- TODO implement
150 throw new UnsupportedOperationException("Not implemented yet");
151 }
152
153 public long readLong() throws IOException {
154 //-- TODO implement
155 throw new UnsupportedOperationException("Not implemented yet");
156 }
157
158 public float readFloat() throws IOException {
159 //-- TODO implement
160 throw new UnsupportedOperationException("Not implemented yet");
161 }
162
163 public double readDouble() throws IOException {
164 //-- TODO implement
165 throw new UnsupportedOperationException("Not implemented yet");
166 }
167
168 public String readLine() throws IOException {
169 //-- TODO implement
170 throw new UnsupportedOperationException("Not implemented yet");
171 }
172
173 public String readUTF() throws IOException {
174 //-- TODO implement
175 throw new UnsupportedOperationException("Not implemented yet");
176 }
177
178 public void readFully(byte[] b, int off, int len) throws IOException {
179 //-- TODO implement
180 throw new UnsupportedOperationException("Not implemented yet");
181 }
182
183 public void readFully(byte[] b) throws IOException {
184 readFully(b, 0, b.length);
185 }
186
187 public void readFully(short[] s, int off, int len) throws IOException {
188 //-- TODO implement
189 throw new UnsupportedOperationException("Not implemented yet");
190 }
191
192 public void readFully(char[] c, int off, int len) throws IOException {
193 //-- TODO implement
194 throw new UnsupportedOperationException("Not implemented yet");
195 }
196
197 public void readFully(int[] i, int off, int len) throws IOException {
198 //-- TODO implement
199 throw new UnsupportedOperationException("Not implemented yet");
200 }
201
202 public void readFully(long[] l, int off, int len) throws IOException {
203 //-- TODO implement
204 throw new UnsupportedOperationException("Not implemented yet");
205 }
206
207 public void readFully(float[] f, int off, int len) throws IOException {
208 //-- TODO implement
209 throw new UnsupportedOperationException("Not implemented yet");
210 }
211
212 public void readFully(double[] d, int off, int len) throws IOException {
213 //-- TODO implement
214 throw new UnsupportedOperationException("Not implemented yet");
215 }
216
217 public long getStreamPosition() throws IOException {
218 checkClosed();
219 return streamPos;
220 }
221
222 public int getBitOffset() throws IOException {
223 checkClosed();
224 return bitOffset;
225 }
226
227 public void setBitOffset(int bitOffset) throws IOException {
228 checkClosed();
229 this.bitOffset = bitOffset;
230 }
231
232 public int readBit() throws IOException {
233 //-- TODO implement
234 throw new UnsupportedOperationException("Not implemented yet");
235 }
236
237 public long readBits(int numBits) throws IOException {
238 //-- TODO implement
239 throw new UnsupportedOperationException("Not implemented yet");
240 }
241
242 public long length() {
243 return -1L;
244 }
245
246 public int skipBytes(int n) throws IOException {
247 //-- TODO implement
248 throw new UnsupportedOperationException("Not implemented yet");
249 }
250
251 public long skipBytes(long n) throws IOException {
252 //-- TODO implement
253 throw new UnsupportedOperationException("Not implemented yet");
254 }
255
256 public void seek(long pos) throws IOException {
257 checkClosed();
258 if (pos < getFlushedPosition()) {
259 throw new IllegalArgumentException("trying to seek before flushed pos");
260 }
261 bitOffset = 0;
262 streamPos = pos;
263 }
264
265 public void mark() {
266 try {
267 posStack.push(getStreamPosition());
268 } catch (IOException e) {
269 e.printStackTrace();
270 throw new RuntimeException("Stream marking error");
271 }
272 }
273
274 public void reset() throws IOException {
275 //-- TODO bit pos
276 if (!posStack.isEmpty()) {
277 long p = posStack.pop();
278 if (p < flushedPos) {
279 throw new IOException("marked position lies in the flushed portion of the stream");
280 }
281 seek(p);
282 }
283 }
284
285 public void flushBefore(long pos) throws IOException {
286 if (pos > getStreamPosition()) {
287 throw new IndexOutOfBoundsException("Trying to flush outside of current position");
288 }
289 if (pos < flushedPos) {
290 throw new IndexOutOfBoundsException("Trying to flush within already flushed portion");
291 }
292 flushedPos = pos;
293 //-- TODO implement
294 }
295
296 public void flush() throws IOException {
297 flushBefore(getStreamPosition());
298 }
299
300 public long getFlushedPosition() {
301 return flushedPos;
302 }
303
304 public boolean isCached() {
305 return false; //def
306 }
307
308 public boolean isCachedMemory() {
309 return false; //def
310 }
311
312 public boolean isCachedFile() {
313 return false; //def
314 }
315
316 public void close() throws IOException {
317 checkClosed();
318 closed = true;
319
320 }
321
322 /**
323 * Finalizes this object.
324 *
325 * @throws Throwable if an error occurs.
326 */
327 @Override
328 protected void finalize() throws Throwable {
329 if (!closed) {
330 try {
331 close();
332 } finally {
333 super.finalize();
334 }
335 }
336 }
337
338 /**
339 * The Class PositionStack.
340 */
341 private static class PositionStack {
342
343 /** The Constant SIZE. */
344 private static final int SIZE = 10;
345
346 /** The values. */
347 private long[] values = new long[SIZE];
348
349 /** The pos. */
350 private int pos = 0;
351
352
353 /**
354 * Push.
355 *
356 * @param v the v
357 */
358 void push(long v) {
359 if (pos >= values.length) {
360 ensure(pos+1);
361 }
362 values[pos++] = v;
363 }
364
365 /**
366 * Pop.
367 *
368 * @return the long
369 */
370 long pop() {
371 return values[--pos];
372 }
373
374 /**
375 * Checks if is empty.
376 *
377 * @return true, if is empty
378 */
379 boolean isEmpty() {
380 return pos == 0;
381 }
382
383 /**
384 * Ensure.
385 *
386 * @param size the size
387 */
388 private void ensure(int size) {
389 long[] arr = new long[Math.max(2 * values.length, size)];
390 System.arraycopy(values, 0, arr, 0, values.length);
391 values = arr;
392 }
393 }
394}