jdk/src/share/classes/javax/imageio/stream/ImageInputStreamImpl.java
author bae
Fri, 08 May 2009 15:57:33 +0400
changeset 3448 1ccef37a150f
parent 715 f16baef3a20e
child 5506 202f599c92aa
permissions -rw-r--r--
6657133: Mutable statics in imageio plugins (findbugs) Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 2000-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.imageio.stream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.EOFException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.imageio.IIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * An abstract class implementing the <code>ImageInputStream</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class is designed to reduce the number of methods that must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * be implemented by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> In particular, this class handles most or all of the details of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * byte order interpretation, buffering, mark/reset, discarding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * closing, and disposing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public abstract class ImageInputStreamImpl implements ImageInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Stack markByteStack = new Stack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private Stack markBitStack = new Stack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private boolean isClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Length of the buffer used for readFully(type[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static final int BYTE_BUF_LENGTH = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Byte buffer used for readFully(type[], int, int).  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * array is also used for bulk reads in readShort(), readInt(), etc, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * it should be large enough to hold a primitive value (i.e. >= 8 bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Also note that this array is package protected, so that it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * used by ImageOutputStreamImpl in a similar manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    byte[] byteBuf = new byte[BYTE_BUF_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The byte order of the stream as an instance of the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * class <code>java.nio.ByteOrder</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <code>ByteOrder.BIG_ENDIAN</code> indicates network byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * and <code>ByteOrder.LITTLE_ENDIAN</code> indicates the reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * order.  By default, the value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * <code>ByteOrder.BIG_ENDIAN</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The current read position within the stream.  Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * responsible for keeping this value current from any method they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * override that alters the read position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected long streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The current bit offset within the stream.  Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * responsible for keeping this value current from any method they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * override that alters the bit offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected int bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The position prior to which data may be discarded.  Seeking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * to a smaller position is not allowed.  <code>flushedPos</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * will always be >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected long flushedPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Constructs an <code>ImageInputStreamImpl</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public ImageInputStreamImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Throws an <code>IOException</code> if the stream has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Subclasses may call this method from any of their methods that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * require the stream not to be closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @exception IOException if the stream is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected final void checkClosed() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throw new IOException("closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public void setByteOrder(ByteOrder byteOrder) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this.byteOrder = byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public ByteOrder getByteOrder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return byteOrder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Reads a single byte from the stream and returns it as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>int</code> between 0 and 255.  If EOF is reached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>-1</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> Subclasses must provide an implementation for this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * The subclass implementation should update the stream position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * before exiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p> The bit offset within the stream must be reset to zero before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the read occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @return the value of the next byte in the stream, or <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * if EOF is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception IOException if the stream has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public abstract int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * A convenience method that calls <code>read(b, 0, b.length)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p> The bit offset within the stream is reset to zero before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the read occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @return the number of bytes actually read, or <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * to indicate EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception NullPointerException if <code>b</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public int read(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Reads up to <code>len</code> bytes from the stream, and stores
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * them into <code>b</code> starting at index <code>off</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * If no bytes can be read because the end of the stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * reached, <code>-1</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p> The bit offset within the stream must be reset to zero before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * the read occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p> Subclasses must provide an implementation for this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * The subclass implementation should update the stream position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * before exiting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param b an array of bytes to be written to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param off the starting position within <code>b</code> to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param len the maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the number of bytes actually read, or <code>-1</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * to indicate EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @exception IndexOutOfBoundsException if <code>off</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * negative, <code>len</code> is negative, or <code>off +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * len</code> is greater than <code>b.length</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception NullPointerException if <code>b</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public abstract int read(byte[] b, int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void readBytes(IIOByteBuffer buf, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            throw new IndexOutOfBoundsException("len < 0!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new NullPointerException("buf == null!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        byte[] data = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        len = read(data, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        buf.setData(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        buf.setOffset(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        buf.setLength(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public boolean readBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return (ch != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public byte readByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return (byte)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int readUnsignedByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (ch < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public short readShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (read(byteBuf, 0, 2) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                (((byteBuf[0] & 0xff) << 8) | ((byteBuf[1] & 0xff) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return (short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                (((byteBuf[1] & 0xff) << 8) | ((byteBuf[0] & 0xff) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return ((int)readShort()) & 0xffff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public char readChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return (char)readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (read(byteBuf, 0, 4) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                (((byteBuf[0] & 0xff) << 24) | ((byteBuf[1] & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                 ((byteBuf[2] & 0xff) <<  8) | ((byteBuf[3] & 0xff) <<  0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                (((byteBuf[3] & 0xff) << 24) | ((byteBuf[2] & 0xff) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                 ((byteBuf[1] & 0xff) <<  8) | ((byteBuf[0] & 0xff) <<  0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public long readUnsignedInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return ((long)readInt()) & 0xffffffffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public long readLong() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // REMIND: Once 6277756 is fixed, we should do a bulk read of all 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // bytes here as we do in readShort() and readInt() for even better
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // performance (see 6347575 for details).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        int i1 = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        int i2 = readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return ((long)i1 << 32) + (i2 & 0xFFFFFFFFL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return ((long)i2 << 32) + (i1 & 0xFFFFFFFFL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public float readFloat() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return Float.intBitsToFloat(readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public double readDouble() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return Double.longBitsToDouble(readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        StringBuffer input = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        boolean eol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        while (!eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            switch (c = read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                long cur = getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                if ((read()) != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    seek(cur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                input.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if ((c == -1) && (input.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return input.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public String readUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        this.bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Fix 4494369: method ImageInputStreamImpl.readUTF()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        // does not work as specified (it should always assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // network byte order).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        ByteOrder oldByteOrder = getByteOrder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        setByteOrder(ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        String ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            ret = DataInputStream.readUTF(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            // Restore the old byte order even if an exception occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            setByteOrder(oldByteOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        setByteOrder(oldByteOrder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void readFully(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (off < 0 || len < 0 || off + len > b.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                ("off < 0 || len < 0 || off + len > b.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            int nbytes = read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (nbytes == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            off += nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            len -= nbytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public void readFully(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        readFully(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public void readFully(short[] s, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (off < 0 || len < 0 || off + len > s.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                ("off < 0 || len < 0 || off + len > s.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            int nelts = Math.min(len, byteBuf.length/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            readFully(byteBuf, 0, nelts*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            toShorts(byteBuf, s, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public void readFully(char[] c, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (off < 0 || len < 0 || off + len > c.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                ("off < 0 || len < 0 || off + len > c.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            int nelts = Math.min(len, byteBuf.length/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            readFully(byteBuf, 0, nelts*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            toChars(byteBuf, c, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public void readFully(int[] i, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (off < 0 || len < 0 || off + len > i.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                ("off < 0 || len < 0 || off + len > i.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            int nelts = Math.min(len, byteBuf.length/4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            readFully(byteBuf, 0, nelts*4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            toInts(byteBuf, i, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public void readFully(long[] l, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (off < 0 || len < 0 || off + len > l.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                ("off < 0 || len < 0 || off + len > l.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            int nelts = Math.min(len, byteBuf.length/8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            readFully(byteBuf, 0, nelts*8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            toLongs(byteBuf, l, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    public void readFully(float[] f, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (off < 0 || len < 0 || off + len > f.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                ("off < 0 || len < 0 || off + len > f.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            int nelts = Math.min(len, byteBuf.length/4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            readFully(byteBuf, 0, nelts*4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            toFloats(byteBuf, f, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public void readFully(double[] d, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // Fix 4430357 - if off + len < 0, overflow occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (off < 0 || len < 0 || off + len > d.length || off + len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                ("off < 0 || len < 0 || off + len > d.length!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int nelts = Math.min(len, byteBuf.length/8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            readFully(byteBuf, 0, nelts*8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            toDoubles(byteBuf, d, off, nelts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            off += nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            len -= nelts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    private void toShorts(byte[] b, short[] s, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                s[off + j] = (short)((b0 << 8) | b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                boff += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                int b0 = b[boff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                int b1 = b[boff] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                s[off + j] = (short)((b0 << 8) | b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                boff += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    private void toChars(byte[] b, char[] c, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                c[off + j] = (char)((b0 << 8) | b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                boff += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                int b0 = b[boff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                int b1 = b[boff] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                c[off + j] = (char)((b0 << 8) | b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                boff += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private void toInts(byte[] b, int[] i, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                int b2 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                int b3 = b[boff + 3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                i[off + j] = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                boff += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                int b0 = b[boff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                int b1 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                int b2 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                int b3 = b[boff] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                i[off + j] = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                boff += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private void toLongs(byte[] b, long[] l, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                int b2 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                int b3 = b[boff + 3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                int b4 = b[boff + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                int b5 = b[boff + 5] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                int b6 = b[boff + 6] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                int b7 = b[boff + 7] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                int i0 = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                int i1 = (b4 << 24) | (b5 << 16) | (b6 << 8) | b7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                l[off + j] = ((long)i0 << 32) | (i1 & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                boff += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                int b0 = b[boff + 7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                int b1 = b[boff + 6] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                int b2 = b[boff + 5] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                int b3 = b[boff + 4] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                int b4 = b[boff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                int b5 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                int b6 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                int b7 = b[boff]     & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                int i0 = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                int i1 = (b4 << 24) | (b5 << 16) | (b6 << 8) | b7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                l[off + j] = ((long)i0 << 32) | (i1 & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                boff += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    private void toFloats(byte[] b, float[] f, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                int b2 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                int b3 = b[boff + 3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                int i = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                f[off + j] = Float.intBitsToFloat(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                boff += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                int b0 = b[boff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                int b1 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                int b2 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                int b3 = b[boff + 0] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                int i = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                f[off + j] = Float.intBitsToFloat(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                boff += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    private void toDoubles(byte[] b, double[] d, int off, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int boff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        if (byteOrder == ByteOrder.BIG_ENDIAN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                int b0 = b[boff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                int b1 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                int b2 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                int b3 = b[boff + 3] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                int b4 = b[boff + 4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                int b5 = b[boff + 5] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                int b6 = b[boff + 6] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                int b7 = b[boff + 7] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                int i0 = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                int i1 = (b4 << 24) | (b5 << 16) | (b6 << 8) | b7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                long l = ((long)i0 << 32) | (i1 & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                d[off + j] = Double.longBitsToDouble(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                boff += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            for (int j = 0; j < len; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                int b0 = b[boff + 7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                int b1 = b[boff + 6] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                int b2 = b[boff + 5] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                int b3 = b[boff + 4] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                int b4 = b[boff + 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                int b5 = b[boff + 2] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                int b6 = b[boff + 1] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                int b7 = b[boff] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                int i0 = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                int i1 = (b4 << 24) | (b5 << 16) | (b6 << 8) | b7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                long l = ((long)i0 << 32) | (i1 & 0xffffffffL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                d[off + j] = Double.longBitsToDouble(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                boff += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public long getStreamPosition() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        return streamPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public int getBitOffset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    public void setBitOffset(int bitOffset) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (bitOffset < 0 || bitOffset > 7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            throw new IllegalArgumentException("bitOffset must be betwwen 0 and 7!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        this.bitOffset = bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public int readBit() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        // Compute final bit offset before we call read() and seek()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        int newBitOffset = (this.bitOffset + 1) & 0x7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        int val = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (val == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (newBitOffset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            // Move byte position back if in the middle of a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            seek(getStreamPosition() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            // Shift the bit to be read to the rightmost position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            val >>= 8 - newBitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        this.bitOffset = newBitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return val & 0x1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public long readBits(int numBits) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (numBits < 0 || numBits > 64) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (numBits == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            return 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        // Have to read additional bits on the left equal to the bit offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        int bitsToRead = numBits + bitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        // Compute final bit offset before we call read() and seek()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        int newBitOffset = (this.bitOffset + numBits) & 0x7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        // Read a byte at a time, accumulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        long accum = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        while (bitsToRead > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            int val = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if (val == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            accum <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            accum |= val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            bitsToRead -= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        // Move byte position back if in the middle of a byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (newBitOffset != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            seek(getStreamPosition() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        this.bitOffset = newBitOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        // Shift away unwanted bits on the right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        accum >>>= (-bitsToRead); // Negative of bitsToRead == extra bits read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        // Mask out unwanted bits on the left
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        accum &= (-1L >>> (64 - numBits));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return accum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Returns <code>-1L</code> to indicate that the stream has unknown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * length.  Subclasses must override this method to provide actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * length information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @return -1L to indicate unknown length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * Advances the current stream position by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * <code>seek(getStreamPosition() + n)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * <p> The bit offset is reset to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param n the number of bytes to seek forward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @return an <code>int</code> representing the number of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @exception IOException if <code>getStreamPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * throws an <code>IOException</code> when computing either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * the starting or ending position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public int skipBytes(int n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        long pos = getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        seek(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return (int)(getStreamPosition() - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * Advances the current stream position by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * <code>seek(getStreamPosition() + n)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * <p> The bit offset is reset to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * @param n the number of bytes to seek forward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * @return a <code>long</code> representing the number of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @exception IOException if <code>getStreamPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * throws an <code>IOException</code> when computing either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * the starting or ending position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    public long skipBytes(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        long pos = getStreamPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        seek(pos + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return getStreamPosition() - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public void seek(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        // This test also covers pos < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            throw new IndexOutOfBoundsException("pos < flushedPos!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        this.streamPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        this.bitOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Pushes the current stream position onto a stack of marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    public void mark() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        try {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   781
            markByteStack.push(Long.valueOf(getStreamPosition()));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   782
            markBitStack.push(Integer.valueOf(getBitOffset()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Resets the current stream byte and bit positions from the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * of marked positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <p> An <code>IOException</code> will be thrown if the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * marked position lies in the discarded portion of the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @exception IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (markByteStack.empty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        long pos = ((Long)markByteStack.pop()).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            throw new IIOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                ("Previous marked position has been discarded!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        seek(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        int offset = ((Integer)markBitStack.pop()).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        setBitOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public void flushBefore(long pos) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        if (pos < flushedPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            throw new IndexOutOfBoundsException("pos < flushedPos!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (pos > getStreamPosition()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            throw new IndexOutOfBoundsException("pos > getStreamPosition()!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        // Invariant: flushedPos >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        flushedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        flushBefore(getStreamPosition());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    public long getFlushedPosition() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        return flushedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Default implementation returns false.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * override this if they cache data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public boolean isCached() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Default implementation returns false.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * override this if they cache data in main memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public boolean isCachedMemory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Default implementation returns false.  Subclasses should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * override this if they cache data in a temporary file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    public boolean isCachedFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        checkClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        isClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * Finalizes this object prior to garbage collection.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * <code>close</code> method is called to close any open input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * source.  This method should not be called from application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @exception Throwable if an error occurs during superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * finalization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (!isClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
}