jdk/src/share/classes/java/io/PushbackInputStream.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8540 ed028ce13912
child 18156 edb590d448c5
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8540
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A <code>PushbackInputStream</code> adds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * functionality to another input stream, namely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * the  ability to "push back" or "unread"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * one byte. This is useful in situations where
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * it is  convenient for a fragment of code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to read an indefinite number of data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that  are delimited by a particular byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * value; after reading the terminating byte,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the  code fragment can "unread" it, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the next read operation on the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * will reread the byte that was pushed back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * For example, bytes representing the  characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * constituting an identifier might be terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * by a byte representing an  operator character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * a method whose job is to read just an identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * can read until it  sees the operator and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * then push the operator back to be re-read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Jonathan Payne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class PushbackInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The pushback buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * The position within the pushback buffer from which the next byte will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * be read.  When the buffer is empty, <code>pos</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <code>buf.length</code>; when the buffer is full, <code>pos</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Check to make sure that this stream has not been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Creates a <code>PushbackInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * with a pushback buffer of the specified <code>size</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * and saves its  argument, the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <code>in</code>, for later use. Initially,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * there is no pushed-back byte  (the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>pushBack</code> is initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>-1</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param  in    the input stream from which bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param  size  the size of the pushback buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception IllegalArgumentException if size is <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @since  JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public PushbackInputStream(InputStream in, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException("size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.pos = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates a <code>PushbackInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * and saves its  argument, the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <code>in</code>, for later use. Initially,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * there is no pushed-back byte  (the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <code>pushBack</code> is initialized to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <code>-1</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param   in   the input stream from which bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public PushbackInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this(in, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Reads the next byte of data from this input stream. The value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * byte is returned as an <code>int</code> in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <code>0</code> to <code>255</code>. If no byte is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * because the end of the stream has been reached, the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>-1</code> is returned. This method blocks until input data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * is available, the end of the stream is detected, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <p> This method returns the most recently pushed-back byte, if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * one, and otherwise calls the <code>read</code> method of its underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * input stream and returns whatever value that method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *             stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @exception  IOException  if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (pos < buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return buf[pos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Reads up to <code>len</code> bytes of data from this input stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * an array of bytes.  This method first reads any pushed-back bytes; after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * that, if fewer than <code>len</code> bytes have been read then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * reads from the underlying input stream. If <code>len</code> is not zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * blocks until at least 1 byte of input is available; otherwise, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param      off   the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception  IOException  if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (len < avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                avail = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            System.arraycopy(buf, pos, b, off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            pos += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            off += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            len -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            len = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                return avail == 0 ? -1 : avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return avail + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Pushes back a byte by copying it to the front of the pushback buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * After this method returns, the next byte to be read will have the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <code>(byte)b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param      b   the <code>int</code> value whose low-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *                  byte is to be pushed back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception IOException If there is not enough room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *            buffer for the byte, or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *            invoking its {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public void unread(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new IOException("Push back buffer is full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        buf[--pos] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Pushes back a portion of an array of bytes by copying it to the front
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * of the pushback buffer.  After this method returns, the next byte to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * read will have the value <code>b[off]</code>, the byte after that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * have the value <code>b[off+1]</code>, and so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param b the byte array to push back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param off the start offset of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param len the number of bytes to push back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @exception IOException If there is not enough room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *            buffer for the specified number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *            or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *            invoking its {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void unread(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (len > pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            throw new IOException("Push back buffer is full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        pos -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        System.arraycopy(b, off, buf, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Pushes back an array of bytes by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * pushback buffer.  After this method returns, the next byte to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * will have the value <code>b[0]</code>, the byte after that will have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * value <code>b[1]</code>, and so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param b the byte array to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception IOException If there is not enough room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *            buffer for the specified number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *            or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *            invoking its {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void unread(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        unread(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * invocation of a method for this input stream. The next invocation might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <p> The method returns the sum of the number of bytes that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * pushed back and the value returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * java.io.FilterInputStream#available available}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return     the number of bytes that can be read (or skipped over) from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *             the input stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @exception  IOException  if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @see        java.io.InputStream#available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        ensureOpen();
7280
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   276
        int n = buf.length - pos;
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   277
        int avail = super.available();
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   278
        return n > (Integer.MAX_VALUE - avail)
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   279
                    ? Integer.MAX_VALUE
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   280
                    : n + avail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Skips over and discards <code>n</code> bytes of data from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * input stream. The <code>skip</code> method may, for a variety of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * reasons, end up skipping over some smaller number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * possibly zero.  If <code>n</code> is negative, no bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p> The <code>skip</code> method of <code>PushbackInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * first skips over the bytes in the pushback buffer, if any.  It then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * calls the <code>skip</code> method of the underlying input stream if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * more bytes need to be skipped.  The actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param      n  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return     {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @exception  IOException  if the stream does not support seek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *            or the stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *            invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *            or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @see        java.io.InputStream#skip(long n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        long pskip = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if (pskip > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (n < pskip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                pskip = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            pos += pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            n -= pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            pskip += super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <code>reset</code> methods, which it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return   <code>false</code>, since this class does not support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *           <code>mark</code> and <code>reset</code> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Marks the current position in this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p> The <code>mark</code> method of <code>PushbackInputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    public synchronized void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * <p> The method <code>reset</code> for class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>PushbackInputStream</code> does nothing except throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @exception  IOException  if this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * Once the stream has been closed, further read(), unread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * available(), reset(), or skip() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public synchronized void close() throws IOException {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   377
        if (in == null)
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   378
            return;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   379
        in.close();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   380
        in = null;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   381
        buf = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
}