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