jdk/src/share/classes/java/io/InputStream.java
author darcy
Fri, 30 Jan 2009 15:09:00 -0800
changeset 1942 bdfd8f01987a
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6799462: Minor typo (wrong word) in JavaDoc for InputStream.read(byte[] b) method Reviewed-by: sherman, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 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
 * This abstract class is the superclass of all classes representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * an input stream of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p> Applications that need to define a subclass of <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * must always provide a method that returns the next byte of input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see     java.io.BufferedInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see     java.io.ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see     java.io.DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     java.io.PushbackInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class InputStream implements Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int SKIP_BUFFER_SIZE = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // skipBuffer is initialized in skip(long), if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static byte[] skipBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Reads the next byte of data from the input stream. The value byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * returned as an <code>int</code> in the range <code>0</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <code>255</code>. If no byte is available because the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * has been reached, the value <code>-1</code> is returned. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * blocks until input data is available, the end of the stream is detected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * <p> A subclass must provide an implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *             stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public abstract int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Reads some number of bytes from the input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * returned as an integer.  This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * available, end of file is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * least one byte. If no byte is available because the stream is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * end of the file, the value <code>-1</code> is returned; otherwise, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * least one byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p> The first byte read is stored into element <code>b[0]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * number of bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <code>b[b.length-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return     the total number of bytes read into the buffer, or
1942
bdfd8f01987a 6799462: Minor typo (wrong word) in JavaDoc for InputStream.read(byte[] b) method
darcy
parents: 2
diff changeset
    93
     *             <code>-1</code> if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception  IOException  If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * other than the end of the file, if the input stream has been closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Reads up to <code>len</code> bytes of data from the input stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * an array of bytes.  An attempt is made to read as many as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * <code>len</code> bytes, but a smaller number may be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * The number of bytes actually read is returned as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p> This method blocks until input data is available, end of file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p> If <code>len</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * least one byte. If no byte is available because the stream is at end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * file, the value <code>-1</code> is returned; otherwise, at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p> The first byte read is stored into element <code>b[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <code>b[off+len-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <p> In every case, elements <code>b[0]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>b[off]</code> and elements <code>b[off+len]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>b[b.length-1]</code> are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * for class <code>InputStream</code> simply calls the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>read()</code> repeatedly. If the first such call results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>IOException</code>, that exception is returned from the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * any subsequent call to <code>read()</code> results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>IOException</code>, the exception is caught and treated as if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * were end of file; the bytes read up to that point are stored into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>b</code> and the number of bytes read before the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * occurred is returned. The default implementation of this method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * until the requested amount of input data <code>len</code> has been read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * end of file is detected, or an exception is thrown. Subclasses are encouraged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param      off   the start offset in array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param      len   the maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * other than end of file, or if the input stream has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * some other I/O error occurs.
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
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Skips over and discards <code>n</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * stream. The <code>skip</code> method may, for a variety of reasons, end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * up skipping over some smaller number of bytes, possibly <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * This may result from any of a number of conditions; reaching end of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * before <code>n</code> bytes have been skipped is only one possibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The actual number of bytes skipped is returned.  If <code>n</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * negative, no bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p> The <code>skip</code> method of this class creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * byte array and then repeatedly reads into it until <code>n</code> bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * have been read or the end of the stream has been reached. Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * encouraged to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * For instance, the implementation may depend on the ability to seek.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception  IOException  if the stream does not support seek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *                          or if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        long remaining = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (skipBuffer == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            skipBuffer = new byte[SKIP_BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        byte[] localSkipBuffer = skipBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        while (remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            nr = read(localSkipBuffer, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                      (int) Math.min(SKIP_BUFFER_SIZE, remaining));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (nr < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            remaining -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return n - remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * invocation of a method for this input stream. The next invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * might be the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p> Note that while some implementations of {@code InputStream} will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * the total number of bytes in the stream, many will not.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * never correct to use the return value of this method to allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * a buffer intended to hold all data in this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p> A subclass' implementation of this method may choose to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * {@link IOException} if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * invoking the {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p> The {@code available} method for class {@code InputStream} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * returns {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p> This method should be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return     an estimate of the number of bytes that can be read (or skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *             over) from this input stream without blocking or {@code 0} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *             it reaches the end of the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p> The <code>close</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void close() throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Marks the current position in this input stream. A subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * the <code>reset</code> method repositions this stream at the last marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * position so that subsequent reads re-read the same bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <p> The <code>readlimit</code> arguments tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <p> The general contract of <code>mark</code> is that, if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * <code>markSupported</code> returns <code>true</code>, the stream somehow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * remembers all the bytes read after the call to <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * stands ready to supply those same bytes again if and whenever the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <code>reset</code> is called.  However, the stream is not required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * remember any data at all if more than <code>readlimit</code> bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * read from the stream before <code>reset</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> Marking a closed stream should not have any effect on the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p> The <code>mark</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public synchronized void mark(int readlimit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p> The general contract of <code>reset</code> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p><ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <code>true</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *     <ul><li> If the method <code>mark</code> has not been called since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *     the stream was created, or the number of bytes read from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *     since <code>mark</code> was last called is larger than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *     to <code>mark</code> at that last call, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *     <code>IOException</code> might be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *     <li> If such an <code>IOException</code> is not thrown, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *     stream is reset to a state such that all the bytes read since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *     most recent call to <code>mark</code> (or since the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *     file, if <code>mark</code> has not been called) will be resupplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *     to subsequent callers of the <code>read</code> method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *     any bytes that otherwise would have been the next input data as of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *     the time of the call to <code>reset</code>. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * <code>false</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *     <ul><li> The call to <code>reset</code> may throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *     <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *     <li> If an <code>IOException</code> is not thrown, then the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *     is reset to a fixed state that depends on the particular type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *     input stream and how it was created. The bytes that will be supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *     to subsequent callers of the <code>read</code> method depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *     particular type of the input stream. </ul></ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>The method <code>reset</code> for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * does nothing except throw an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @exception  IOException  if this stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>reset</code> methods. Whether or not <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * <code>reset</code> are supported is an invariant property of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * particular input stream instance. The <code>markSupported</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * of <code>InputStream</code> returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @return  <code>true</code> if this stream instance supports the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          and reset methods; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}