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