Merge with trank @ 137446
[official-gcc.git] / libjava / classpath / gnu / javax / imageio / jpeg / JPEGImageInputStream.java
blob29a12f346cfe843072b282ca29c81b8bd3f7bd35
1 /* JPEGImageInputStream.java --
2 Copyright (C) 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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. */
38 package gnu.javax.imageio.jpeg;
40 import java.io.IOException;
41 import javax.imageio.stream.ImageInputStream;
42 import javax.imageio.stream.ImageInputStreamImpl;
44 public class JPEGImageInputStream
45 extends ImageInputStreamImpl
47 private ImageInputStream in;
49 byte marker;
51 public JPEGImageInputStream(ImageInputStream in)
53 super();
55 this.in = in;
58 public int read()
59 throws IOException
61 setBitOffset(0);
62 return in.read();
65 public int read(byte[] data, int offset, int len)
66 throws IOException
68 setBitOffset(0);
69 return in.read(data, offset, len);
72 /**
73 * Pull a byte from the stream, this checks to see if the byte is 0xff
74 * and if the next byte isn't 0x00 (stuffed byte) it errors out. If it's
75 * 0x00 then it simply ignores the byte.
77 * @return the next byte in the buffer
79 * @throws IOException TODO
80 * @throws BitStreamException TODO
82 private byte pullByte() throws IOException, JPEGMarkerFoundException
84 byte mybyte = readByte();
85 // FIXME: handle multiple 0xff in a row
86 if(mybyte==(byte)(0xff))
88 byte secondbyte = readByte();
89 if(secondbyte != (byte)(0x00))
91 marker = secondbyte;
92 throw new JPEGMarkerFoundException();
95 return mybyte;
98 /**
99 * This returns the marker that was last encountered. This should only be
100 * used if removeBit() throws a MarkerTagFound exception.
102 * @return marker as byte
104 public byte getMarker()
106 return marker;
110 * Removes a bit from the buffer. (Removes from the top of a queue). This
111 * also checks for markers and throws MarkerTagFound exception if it does.
112 * If MarkerTagFound is thrown you can use getMarker() method to get the
113 * marker that caused the throw.
115 * @param l specifies how many bits you want to remove and add to the
116 * integer
117 * @return the amount of bits specified by l as an integer
119 * @throws IOException TODO
120 * @throws JPEGMarkerFoundException
121 * @throws BitStreamException TODO
123 public int readBit()
124 throws IOException, JPEGMarkerFoundException
126 checkClosed();
128 // Calc new bit offset here, readByte resets it.
129 int newOffset = (bitOffset + 1) & 0x7;
131 byte data = pullByte();
133 if (bitOffset != 0)
135 seek(getStreamPosition() - 1);
136 data = (byte) (data >> (8 - newOffset));
139 bitOffset = newOffset;
140 return data & 0x1;
145 * This method skips over the the data and finds the next position
146 * in the bit sequence with a X'FF' X'??' sequence. Multiple X'FF
147 * bytes in sequence are considered padding and interpreted as one
148 * X'FF byte.
150 * @return the next marker byte in the stream
151 * @throws IOException if the end of the stream is reached
152 * unexpectedly
154 public byte findNextMarker()
155 throws IOException
157 boolean marked0xff = false;
158 byte byteinfo = JPEGMarker.X00;
160 setBitOffset(0);
161 while (true)
163 byteinfo = readByte();
164 if (!marked0xff)
166 if (byteinfo == JPEGMarker.XFF)
167 marked0xff = true;
169 else
171 if (byteinfo == JPEGMarker.XFF)
172 // Ignore the value 0xff when it is immediately
173 // followed by another 0xff byte.
174 continue;
175 else if (byteinfo == JPEGMarker.X00)
176 // The sequence 0xff 0x00 is used to encode the
177 // actual value 0xff. So restart our search for a
178 // marker.
179 marked0xff = false;
180 else
181 // One or more 0xff values were follwed by a
182 // non-0x00, non-0xff value so return this as the
183 // marker byte.
184 return byteinfo;