Merge from the pain train
[official-gcc.git] / libjava / javax / imageio / stream / ImageInputStreamImpl.java
blobdbe6d1aaab9cef97aab1e37d4faa4e1d84ebbfad
1 /* ImageInputStream.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.imageio.stream;
41 import java.io.DataInputStream;
42 import java.io.EOFException;
43 import java.io.IOException;
44 import java.nio.ByteOrder;
45 import java.util.Stack;
47 /**
48 * @author Michael Koch (konqueror@gmx.de)
50 public abstract class ImageInputStreamImpl implements ImageInputStream
52 private boolean closed;
53 private Stack markStack = new Stack();
55 byte[] buffer = new byte[8];
57 protected int bitOffset;
58 protected ByteOrder byteOrder;
59 protected long flushedPos;
60 protected long streamPos;
62 public ImageInputStreamImpl()
64 // Do nothing here.
67 protected final void checkClosed()
68 throws IOException
70 if (closed)
71 throw new IOException("stream closed");
74 public void close()
75 throws IOException
77 checkClosed();
78 closed = true;
81 protected void finalize()
82 throws Throwable
84 close();
87 public void flush()
88 throws IOException
90 flushBefore(getStreamPosition());
93 public void flushBefore(long position)
94 throws IOException
96 if (position < flushedPos)
97 throw new IndexOutOfBoundsException();
99 if (position > streamPos)
100 throw new IndexOutOfBoundsException();
102 flushedPos = position;
105 public int getBitOffset()
106 throws IOException
108 checkClosed();
109 return bitOffset;
112 public ByteOrder getByteOrder()
114 return byteOrder;
117 public long getFlushedPosition()
119 return flushedPos;
122 public long getStreamPosition()
123 throws IOException
125 checkClosed();
126 return streamPos;
129 public boolean isCached()
131 return false;
134 public boolean isCachedFile()
136 return false;
139 public boolean isCachedMemory()
141 return false;
144 public long length()
146 return -1L;
149 public void mark()
153 markStack.push(new Long(getStreamPosition()));
155 catch (IOException e)
157 // Ignored.
161 public abstract int read()
162 throws IOException;
164 public int read(byte[] data)
165 throws IOException
167 return read(data, 0, data.length);
170 public abstract int read(byte[] data, int offset, int len)
171 throws IOException;
173 public int readBit()
174 throws IOException
176 checkClosed();
178 // Calc new bit offset here, readByte resets it.
179 int newOffset = (bitOffset + 1) & 0x7;
181 byte data = readByte();
183 if (bitOffset != 0)
185 seek(getStreamPosition() - 1);
186 data = (byte) (data >> (8 - newOffset));
189 bitOffset = newOffset;
190 return data & 0x1;
193 public long readBits(int numBits)
194 throws IOException
196 checkClosed();
198 if (numBits < 0 || numBits > 64)
199 throw new IllegalArgumentException();
201 if (numBits == 0)
202 return 0L;
204 long bits = 0L;
206 for (int i = 0; i < numBits; i++)
208 bits <<= 1;
209 bits |= readBit();
212 return bits;
215 public boolean readBoolean()
216 throws IOException
218 byte data = readByte();
219 return data != 0;
222 public byte readByte()
223 throws IOException
225 int data = read();
227 if (data == -1)
228 throw new EOFException();
230 return (byte) data;
233 public void readBytes(IIOByteBuffer buffer, int len)
234 throws IOException
236 int result = read(buffer.getData(), buffer.getOffset(), len);
238 if (result == -1 || result < len)
239 throw new EOFException();
241 buffer.setLength(len);
244 public char readChar()
245 throws IOException
247 return (char) readShort();
250 public double readDouble()
251 throws IOException
253 return (double) readLong();
256 public float readFloat()
257 throws IOException
259 return (float) readInt();
262 public void readFully(byte[] data)
263 throws IOException
265 readFully(data, 0, data.length);
268 public void readFully(byte[] data, int offset, int len)
269 throws IOException
271 for (int i = 0; i < len; ++i)
272 data[offset + i] = readByte();
275 public void readFully(char[] data, int offset, int len)
276 throws IOException
278 for (int i = 0; i < len; ++i)
279 data[offset + i] = readChar();
282 public void readFully(double[] data, int offset, int len)
283 throws IOException
285 for (int i = 0; i < len; ++i)
286 data[offset + i] = readDouble();
289 public void readFully(float[] data, int offset, int len)
290 throws IOException
292 for (int i = 0; i < len; ++i)
293 data[offset + i] = readFloat();
296 public void readFully(int[] data, int offset, int len)
297 throws IOException
299 for (int i = 0; i < len; ++i)
300 data[offset + i] = readInt();
303 public void readFully(long[] data, int offset, int len)
304 throws IOException
306 for (int i = 0; i < len; ++i)
307 data[offset + i] = readLong();
310 public void readFully(short[] data, int offset, int len)
311 throws IOException
313 for (int i = 0; i < len; ++i)
314 data[offset + i] = readShort();
317 public int readInt()
318 throws IOException
320 int result = read(buffer, 0, 4);
322 if (result == -1)
323 throw new EOFException();
325 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
327 return ((buffer[0] & 0xff)
328 + (buffer[1] << 8)
329 + (buffer[2] << 16)
330 + (buffer[3] << 24));
333 return ((buffer[4] << 24)
334 + (buffer[3] << 16)
335 + (buffer[2] << 8)
336 + (buffer[1] & 0xff));
339 public String readLine()
340 throws IOException
342 checkClosed();
344 int c = -1;
345 boolean eol = false;
346 StringBuffer buffer = new StringBuffer();
348 while (!eol && (c = read()) != -1)
350 switch(c)
352 case '\r':
353 // Consume following \n'
354 long oldPosition = getStreamPosition();
355 if (read() != '\n')
356 seek(oldPosition);
357 case '\n':
358 eol = true;
359 break;
360 default:
361 buffer.append((char) c);
362 break;
366 if (c == -1 && buffer.length() == 0)
367 return null;
369 return buffer.toString();
372 public long readLong()
373 throws IOException
375 int result = read(buffer, 0, 8);
377 if (result == -1)
378 throw new EOFException();
380 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
382 return ((buffer[0] & 0xff)
383 + (((buffer[1] & 0xff)) << 8)
384 + (((buffer[2] & 0xff)) << 16)
385 + (((buffer[3] & 0xffL)) << 24)
386 + (((buffer[4] & 0xffL)) << 32)
387 + (((buffer[5] & 0xffL)) << 40)
388 + (((buffer[6] & 0xffL)) << 48)
389 + (((long) buffer[7]) << 56));
392 return ((((long) buffer[7]) << 56)
393 + ((buffer[6] & 0xffL) << 48)
394 + ((buffer[5] & 0xffL) << 40)
395 + ((buffer[4] & 0xffL) << 32)
396 + ((buffer[3] & 0xffL) << 24)
397 + ((buffer[2] & 0xff) << 16)
398 + ((buffer[1] & 0xff) << 8)
399 + (buffer[0] & 0xff));
402 public short readShort()
403 throws IOException
405 int result = read(buffer, 0, 2);
407 if (result == -1)
408 throw new EOFException();
410 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
412 return (short) ((buffer[0] & 0xff)
413 + (buffer[1] << 8));
416 return (short) ((buffer[0] << 8)
417 + (buffer[1] & 0xff));
420 public int readUnsignedByte()
421 throws IOException
423 return readByte() & 0xff;
426 public long readUnsignedInt()
427 throws IOException
429 return readInt() & 0xffffffff;
432 public int readUnsignedShort()
433 throws IOException
435 return readShort() & 0xffff;
438 public String readUTF()
439 throws IOException
441 checkClosed();
443 String data;
444 ByteOrder old = getByteOrder();
445 setByteOrder(ByteOrder.BIG_ENDIAN); // Strings are always big endian.
449 data = DataInputStream.readUTF(this);
451 finally
453 setByteOrder(old);
456 return data;
459 public void reset()
460 throws IOException
462 checkClosed();
464 long mark = ((Long) markStack.pop()).longValue();
465 seek(mark);
468 public void seek(long position)
469 throws IOException
471 checkClosed();
473 if (position < getFlushedPosition())
474 throw new IndexOutOfBoundsException("position < flushed position");
476 streamPos = position;
477 bitOffset = 0;
480 public void setBitOffset (int bitOffset)
481 throws IOException
483 checkClosed();
485 if (bitOffset < 0 || bitOffset > 7)
486 throw new IllegalArgumentException();
488 this.bitOffset = bitOffset;
491 public void setByteOrder(ByteOrder byteOrder)
493 this.byteOrder = byteOrder;
496 public int skipBytes(int num)
497 throws IOException
499 checkClosed();
501 seek(getStreamPosition() + num);
502 bitOffset = 0;
503 return num;
506 public long skipBytes(long num)
507 throws IOException
509 checkClosed();
511 seek(getStreamPosition() + num);
512 bitOffset = 0;
513 return num;