ImageInputStreamImpl.java revision 0
0N/A * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * An abstract class implementing the <code>ImageInputStream</code> interface. 0N/A * This class is designed to reduce the number of methods that must 0N/A * be implemented by subclasses. 0N/A * <p> In particular, this class handles most or all of the details of 0N/A * byte order interpretation, buffering, mark/reset, discarding, 0N/A * closing, and disposing. 0N/A // Length of the buffer used for readFully(type[], int, int) 0N/A * Byte buffer used for readFully(type[], int, int). Note that this 0N/A * array is also used for bulk reads in readShort(), readInt(), etc, so 0N/A * it should be large enough to hold a primitive value (i.e. >= 8 bytes). 0N/A * Also note that this array is package protected, so that it can be 0N/A * used by ImageOutputStreamImpl in a similar manner. 0N/A * The byte order of the stream as an instance of the enumeration 0N/A * class <code>java.nio.ByteOrder</code>, where 0N/A * <code>ByteOrder.BIG_ENDIAN</code> indicates network byte order 0N/A * and <code>ByteOrder.LITTLE_ENDIAN</code> indicates the reverse 0N/A * order. By default, the value is 0N/A * <code>ByteOrder.BIG_ENDIAN</code>. 0N/A * The current read position within the stream. Subclasses are 0N/A * responsible for keeping this value current from any method they 0N/A * override that alters the read position. 0N/A * The current bit offset within the stream. Subclasses are 0N/A * responsible for keeping this value current from any method they 0N/A * override that alters the bit offset. 0N/A * The position prior to which data may be discarded. Seeking 0N/A * to a smaller position is not allowed. <code>flushedPos</code> 0N/A * will always be >= 0. 0N/A * Constructs an <code>ImageInputStreamImpl</code>. 0N/A * Throws an <code>IOException</code> if the stream has been closed. 0N/A * Subclasses may call this method from any of their methods that 0N/A * require the stream not to be closed. 0N/A * @exception IOException if the stream is closed. 0N/A * Reads a single byte from the stream and returns it as an 0N/A * <code>int</code> between 0 and 255. If EOF is reached, 0N/A * <code>-1</code> is returned. 0N/A * <p> Subclasses must provide an implementation for this method. 0N/A * The subclass implementation should update the stream position 0N/A * <p> The bit offset within the stream must be reset to zero before 0N/A * @return the value of the next byte in the stream, or <code>-1</code> 0N/A * if EOF is reached. 0N/A * @exception IOException if the stream has been closed. 0N/A * A convenience method that calls <code>read(b, 0, b.length)</code>. 0N/A * <p> The bit offset within the stream is reset to zero before 0N/A * @return the number of bytes actually read, or <code>-1</code> 0N/A * @exception NullPointerException if <code>b</code> is 0N/A * <code>null</code>. 0N/A * @exception IOException if an I/O error occurs. 0N/A * Reads up to <code>len</code> bytes from the stream, and stores 0N/A * them into <code>b</code> starting at index <code>off</code>. 0N/A * If no bytes can be read because the end of the stream has been 0N/A * reached, <code>-1</code> is returned. 0N/A * <p> The bit offset within the stream must be reset to zero before 0N/A * <p> Subclasses must provide an implementation for this method. 0N/A * The subclass implementation should update the stream position 0N/A * @param b an array of bytes to be written to. 0N/A * @param off the starting position within <code>b</code> to write to. 0N/A * @param len the maximum number of bytes to read. 0N/A * @return the number of bytes actually read, or <code>-1</code> 0N/A * @exception IndexOutOfBoundsException if <code>off</code> is 0N/A * negative, <code>len</code> is negative, or <code>off + 0N/A * len</code> is greater than <code>b.length</code>. 0N/A * @exception NullPointerException if <code>b</code> is 0N/A * <code>null</code>. 0N/A * @exception IOException if an I/O error occurs. 0N/A // REMIND: Once 6277756 is fixed, we should do a bulk read of all 8 0N/A // bytes here as we do in readShort() and readInt() for even better 0N/A // performance (see 6347575 for details). 0N/A return ((
long)
i1 <<
32) + (
i2 &
0xFFFFFFFFL);
0N/A return ((
long)
i2 <<
32) + (
i1 &
0xFFFFFFFFL);
0N/A // Fix 4494369: method ImageInputStreamImpl.readUTF() 0N/A // does not work as specified (it should always assume 0N/A // network byte order). 0N/A // Restore the old byte order even if an exception occurs 0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > b.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > s.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > c.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > i.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > l.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > f.length!");
0N/A // Fix 4430357 - if off + len < 0, overflow occurred 0N/A (
"off < 0 || len < 0 || off + len > d.length!");
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A l[
off + j] = ((
long)
i0 <<
32) | (
i1 &
0xffffffffL);
0N/A for (
int j =
0; j <
len; j++) {
0N/A l[
off + j] = ((
long)
i0 <<
32) | (
i1 &
0xffffffffL);
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A for (
int j =
0; j <
len; j++) {
0N/A long l = ((
long)
i0 <<
32) | (
i1 &
0xffffffffL);
0N/A for (
int j =
0; j <
len; j++) {
0N/A long l = ((
long)
i0 <<
32) | (
i1 &
0xffffffffL);
0N/A // Compute final bit offset before we call read() and seek() 0N/A // Move byte position back if in the middle of a byte 0N/A // Shift the bit to be read to the rightmost position 0N/A // Have to read additional bits on the left equal to the bit offset 0N/A // Compute final bit offset before we call read() and seek() 0N/A // Read a byte at a time, accumulate 0N/A // Move byte position back if in the middle of a byte 0N/A // Shift away unwanted bits on the right. 0N/A // Mask out unwanted bits on the left 0N/A * Returns <code>-1L</code> to indicate that the stream has unknown 0N/A * length. Subclasses must override this method to provide actual 0N/A * length information. 0N/A * @return -1L to indicate unknown length. 0N/A * Advances the current stream position by calling 0N/A * <code>seek(getStreamPosition() + n)</code>. 0N/A * <p> The bit offset is reset to zero. 0N/A * @param n the number of bytes to seek forward. 0N/A * @return an <code>int</code> representing the number of bytes 0N/A * @exception IOException if <code>getStreamPosition</code> 0N/A * throws an <code>IOException</code> when computing either 0N/A * the starting or ending position. 0N/A * Advances the current stream position by calling 0N/A * <code>seek(getStreamPosition() + n)</code>. 0N/A * <p> The bit offset is reset to zero. 0N/A * @param n the number of bytes to seek forward. 0N/A * @return a <code>long</code> representing the number of bytes 0N/A * @exception IOException if <code>getStreamPosition</code> 0N/A * throws an <code>IOException</code> when computing either 0N/A * the starting or ending position. 0N/A // This test also covers pos < 0 0N/A * Pushes the current stream position onto a stack of marked 0N/A * Resets the current stream byte and bit positions from the stack 0N/A * of marked positions. 0N/A * <p> An <code>IOException</code> will be thrown if the previous 0N/A * marked position lies in the discarded portion of the stream. 0N/A * @exception IOException if an I/O error occurs. 0N/A (
"Previous marked position has been discarded!");
0N/A // Invariant: flushedPos >= 0 0N/A * Default implementation returns false. Subclasses should 0N/A * override this if they cache data. 0N/A * Default implementation returns false. Subclasses should 0N/A * override this if they cache data in main memory. 0N/A * Default implementation returns false. Subclasses should 0N/A * override this if they cache data in a temporary file. 0N/A * Finalizes this object prior to garbage collection. The 0N/A * <code>close</code> method is called to close any open input 0N/A * source. This method should not be called from application 0N/A * @exception Throwable if an error occurs during superclass