rt/emul/compact/src/main/java/java/io/BufferedInputStream.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 03 Oct 2013 17:36:44 +0200
changeset 1337 c794024954b5
parent 1334 588d5bf7a560
permissions -rw-r--r--
Implementation of few more JDK classes
jtulach@1334
     1
/*
jtulach@1334
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jtulach@1334
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1334
     4
 *
jtulach@1334
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1334
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1334
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1334
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1334
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1334
    10
 *
jtulach@1334
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1334
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1334
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1334
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1334
    15
 * accompanied this code).
jtulach@1334
    16
 *
jtulach@1334
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1334
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1334
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1334
    20
 *
jtulach@1334
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1334
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1334
    23
 * questions.
jtulach@1334
    24
 */
jtulach@1334
    25
jtulach@1334
    26
package java.io;
jtulach@1334
    27
jtulach@1334
    28
/**
jtulach@1334
    29
 * A <code>BufferedInputStream</code> adds
jtulach@1334
    30
 * functionality to another input stream-namely,
jtulach@1334
    31
 * the ability to buffer the input and to
jtulach@1334
    32
 * support the <code>mark</code> and <code>reset</code>
jtulach@1334
    33
 * methods. When  the <code>BufferedInputStream</code>
jtulach@1334
    34
 * is created, an internal buffer array is
jtulach@1334
    35
 * created. As bytes  from the stream are read
jtulach@1334
    36
 * or skipped, the internal buffer is refilled
jtulach@1334
    37
 * as necessary  from the contained input stream,
jtulach@1334
    38
 * many bytes at a time. The <code>mark</code>
jtulach@1334
    39
 * operation  remembers a point in the input
jtulach@1334
    40
 * stream and the <code>reset</code> operation
jtulach@1334
    41
 * causes all the  bytes read since the most
jtulach@1334
    42
 * recent <code>mark</code> operation to be
jtulach@1334
    43
 * reread before new bytes are  taken from
jtulach@1334
    44
 * the contained input stream.
jtulach@1334
    45
 *
jtulach@1334
    46
 * @author  Arthur van Hoff
jtulach@1334
    47
 * @since   JDK1.0
jtulach@1334
    48
 */
jtulach@1334
    49
public
jtulach@1334
    50
class BufferedInputStream extends FilterInputStream {
jtulach@1334
    51
jtulach@1334
    52
    private static int defaultBufferSize = 8192;
jtulach@1334
    53
jtulach@1334
    54
    /**
jtulach@1334
    55
     * The internal buffer array where the data is stored. When necessary,
jtulach@1334
    56
     * it may be replaced by another array of
jtulach@1334
    57
     * a different size.
jtulach@1334
    58
     */
jtulach@1334
    59
    protected volatile byte buf[];
jtulach@1334
    60
jtulach@1334
    61
jtulach@1334
    62
    /**
jtulach@1334
    63
     * The index one greater than the index of the last valid byte in
jtulach@1334
    64
     * the buffer.
jtulach@1334
    65
     * This value is always
jtulach@1334
    66
     * in the range <code>0</code> through <code>buf.length</code>;
jtulach@1334
    67
     * elements <code>buf[0]</code>  through <code>buf[count-1]
jtulach@1334
    68
     * </code>contain buffered input data obtained
jtulach@1334
    69
     * from the underlying  input stream.
jtulach@1334
    70
     */
jtulach@1334
    71
    protected int count;
jtulach@1334
    72
jtulach@1334
    73
    /**
jtulach@1334
    74
     * The current position in the buffer. This is the index of the next
jtulach@1334
    75
     * character to be read from the <code>buf</code> array.
jtulach@1334
    76
     * <p>
jtulach@1334
    77
     * This value is always in the range <code>0</code>
jtulach@1334
    78
     * through <code>count</code>. If it is less
jtulach@1334
    79
     * than <code>count</code>, then  <code>buf[pos]</code>
jtulach@1334
    80
     * is the next byte to be supplied as input;
jtulach@1334
    81
     * if it is equal to <code>count</code>, then
jtulach@1334
    82
     * the  next <code>read</code> or <code>skip</code>
jtulach@1334
    83
     * operation will require more bytes to be
jtulach@1334
    84
     * read from the contained  input stream.
jtulach@1334
    85
     *
jtulach@1334
    86
     * @see     java.io.BufferedInputStream#buf
jtulach@1334
    87
     */
jtulach@1334
    88
    protected int pos;
jtulach@1334
    89
jtulach@1334
    90
    /**
jtulach@1334
    91
     * The value of the <code>pos</code> field at the time the last
jtulach@1334
    92
     * <code>mark</code> method was called.
jtulach@1334
    93
     * <p>
jtulach@1334
    94
     * This value is always
jtulach@1334
    95
     * in the range <code>-1</code> through <code>pos</code>.
jtulach@1334
    96
     * If there is no marked position in  the input
jtulach@1334
    97
     * stream, this field is <code>-1</code>. If
jtulach@1334
    98
     * there is a marked position in the input
jtulach@1334
    99
     * stream,  then <code>buf[markpos]</code>
jtulach@1334
   100
     * is the first byte to be supplied as input
jtulach@1334
   101
     * after a <code>reset</code> operation. If
jtulach@1334
   102
     * <code>markpos</code> is not <code>-1</code>,
jtulach@1334
   103
     * then all bytes from positions <code>buf[markpos]</code>
jtulach@1334
   104
     * through  <code>buf[pos-1]</code> must remain
jtulach@1334
   105
     * in the buffer array (though they may be
jtulach@1334
   106
     * moved to  another place in the buffer array,
jtulach@1334
   107
     * with suitable adjustments to the values
jtulach@1334
   108
     * of <code>count</code>,  <code>pos</code>,
jtulach@1334
   109
     * and <code>markpos</code>); they may not
jtulach@1334
   110
     * be discarded unless and until the difference
jtulach@1334
   111
     * between <code>pos</code> and <code>markpos</code>
jtulach@1334
   112
     * exceeds <code>marklimit</code>.
jtulach@1334
   113
     *
jtulach@1334
   114
     * @see     java.io.BufferedInputStream#mark(int)
jtulach@1334
   115
     * @see     java.io.BufferedInputStream#pos
jtulach@1334
   116
     */
jtulach@1334
   117
    protected int markpos = -1;
jtulach@1334
   118
jtulach@1334
   119
    /**
jtulach@1334
   120
     * The maximum read ahead allowed after a call to the
jtulach@1334
   121
     * <code>mark</code> method before subsequent calls to the
jtulach@1334
   122
     * <code>reset</code> method fail.
jtulach@1334
   123
     * Whenever the difference between <code>pos</code>
jtulach@1334
   124
     * and <code>markpos</code> exceeds <code>marklimit</code>,
jtulach@1334
   125
     * then the  mark may be dropped by setting
jtulach@1334
   126
     * <code>markpos</code> to <code>-1</code>.
jtulach@1334
   127
     *
jtulach@1334
   128
     * @see     java.io.BufferedInputStream#mark(int)
jtulach@1334
   129
     * @see     java.io.BufferedInputStream#reset()
jtulach@1334
   130
     */
jtulach@1334
   131
    protected int marklimit;
jtulach@1334
   132
jtulach@1334
   133
    /**
jtulach@1334
   134
     * Check to make sure that underlying input stream has not been
jtulach@1334
   135
     * nulled out due to close; if not return it;
jtulach@1334
   136
     */
jtulach@1334
   137
    private InputStream getInIfOpen() throws IOException {
jtulach@1334
   138
        InputStream input = in;
jtulach@1334
   139
        if (input == null)
jtulach@1334
   140
            throw new IOException("Stream closed");
jtulach@1334
   141
        return input;
jtulach@1334
   142
    }
jtulach@1334
   143
jtulach@1334
   144
    /**
jtulach@1334
   145
     * Check to make sure that buffer has not been nulled out due to
jtulach@1334
   146
     * close; if not return it;
jtulach@1334
   147
     */
jtulach@1334
   148
    private byte[] getBufIfOpen() throws IOException {
jtulach@1334
   149
        byte[] buffer = buf;
jtulach@1334
   150
        if (buffer == null)
jtulach@1334
   151
            throw new IOException("Stream closed");
jtulach@1334
   152
        return buffer;
jtulach@1334
   153
    }
jtulach@1334
   154
jtulach@1334
   155
    /**
jtulach@1334
   156
     * Creates a <code>BufferedInputStream</code>
jtulach@1334
   157
     * and saves its  argument, the input stream
jtulach@1334
   158
     * <code>in</code>, for later use. An internal
jtulach@1334
   159
     * buffer array is created and  stored in <code>buf</code>.
jtulach@1334
   160
     *
jtulach@1334
   161
     * @param   in   the underlying input stream.
jtulach@1334
   162
     */
jtulach@1334
   163
    public BufferedInputStream(InputStream in) {
jtulach@1334
   164
        this(in, defaultBufferSize);
jtulach@1334
   165
    }
jtulach@1334
   166
jtulach@1334
   167
    /**
jtulach@1334
   168
     * Creates a <code>BufferedInputStream</code>
jtulach@1334
   169
     * with the specified buffer size,
jtulach@1334
   170
     * and saves its  argument, the input stream
jtulach@1334
   171
     * <code>in</code>, for later use.  An internal
jtulach@1334
   172
     * buffer array of length  <code>size</code>
jtulach@1334
   173
     * is created and stored in <code>buf</code>.
jtulach@1334
   174
     *
jtulach@1334
   175
     * @param   in     the underlying input stream.
jtulach@1334
   176
     * @param   size   the buffer size.
jtulach@1334
   177
     * @exception IllegalArgumentException if size <= 0.
jtulach@1334
   178
     */
jtulach@1334
   179
    public BufferedInputStream(InputStream in, int size) {
jtulach@1334
   180
        super(in);
jtulach@1334
   181
        if (size <= 0) {
jtulach@1334
   182
            throw new IllegalArgumentException("Buffer size <= 0");
jtulach@1334
   183
        }
jtulach@1334
   184
        buf = new byte[size];
jtulach@1334
   185
    }
jtulach@1334
   186
jtulach@1334
   187
    /**
jtulach@1334
   188
     * Fills the buffer with more data, taking into account
jtulach@1334
   189
     * shuffling and other tricks for dealing with marks.
jtulach@1334
   190
     * Assumes that it is being called by a synchronized method.
jtulach@1334
   191
     * This method also assumes that all data has already been read in,
jtulach@1334
   192
     * hence pos > count.
jtulach@1334
   193
     */
jtulach@1334
   194
    private void fill() throws IOException {
jtulach@1334
   195
        byte[] buffer = getBufIfOpen();
jtulach@1334
   196
        if (markpos < 0)
jtulach@1334
   197
            pos = 0;            /* no mark: throw away the buffer */
jtulach@1334
   198
        else if (pos >= buffer.length)  /* no room left in buffer */
jtulach@1334
   199
            if (markpos > 0) {  /* can throw away early part of the buffer */
jtulach@1334
   200
                int sz = pos - markpos;
jtulach@1334
   201
                System.arraycopy(buffer, markpos, buffer, 0, sz);
jtulach@1334
   202
                pos = sz;
jtulach@1334
   203
                markpos = 0;
jtulach@1334
   204
            } else if (buffer.length >= marklimit) {
jtulach@1334
   205
                markpos = -1;   /* buffer got too big, invalidate mark */
jtulach@1334
   206
                pos = 0;        /* drop buffer contents */
jtulach@1334
   207
            } else {            /* grow buffer */
jtulach@1334
   208
                int nsz = pos * 2;
jtulach@1334
   209
                if (nsz > marklimit)
jtulach@1334
   210
                    nsz = marklimit;
jtulach@1334
   211
                byte nbuf[] = new byte[nsz];
jtulach@1334
   212
                System.arraycopy(buffer, 0, nbuf, 0, pos);
jtulach@1334
   213
                buffer = nbuf;
jtulach@1334
   214
            }
jtulach@1334
   215
        count = pos;
jtulach@1334
   216
        int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
jtulach@1334
   217
        if (n > 0)
jtulach@1334
   218
            count = n + pos;
jtulach@1334
   219
    }
jtulach@1334
   220
jtulach@1334
   221
    /**
jtulach@1334
   222
     * See
jtulach@1334
   223
     * the general contract of the <code>read</code>
jtulach@1334
   224
     * method of <code>InputStream</code>.
jtulach@1334
   225
     *
jtulach@1334
   226
     * @return     the next byte of data, or <code>-1</code> if the end of the
jtulach@1334
   227
     *             stream is reached.
jtulach@1334
   228
     * @exception  IOException  if this input stream has been closed by
jtulach@1334
   229
     *                          invoking its {@link #close()} method,
jtulach@1334
   230
     *                          or an I/O error occurs.
jtulach@1334
   231
     * @see        java.io.FilterInputStream#in
jtulach@1334
   232
     */
jtulach@1334
   233
    public synchronized int read() throws IOException {
jtulach@1334
   234
        if (pos >= count) {
jtulach@1334
   235
            fill();
jtulach@1334
   236
            if (pos >= count)
jtulach@1334
   237
                return -1;
jtulach@1334
   238
        }
jtulach@1334
   239
        return getBufIfOpen()[pos++] & 0xff;
jtulach@1334
   240
    }
jtulach@1334
   241
jtulach@1334
   242
    /**
jtulach@1334
   243
     * Read characters into a portion of an array, reading from the underlying
jtulach@1334
   244
     * stream at most once if necessary.
jtulach@1334
   245
     */
jtulach@1334
   246
    private int read1(byte[] b, int off, int len) throws IOException {
jtulach@1334
   247
        int avail = count - pos;
jtulach@1334
   248
        if (avail <= 0) {
jtulach@1334
   249
            /* If the requested length is at least as large as the buffer, and
jtulach@1334
   250
               if there is no mark/reset activity, do not bother to copy the
jtulach@1334
   251
               bytes into the local buffer.  In this way buffered streams will
jtulach@1334
   252
               cascade harmlessly. */
jtulach@1334
   253
            if (len >= getBufIfOpen().length && markpos < 0) {
jtulach@1334
   254
                return getInIfOpen().read(b, off, len);
jtulach@1334
   255
            }
jtulach@1334
   256
            fill();
jtulach@1334
   257
            avail = count - pos;
jtulach@1334
   258
            if (avail <= 0) return -1;
jtulach@1334
   259
        }
jtulach@1334
   260
        int cnt = (avail < len) ? avail : len;
jtulach@1334
   261
        System.arraycopy(getBufIfOpen(), pos, b, off, cnt);
jtulach@1334
   262
        pos += cnt;
jtulach@1334
   263
        return cnt;
jtulach@1334
   264
    }
jtulach@1334
   265
jtulach@1334
   266
    /**
jtulach@1334
   267
     * Reads bytes from this byte-input stream into the specified byte array,
jtulach@1334
   268
     * starting at the given offset.
jtulach@1334
   269
     *
jtulach@1334
   270
     * <p> This method implements the general contract of the corresponding
jtulach@1334
   271
     * <code>{@link InputStream#read(byte[], int, int) read}</code> method of
jtulach@1334
   272
     * the <code>{@link InputStream}</code> class.  As an additional
jtulach@1334
   273
     * convenience, it attempts to read as many bytes as possible by repeatedly
jtulach@1334
   274
     * invoking the <code>read</code> method of the underlying stream.  This
jtulach@1334
   275
     * iterated <code>read</code> continues until one of the following
jtulach@1334
   276
     * conditions becomes true: <ul>
jtulach@1334
   277
     *
jtulach@1334
   278
     *   <li> The specified number of bytes have been read,
jtulach@1334
   279
     *
jtulach@1334
   280
     *   <li> The <code>read</code> method of the underlying stream returns
jtulach@1334
   281
     *   <code>-1</code>, indicating end-of-file, or
jtulach@1334
   282
     *
jtulach@1334
   283
     *   <li> The <code>available</code> method of the underlying stream
jtulach@1334
   284
     *   returns zero, indicating that further input requests would block.
jtulach@1334
   285
     *
jtulach@1334
   286
     * </ul> If the first <code>read</code> on the underlying stream returns
jtulach@1334
   287
     * <code>-1</code> to indicate end-of-file then this method returns
jtulach@1334
   288
     * <code>-1</code>.  Otherwise this method returns the number of bytes
jtulach@1334
   289
     * actually read.
jtulach@1334
   290
     *
jtulach@1334
   291
     * <p> Subclasses of this class are encouraged, but not required, to
jtulach@1334
   292
     * attempt to read as many bytes as possible in the same fashion.
jtulach@1334
   293
     *
jtulach@1334
   294
     * @param      b     destination buffer.
jtulach@1334
   295
     * @param      off   offset at which to start storing bytes.
jtulach@1334
   296
     * @param      len   maximum number of bytes to read.
jtulach@1334
   297
     * @return     the number of bytes read, or <code>-1</code> if the end of
jtulach@1334
   298
     *             the stream has been reached.
jtulach@1334
   299
     * @exception  IOException  if this input stream has been closed by
jtulach@1334
   300
     *                          invoking its {@link #close()} method,
jtulach@1334
   301
     *                          or an I/O error occurs.
jtulach@1334
   302
     */
jtulach@1334
   303
    public synchronized int read(byte b[], int off, int len)
jtulach@1334
   304
        throws IOException
jtulach@1334
   305
    {
jtulach@1334
   306
        getBufIfOpen(); // Check for closed stream
jtulach@1334
   307
        if ((off | len | (off + len) | (b.length - (off + len))) < 0) {
jtulach@1334
   308
            throw new IndexOutOfBoundsException();
jtulach@1334
   309
        } else if (len == 0) {
jtulach@1334
   310
            return 0;
jtulach@1334
   311
        }
jtulach@1334
   312
jtulach@1334
   313
        int n = 0;
jtulach@1334
   314
        for (;;) {
jtulach@1334
   315
            int nread = read1(b, off + n, len - n);
jtulach@1334
   316
            if (nread <= 0)
jtulach@1334
   317
                return (n == 0) ? nread : n;
jtulach@1334
   318
            n += nread;
jtulach@1334
   319
            if (n >= len)
jtulach@1334
   320
                return n;
jtulach@1334
   321
            // if not closed but no bytes available, return
jtulach@1334
   322
            InputStream input = in;
jtulach@1334
   323
            if (input != null && input.available() <= 0)
jtulach@1334
   324
                return n;
jtulach@1334
   325
        }
jtulach@1334
   326
    }
jtulach@1334
   327
jtulach@1334
   328
    /**
jtulach@1334
   329
     * See the general contract of the <code>skip</code>
jtulach@1334
   330
     * method of <code>InputStream</code>.
jtulach@1334
   331
     *
jtulach@1334
   332
     * @exception  IOException  if the stream does not support seek,
jtulach@1334
   333
     *                          or if this input stream has been closed by
jtulach@1334
   334
     *                          invoking its {@link #close()} method, or an
jtulach@1334
   335
     *                          I/O error occurs.
jtulach@1334
   336
     */
jtulach@1334
   337
    public synchronized long skip(long n) throws IOException {
jtulach@1334
   338
        getBufIfOpen(); // Check for closed stream
jtulach@1334
   339
        if (n <= 0) {
jtulach@1334
   340
            return 0;
jtulach@1334
   341
        }
jtulach@1334
   342
        long avail = count - pos;
jtulach@1334
   343
jtulach@1334
   344
        if (avail <= 0) {
jtulach@1334
   345
            // If no mark position set then don't keep in buffer
jtulach@1334
   346
            if (markpos <0)
jtulach@1334
   347
                return getInIfOpen().skip(n);
jtulach@1334
   348
jtulach@1334
   349
            // Fill in buffer to save bytes for reset
jtulach@1334
   350
            fill();
jtulach@1334
   351
            avail = count - pos;
jtulach@1334
   352
            if (avail <= 0)
jtulach@1334
   353
                return 0;
jtulach@1334
   354
        }
jtulach@1334
   355
jtulach@1334
   356
        long skipped = (avail < n) ? avail : n;
jtulach@1334
   357
        pos += skipped;
jtulach@1334
   358
        return skipped;
jtulach@1334
   359
    }
jtulach@1334
   360
jtulach@1334
   361
    /**
jtulach@1334
   362
     * Returns an estimate of the number of bytes that can be read (or
jtulach@1334
   363
     * skipped over) from this input stream without blocking by the next
jtulach@1334
   364
     * invocation of a method for this input stream. The next invocation might be
jtulach@1334
   365
     * the same thread or another thread.  A single read or skip of this
jtulach@1334
   366
     * many bytes will not block, but may read or skip fewer bytes.
jtulach@1334
   367
     * <p>
jtulach@1334
   368
     * This method returns the sum of the number of bytes remaining to be read in
jtulach@1334
   369
     * the buffer (<code>count&nbsp;- pos</code>) and the result of calling the
jtulach@1334
   370
     * {@link java.io.FilterInputStream#in in}.available().
jtulach@1334
   371
     *
jtulach@1334
   372
     * @return     an estimate of the number of bytes that can be read (or skipped
jtulach@1334
   373
     *             over) from this input stream without blocking.
jtulach@1334
   374
     * @exception  IOException  if this input stream has been closed by
jtulach@1334
   375
     *                          invoking its {@link #close()} method,
jtulach@1334
   376
     *                          or an I/O error occurs.
jtulach@1334
   377
     */
jtulach@1334
   378
    public synchronized int available() throws IOException {
jtulach@1334
   379
        int n = count - pos;
jtulach@1334
   380
        int avail = getInIfOpen().available();
jtulach@1334
   381
        return n > (Integer.MAX_VALUE - avail)
jtulach@1334
   382
                    ? Integer.MAX_VALUE
jtulach@1334
   383
                    : n + avail;
jtulach@1334
   384
    }
jtulach@1334
   385
jtulach@1334
   386
    /**
jtulach@1334
   387
     * See the general contract of the <code>mark</code>
jtulach@1334
   388
     * method of <code>InputStream</code>.
jtulach@1334
   389
     *
jtulach@1334
   390
     * @param   readlimit   the maximum limit of bytes that can be read before
jtulach@1334
   391
     *                      the mark position becomes invalid.
jtulach@1334
   392
     * @see     java.io.BufferedInputStream#reset()
jtulach@1334
   393
     */
jtulach@1334
   394
    public synchronized void mark(int readlimit) {
jtulach@1334
   395
        marklimit = readlimit;
jtulach@1334
   396
        markpos = pos;
jtulach@1334
   397
    }
jtulach@1334
   398
jtulach@1334
   399
    /**
jtulach@1334
   400
     * See the general contract of the <code>reset</code>
jtulach@1334
   401
     * method of <code>InputStream</code>.
jtulach@1334
   402
     * <p>
jtulach@1334
   403
     * If <code>markpos</code> is <code>-1</code>
jtulach@1334
   404
     * (no mark has been set or the mark has been
jtulach@1334
   405
     * invalidated), an <code>IOException</code>
jtulach@1334
   406
     * is thrown. Otherwise, <code>pos</code> is
jtulach@1334
   407
     * set equal to <code>markpos</code>.
jtulach@1334
   408
     *
jtulach@1334
   409
     * @exception  IOException  if this stream has not been marked or,
jtulach@1334
   410
     *                  if the mark has been invalidated, or the stream
jtulach@1334
   411
     *                  has been closed by invoking its {@link #close()}
jtulach@1334
   412
     *                  method, or an I/O error occurs.
jtulach@1334
   413
     * @see        java.io.BufferedInputStream#mark(int)
jtulach@1334
   414
     */
jtulach@1334
   415
    public synchronized void reset() throws IOException {
jtulach@1334
   416
        getBufIfOpen(); // Cause exception if closed
jtulach@1334
   417
        if (markpos < 0)
jtulach@1334
   418
            throw new IOException("Resetting to invalid mark");
jtulach@1334
   419
        pos = markpos;
jtulach@1334
   420
    }
jtulach@1334
   421
jtulach@1334
   422
    /**
jtulach@1334
   423
     * Tests if this input stream supports the <code>mark</code>
jtulach@1334
   424
     * and <code>reset</code> methods. The <code>markSupported</code>
jtulach@1334
   425
     * method of <code>BufferedInputStream</code> returns
jtulach@1334
   426
     * <code>true</code>.
jtulach@1334
   427
     *
jtulach@1334
   428
     * @return  a <code>boolean</code> indicating if this stream type supports
jtulach@1334
   429
     *          the <code>mark</code> and <code>reset</code> methods.
jtulach@1334
   430
     * @see     java.io.InputStream#mark(int)
jtulach@1334
   431
     * @see     java.io.InputStream#reset()
jtulach@1334
   432
     */
jtulach@1334
   433
    public boolean markSupported() {
jtulach@1334
   434
        return true;
jtulach@1334
   435
    }
jtulach@1334
   436
jtulach@1334
   437
    /**
jtulach@1334
   438
     * Closes this input stream and releases any system resources
jtulach@1334
   439
     * associated with the stream.
jtulach@1334
   440
     * Once the stream has been closed, further read(), available(), reset(),
jtulach@1334
   441
     * or skip() invocations will throw an IOException.
jtulach@1334
   442
     * Closing a previously closed stream has no effect.
jtulach@1334
   443
     *
jtulach@1334
   444
     * @exception  IOException  if an I/O error occurs.
jtulach@1334
   445
     */
jtulach@1334
   446
    public void close() throws IOException {
jtulach@1334
   447
        byte[] buffer;
jtulach@1334
   448
        while ( (buffer = buf) != null) {
jaroslav@1337
   449
            InputStream input = in;
jaroslav@1337
   450
            buf = null;
jaroslav@1337
   451
            in = null;
jaroslav@1337
   452
            if (input != null)
jaroslav@1337
   453
                input.close();
jaroslav@1337
   454
            return;
jtulach@1334
   455
            // Else retry in case a new buf was CASed in fill()
jtulach@1334
   456
        }
jtulach@1334
   457
    }
jtulach@1334
   458
}