emul/mini/src/main/java/java/io/InputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 20:39:23 +0100
branchemul
changeset 554 05224402145d
parent 118 emul/src/main/java/java/io/InputStream.java@5daeb26fb58d
permissions -rw-r--r--
First attempt to separate 'mini' profile from the rest of JDK APIs
jtulach@118
     1
/*
jtulach@118
     2
 * Copyright (c) 1994, 2006, Oracle and/or its affiliates. All rights reserved.
jtulach@118
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@118
     4
 *
jtulach@118
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@118
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@118
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@118
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@118
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@118
    10
 *
jtulach@118
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@118
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@118
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@118
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@118
    15
 * accompanied this code).
jtulach@118
    16
 *
jtulach@118
    17
 * You should have received a copy of the GNU General Public License version
jtulach@118
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@118
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@118
    20
 *
jtulach@118
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@118
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@118
    23
 * questions.
jtulach@118
    24
 */
jtulach@118
    25
jtulach@118
    26
package java.io;
jtulach@118
    27
jtulach@118
    28
/**
jtulach@118
    29
 * This abstract class is the superclass of all classes representing
jtulach@118
    30
 * an input stream of bytes.
jtulach@118
    31
 *
jtulach@118
    32
 * <p> Applications that need to define a subclass of <code>InputStream</code>
jtulach@118
    33
 * must always provide a method that returns the next byte of input.
jtulach@118
    34
 *
jtulach@118
    35
 * @author  Arthur van Hoff
jtulach@118
    36
 * @see     java.io.BufferedInputStream
jtulach@118
    37
 * @see     java.io.ByteArrayInputStream
jtulach@118
    38
 * @see     java.io.DataInputStream
jtulach@118
    39
 * @see     java.io.FilterInputStream
jtulach@118
    40
 * @see     java.io.InputStream#read()
jtulach@118
    41
 * @see     java.io.OutputStream
jtulach@118
    42
 * @see     java.io.PushbackInputStream
jtulach@118
    43
 * @since   JDK1.0
jtulach@118
    44
 */
jtulach@118
    45
public abstract class InputStream implements Closeable {
jtulach@118
    46
jtulach@118
    47
    // SKIP_BUFFER_SIZE is used to determine the size of skipBuffer
jtulach@118
    48
    private static final int SKIP_BUFFER_SIZE = 2048;
jtulach@118
    49
    // skipBuffer is initialized in skip(long), if needed.
jtulach@118
    50
    private static byte[] skipBuffer;
jtulach@118
    51
jtulach@118
    52
    /**
jtulach@118
    53
     * Reads the next byte of data from the input stream. The value byte is
jtulach@118
    54
     * returned as an <code>int</code> in the range <code>0</code> to
jtulach@118
    55
     * <code>255</code>. If no byte is available because the end of the stream
jtulach@118
    56
     * has been reached, the value <code>-1</code> is returned. This method
jtulach@118
    57
     * blocks until input data is available, the end of the stream is detected,
jtulach@118
    58
     * or an exception is thrown.
jtulach@118
    59
     *
jtulach@118
    60
     * <p> A subclass must provide an implementation of this method.
jtulach@118
    61
     *
jtulach@118
    62
     * @return     the next byte of data, or <code>-1</code> if the end of the
jtulach@118
    63
     *             stream is reached.
jtulach@118
    64
     * @exception  IOException  if an I/O error occurs.
jtulach@118
    65
     */
jtulach@118
    66
    public abstract int read() throws IOException;
jtulach@118
    67
jtulach@118
    68
    /**
jtulach@118
    69
     * Reads some number of bytes from the input stream and stores them into
jtulach@118
    70
     * the buffer array <code>b</code>. The number of bytes actually read is
jtulach@118
    71
     * returned as an integer.  This method blocks until input data is
jtulach@118
    72
     * available, end of file is detected, or an exception is thrown.
jtulach@118
    73
     *
jtulach@118
    74
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
jtulach@118
    75
     * <code>0</code> is returned; otherwise, there is an attempt to read at
jtulach@118
    76
     * least one byte. If no byte is available because the stream is at the
jtulach@118
    77
     * end of the file, the value <code>-1</code> is returned; otherwise, at
jtulach@118
    78
     * least one byte is read and stored into <code>b</code>.
jtulach@118
    79
     *
jtulach@118
    80
     * <p> The first byte read is stored into element <code>b[0]</code>, the
jtulach@118
    81
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
jtulach@118
    82
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
jtulach@118
    83
     * number of bytes actually read; these bytes will be stored in elements
jtulach@118
    84
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
jtulach@118
    85
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
jtulach@118
    86
     * <code>b[b.length-1]</code> unaffected.
jtulach@118
    87
     *
jtulach@118
    88
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
jtulach@118
    89
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
jtulach@118
    90
     *
jtulach@118
    91
     * @param      b   the buffer into which the data is read.
jtulach@118
    92
     * @return     the total number of bytes read into the buffer, or
jtulach@118
    93
     *             <code>-1</code> if there is no more data because the end of
jtulach@118
    94
     *             the stream has been reached.
jtulach@118
    95
     * @exception  IOException  If the first byte cannot be read for any reason
jtulach@118
    96
     * other than the end of the file, if the input stream has been closed, or
jtulach@118
    97
     * if some other I/O error occurs.
jtulach@118
    98
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
jtulach@118
    99
     * @see        java.io.InputStream#read(byte[], int, int)
jtulach@118
   100
     */
jtulach@118
   101
    public int read(byte b[]) throws IOException {
jtulach@118
   102
        return read(b, 0, b.length);
jtulach@118
   103
    }
jtulach@118
   104
jtulach@118
   105
    /**
jtulach@118
   106
     * Reads up to <code>len</code> bytes of data from the input stream into
jtulach@118
   107
     * an array of bytes.  An attempt is made to read as many as
jtulach@118
   108
     * <code>len</code> bytes, but a smaller number may be read.
jtulach@118
   109
     * The number of bytes actually read is returned as an integer.
jtulach@118
   110
     *
jtulach@118
   111
     * <p> This method blocks until input data is available, end of file is
jtulach@118
   112
     * detected, or an exception is thrown.
jtulach@118
   113
     *
jtulach@118
   114
     * <p> If <code>len</code> is zero, then no bytes are read and
jtulach@118
   115
     * <code>0</code> is returned; otherwise, there is an attempt to read at
jtulach@118
   116
     * least one byte. If no byte is available because the stream is at end of
jtulach@118
   117
     * file, the value <code>-1</code> is returned; otherwise, at least one
jtulach@118
   118
     * byte is read and stored into <code>b</code>.
jtulach@118
   119
     *
jtulach@118
   120
     * <p> The first byte read is stored into element <code>b[off]</code>, the
jtulach@118
   121
     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
jtulach@118
   122
     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
jtulach@118
   123
     * bytes actually read; these bytes will be stored in elements
jtulach@118
   124
     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
jtulach@118
   125
     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
jtulach@118
   126
     * <code>b[off+len-1]</code> unaffected.
jtulach@118
   127
     *
jtulach@118
   128
     * <p> In every case, elements <code>b[0]</code> through
jtulach@118
   129
     * <code>b[off]</code> and elements <code>b[off+len]</code> through
jtulach@118
   130
     * <code>b[b.length-1]</code> are unaffected.
jtulach@118
   131
     *
jtulach@118
   132
     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
jtulach@118
   133
     * for class <code>InputStream</code> simply calls the method
jtulach@118
   134
     * <code>read()</code> repeatedly. If the first such call results in an
jtulach@118
   135
     * <code>IOException</code>, that exception is returned from the call to
jtulach@118
   136
     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
jtulach@118
   137
     * any subsequent call to <code>read()</code> results in a
jtulach@118
   138
     * <code>IOException</code>, the exception is caught and treated as if it
jtulach@118
   139
     * were end of file; the bytes read up to that point are stored into
jtulach@118
   140
     * <code>b</code> and the number of bytes read before the exception
jtulach@118
   141
     * occurred is returned. The default implementation of this method blocks
jtulach@118
   142
     * until the requested amount of input data <code>len</code> has been read,
jtulach@118
   143
     * end of file is detected, or an exception is thrown. Subclasses are encouraged
jtulach@118
   144
     * to provide a more efficient implementation of this method.
jtulach@118
   145
     *
jtulach@118
   146
     * @param      b     the buffer into which the data is read.
jtulach@118
   147
     * @param      off   the start offset in array <code>b</code>
jtulach@118
   148
     *                   at which the data is written.
jtulach@118
   149
     * @param      len   the maximum number of bytes to read.
jtulach@118
   150
     * @return     the total number of bytes read into the buffer, or
jtulach@118
   151
     *             <code>-1</code> if there is no more data because the end of
jtulach@118
   152
     *             the stream has been reached.
jtulach@118
   153
     * @exception  IOException If the first byte cannot be read for any reason
jtulach@118
   154
     * other than end of file, or if the input stream has been closed, or if
jtulach@118
   155
     * some other I/O error occurs.
jtulach@118
   156
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
jtulach@118
   157
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
jtulach@118
   158
     * <code>len</code> is negative, or <code>len</code> is greater than
jtulach@118
   159
     * <code>b.length - off</code>
jtulach@118
   160
     * @see        java.io.InputStream#read()
jtulach@118
   161
     */
jtulach@118
   162
    public int read(byte b[], int off, int len) throws IOException {
jtulach@118
   163
        if (b == null) {
jtulach@118
   164
            throw new NullPointerException();
jtulach@118
   165
        } else if (off < 0 || len < 0 || len > b.length - off) {
jtulach@118
   166
            throw new IndexOutOfBoundsException();
jtulach@118
   167
        } else if (len == 0) {
jtulach@118
   168
            return 0;
jtulach@118
   169
        }
jtulach@118
   170
jtulach@118
   171
        int c = read();
jtulach@118
   172
        if (c == -1) {
jtulach@118
   173
            return -1;
jtulach@118
   174
        }
jtulach@118
   175
        b[off] = (byte)c;
jtulach@118
   176
jtulach@118
   177
        int i = 1;
jtulach@118
   178
        try {
jtulach@118
   179
            for (; i < len ; i++) {
jtulach@118
   180
                c = read();
jtulach@118
   181
                if (c == -1) {
jtulach@118
   182
                    break;
jtulach@118
   183
                }
jtulach@118
   184
                b[off + i] = (byte)c;
jtulach@118
   185
            }
jtulach@118
   186
        } catch (IOException ee) {
jtulach@118
   187
        }
jtulach@118
   188
        return i;
jtulach@118
   189
    }
jtulach@118
   190
jtulach@118
   191
    /**
jtulach@118
   192
     * Skips over and discards <code>n</code> bytes of data from this input
jtulach@118
   193
     * stream. The <code>skip</code> method may, for a variety of reasons, end
jtulach@118
   194
     * up skipping over some smaller number of bytes, possibly <code>0</code>.
jtulach@118
   195
     * This may result from any of a number of conditions; reaching end of file
jtulach@118
   196
     * before <code>n</code> bytes have been skipped is only one possibility.
jtulach@118
   197
     * The actual number of bytes skipped is returned.  If <code>n</code> is
jtulach@118
   198
     * negative, no bytes are skipped.
jtulach@118
   199
     *
jtulach@118
   200
     * <p> The <code>skip</code> method of this class creates a
jtulach@118
   201
     * byte array and then repeatedly reads into it until <code>n</code> bytes
jtulach@118
   202
     * have been read or the end of the stream has been reached. Subclasses are
jtulach@118
   203
     * encouraged to provide a more efficient implementation of this method.
jtulach@118
   204
     * For instance, the implementation may depend on the ability to seek.
jtulach@118
   205
     *
jtulach@118
   206
     * @param      n   the number of bytes to be skipped.
jtulach@118
   207
     * @return     the actual number of bytes skipped.
jtulach@118
   208
     * @exception  IOException  if the stream does not support seek,
jtulach@118
   209
     *                          or if some other I/O error occurs.
jtulach@118
   210
     */
jtulach@118
   211
    public long skip(long n) throws IOException {
jtulach@118
   212
jtulach@118
   213
        long remaining = n;
jtulach@118
   214
        int nr;
jtulach@118
   215
        if (skipBuffer == null)
jtulach@118
   216
            skipBuffer = new byte[SKIP_BUFFER_SIZE];
jtulach@118
   217
jtulach@118
   218
        byte[] localSkipBuffer = skipBuffer;
jtulach@118
   219
jtulach@118
   220
        if (n <= 0) {
jtulach@118
   221
            return 0;
jtulach@118
   222
        }
jtulach@118
   223
jtulach@118
   224
        while (remaining > 0) {
jtulach@118
   225
            nr = read(localSkipBuffer, 0,
jtulach@118
   226
                      (int) Math.min(SKIP_BUFFER_SIZE, remaining));
jtulach@118
   227
            if (nr < 0) {
jtulach@118
   228
                break;
jtulach@118
   229
            }
jtulach@118
   230
            remaining -= nr;
jtulach@118
   231
        }
jtulach@118
   232
jtulach@118
   233
        return n - remaining;
jtulach@118
   234
    }
jtulach@118
   235
jtulach@118
   236
    /**
jtulach@118
   237
     * Returns an estimate of the number of bytes that can be read (or
jtulach@118
   238
     * skipped over) from this input stream without blocking by the next
jtulach@118
   239
     * invocation of a method for this input stream. The next invocation
jtulach@118
   240
     * might be the same thread or another thread.  A single read or skip of this
jtulach@118
   241
     * many bytes will not block, but may read or skip fewer bytes.
jtulach@118
   242
     *
jtulach@118
   243
     * <p> Note that while some implementations of {@code InputStream} will return
jtulach@118
   244
     * the total number of bytes in the stream, many will not.  It is
jtulach@118
   245
     * never correct to use the return value of this method to allocate
jtulach@118
   246
     * a buffer intended to hold all data in this stream.
jtulach@118
   247
     *
jtulach@118
   248
     * <p> A subclass' implementation of this method may choose to throw an
jtulach@118
   249
     * {@link IOException} if this input stream has been closed by
jtulach@118
   250
     * invoking the {@link #close()} method.
jtulach@118
   251
     *
jtulach@118
   252
     * <p> The {@code available} method for class {@code InputStream} always
jtulach@118
   253
     * returns {@code 0}.
jtulach@118
   254
     *
jtulach@118
   255
     * <p> This method should be overridden by subclasses.
jtulach@118
   256
     *
jtulach@118
   257
     * @return     an estimate of the number of bytes that can be read (or skipped
jtulach@118
   258
     *             over) from this input stream without blocking or {@code 0} when
jtulach@118
   259
     *             it reaches the end of the input stream.
jtulach@118
   260
     * @exception  IOException if an I/O error occurs.
jtulach@118
   261
     */
jtulach@118
   262
    public int available() throws IOException {
jtulach@118
   263
        return 0;
jtulach@118
   264
    }
jtulach@118
   265
jtulach@118
   266
    /**
jtulach@118
   267
     * Closes this input stream and releases any system resources associated
jtulach@118
   268
     * with the stream.
jtulach@118
   269
     *
jtulach@118
   270
     * <p> The <code>close</code> method of <code>InputStream</code> does
jtulach@118
   271
     * nothing.
jtulach@118
   272
     *
jtulach@118
   273
     * @exception  IOException  if an I/O error occurs.
jtulach@118
   274
     */
jtulach@118
   275
    public void close() throws IOException {}
jtulach@118
   276
jtulach@118
   277
    /**
jtulach@118
   278
     * Marks the current position in this input stream. A subsequent call to
jtulach@118
   279
     * the <code>reset</code> method repositions this stream at the last marked
jtulach@118
   280
     * position so that subsequent reads re-read the same bytes.
jtulach@118
   281
     *
jtulach@118
   282
     * <p> The <code>readlimit</code> arguments tells this input stream to
jtulach@118
   283
     * allow that many bytes to be read before the mark position gets
jtulach@118
   284
     * invalidated.
jtulach@118
   285
     *
jtulach@118
   286
     * <p> The general contract of <code>mark</code> is that, if the method
jtulach@118
   287
     * <code>markSupported</code> returns <code>true</code>, the stream somehow
jtulach@118
   288
     * remembers all the bytes read after the call to <code>mark</code> and
jtulach@118
   289
     * stands ready to supply those same bytes again if and whenever the method
jtulach@118
   290
     * <code>reset</code> is called.  However, the stream is not required to
jtulach@118
   291
     * remember any data at all if more than <code>readlimit</code> bytes are
jtulach@118
   292
     * read from the stream before <code>reset</code> is called.
jtulach@118
   293
     *
jtulach@118
   294
     * <p> Marking a closed stream should not have any effect on the stream.
jtulach@118
   295
     *
jtulach@118
   296
     * <p> The <code>mark</code> method of <code>InputStream</code> does
jtulach@118
   297
     * nothing.
jtulach@118
   298
     *
jtulach@118
   299
     * @param   readlimit   the maximum limit of bytes that can be read before
jtulach@118
   300
     *                      the mark position becomes invalid.
jtulach@118
   301
     * @see     java.io.InputStream#reset()
jtulach@118
   302
     */
jtulach@118
   303
    public synchronized void mark(int readlimit) {}
jtulach@118
   304
jtulach@118
   305
    /**
jtulach@118
   306
     * Repositions this stream to the position at the time the
jtulach@118
   307
     * <code>mark</code> method was last called on this input stream.
jtulach@118
   308
     *
jtulach@118
   309
     * <p> The general contract of <code>reset</code> is:
jtulach@118
   310
     *
jtulach@118
   311
     * <p><ul>
jtulach@118
   312
     *
jtulach@118
   313
     * <li> If the method <code>markSupported</code> returns
jtulach@118
   314
     * <code>true</code>, then:
jtulach@118
   315
     *
jtulach@118
   316
     *     <ul><li> If the method <code>mark</code> has not been called since
jtulach@118
   317
     *     the stream was created, or the number of bytes read from the stream
jtulach@118
   318
     *     since <code>mark</code> was last called is larger than the argument
jtulach@118
   319
     *     to <code>mark</code> at that last call, then an
jtulach@118
   320
     *     <code>IOException</code> might be thrown.
jtulach@118
   321
     *
jtulach@118
   322
     *     <li> If such an <code>IOException</code> is not thrown, then the
jtulach@118
   323
     *     stream is reset to a state such that all the bytes read since the
jtulach@118
   324
     *     most recent call to <code>mark</code> (or since the start of the
jtulach@118
   325
     *     file, if <code>mark</code> has not been called) will be resupplied
jtulach@118
   326
     *     to subsequent callers of the <code>read</code> method, followed by
jtulach@118
   327
     *     any bytes that otherwise would have been the next input data as of
jtulach@118
   328
     *     the time of the call to <code>reset</code>. </ul>
jtulach@118
   329
     *
jtulach@118
   330
     * <li> If the method <code>markSupported</code> returns
jtulach@118
   331
     * <code>false</code>, then:
jtulach@118
   332
     *
jtulach@118
   333
     *     <ul><li> The call to <code>reset</code> may throw an
jtulach@118
   334
     *     <code>IOException</code>.
jtulach@118
   335
     *
jtulach@118
   336
     *     <li> If an <code>IOException</code> is not thrown, then the stream
jtulach@118
   337
     *     is reset to a fixed state that depends on the particular type of the
jtulach@118
   338
     *     input stream and how it was created. The bytes that will be supplied
jtulach@118
   339
     *     to subsequent callers of the <code>read</code> method depend on the
jtulach@118
   340
     *     particular type of the input stream. </ul></ul>
jtulach@118
   341
     *
jtulach@118
   342
     * <p>The method <code>reset</code> for class <code>InputStream</code>
jtulach@118
   343
     * does nothing except throw an <code>IOException</code>.
jtulach@118
   344
     *
jtulach@118
   345
     * @exception  IOException  if this stream has not been marked or if the
jtulach@118
   346
     *               mark has been invalidated.
jtulach@118
   347
     * @see     java.io.InputStream#mark(int)
jtulach@118
   348
     * @see     java.io.IOException
jtulach@118
   349
     */
jtulach@118
   350
    public synchronized void reset() throws IOException {
jtulach@118
   351
        throw new IOException("mark/reset not supported");
jtulach@118
   352
    }
jtulach@118
   353
jtulach@118
   354
    /**
jtulach@118
   355
     * Tests if this input stream supports the <code>mark</code> and
jtulach@118
   356
     * <code>reset</code> methods. Whether or not <code>mark</code> and
jtulach@118
   357
     * <code>reset</code> are supported is an invariant property of a
jtulach@118
   358
     * particular input stream instance. The <code>markSupported</code> method
jtulach@118
   359
     * of <code>InputStream</code> returns <code>false</code>.
jtulach@118
   360
     *
jtulach@118
   361
     * @return  <code>true</code> if this stream instance supports the mark
jtulach@118
   362
     *          and reset methods; <code>false</code> otherwise.
jtulach@118
   363
     * @see     java.io.InputStream#mark(int)
jtulach@118
   364
     * @see     java.io.InputStream#reset()
jtulach@118
   365
     */
jtulach@118
   366
    public boolean markSupported() {
jtulach@118
   367
        return false;
jtulach@118
   368
    }
jtulach@118
   369
jtulach@118
   370
}