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