26 package javax.imageio.stream;
27
28 import java.io.DataInputStream;
29 import java.io.EOFException;
30 import java.io.IOException;
31 import java.nio.ByteOrder;
32 import java.util.Stack;
33 import javax.imageio.IIOException;
34
35 /**
36 * An abstract class implementing the <code>ImageInputStream</code> interface.
37 * This class is designed to reduce the number of methods that must
38 * be implemented by subclasses.
39 *
40 * <p> In particular, this class handles most or all of the details of
41 * byte order interpretation, buffering, mark/reset, discarding,
42 * closing, and disposing.
43 */
44 public abstract class ImageInputStreamImpl implements ImageInputStream {
45
46 private Stack markByteStack = new Stack();
47
48 private Stack markBitStack = new Stack();
49
50 private boolean isClosed = false;
51
52 // Length of the buffer used for readFully(type[], int, int)
53 private static final int BYTE_BUF_LENGTH = 8192;
54
55 /**
56 * Byte buffer used for readFully(type[], int, int). Note that this
57 * array is also used for bulk reads in readShort(), readInt(), etc, so
58 * it should be large enough to hold a primitive value (i.e. >= 8 bytes).
59 * Also note that this array is package protected, so that it can be
60 * used by ImageOutputStreamImpl in a similar manner.
61 */
62 byte[] byteBuf = new byte[BYTE_BUF_LENGTH];
63
64 /**
65 * The byte order of the stream as an instance of the enumeration
66 * class <code>java.nio.ByteOrder</code>, where
67 * <code>ByteOrder.BIG_ENDIAN</code> indicates network byte order
68 * and <code>ByteOrder.LITTLE_ENDIAN</code> indicates the reverse
781 markByteStack.push(Long.valueOf(getStreamPosition()));
782 markBitStack.push(Integer.valueOf(getBitOffset()));
783 } catch (IOException e) {
784 }
785 }
786
787 /**
788 * Resets the current stream byte and bit positions from the stack
789 * of marked positions.
790 *
791 * <p> An <code>IOException</code> will be thrown if the previous
792 * marked position lies in the discarded portion of the stream.
793 *
794 * @exception IOException if an I/O error occurs.
795 */
796 public void reset() throws IOException {
797 if (markByteStack.empty()) {
798 return;
799 }
800
801 long pos = ((Long)markByteStack.pop()).longValue();
802 if (pos < flushedPos) {
803 throw new IIOException
804 ("Previous marked position has been discarded!");
805 }
806 seek(pos);
807
808 int offset = ((Integer)markBitStack.pop()).intValue();
809 setBitOffset(offset);
810 }
811
812 public void flushBefore(long pos) throws IOException {
813 checkClosed();
814 if (pos < flushedPos) {
815 throw new IndexOutOfBoundsException("pos < flushedPos!");
816 }
817 if (pos > getStreamPosition()) {
818 throw new IndexOutOfBoundsException("pos > getStreamPosition()!");
819 }
820 // Invariant: flushedPos >= 0
821 flushedPos = pos;
822 }
823
824 public void flush() throws IOException {
825 flushBefore(getStreamPosition());
826 }
827
828 public long getFlushedPosition() {
|
26 package javax.imageio.stream;
27
28 import java.io.DataInputStream;
29 import java.io.EOFException;
30 import java.io.IOException;
31 import java.nio.ByteOrder;
32 import java.util.Stack;
33 import javax.imageio.IIOException;
34
35 /**
36 * An abstract class implementing the <code>ImageInputStream</code> interface.
37 * This class is designed to reduce the number of methods that must
38 * be implemented by subclasses.
39 *
40 * <p> In particular, this class handles most or all of the details of
41 * byte order interpretation, buffering, mark/reset, discarding,
42 * closing, and disposing.
43 */
44 public abstract class ImageInputStreamImpl implements ImageInputStream {
45
46 private Stack<Long> markByteStack = new Stack<>();
47
48 private Stack<Integer> markBitStack = new Stack<>();
49
50 private boolean isClosed = false;
51
52 // Length of the buffer used for readFully(type[], int, int)
53 private static final int BYTE_BUF_LENGTH = 8192;
54
55 /**
56 * Byte buffer used for readFully(type[], int, int). Note that this
57 * array is also used for bulk reads in readShort(), readInt(), etc, so
58 * it should be large enough to hold a primitive value (i.e. >= 8 bytes).
59 * Also note that this array is package protected, so that it can be
60 * used by ImageOutputStreamImpl in a similar manner.
61 */
62 byte[] byteBuf = new byte[BYTE_BUF_LENGTH];
63
64 /**
65 * The byte order of the stream as an instance of the enumeration
66 * class <code>java.nio.ByteOrder</code>, where
67 * <code>ByteOrder.BIG_ENDIAN</code> indicates network byte order
68 * and <code>ByteOrder.LITTLE_ENDIAN</code> indicates the reverse
781 markByteStack.push(Long.valueOf(getStreamPosition()));
782 markBitStack.push(Integer.valueOf(getBitOffset()));
783 } catch (IOException e) {
784 }
785 }
786
787 /**
788 * Resets the current stream byte and bit positions from the stack
789 * of marked positions.
790 *
791 * <p> An <code>IOException</code> will be thrown if the previous
792 * marked position lies in the discarded portion of the stream.
793 *
794 * @exception IOException if an I/O error occurs.
795 */
796 public void reset() throws IOException {
797 if (markByteStack.empty()) {
798 return;
799 }
800
801 long pos = markByteStack.pop().longValue();
802 if (pos < flushedPos) {
803 throw new IIOException
804 ("Previous marked position has been discarded!");
805 }
806 seek(pos);
807
808 int offset = markBitStack.pop().intValue();
809 setBitOffset(offset);
810 }
811
812 public void flushBefore(long pos) throws IOException {
813 checkClosed();
814 if (pos < flushedPos) {
815 throw new IndexOutOfBoundsException("pos < flushedPos!");
816 }
817 if (pos > getStreamPosition()) {
818 throw new IndexOutOfBoundsException("pos > getStreamPosition()!");
819 }
820 // Invariant: flushedPos >= 0
821 flushedPos = pos;
822 }
823
824 public void flush() throws IOException {
825 flushBefore(getStreamPosition());
826 }
827
828 public long getFlushedPosition() {
|