jdk/src/java.base/share/classes/java/io/PushbackReader.java
author bpb
Tue, 15 Dec 2015 09:02:46 -0800
changeset 34694 8566f3f715b0
parent 30443 8d2f2ce637bd
permissions -rw-r--r--
8143394: PushbackReader throws NullPointerException Summary: Synchronize on lock in close() to avoid NPE from read()-close() race. Reviewed-by: alanb, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
34694
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
     2
 * Copyright (c) 1996, 2015, 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
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 character-stream reader that allows characters to be pushed back into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author      Mark Reinhold
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    34
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class PushbackReader extends FilterReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /** Pushback buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private char[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /** Current position in buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Creates a new pushback reader with a pushback buffer of the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param   in   The reader from which characters will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param   size The size of the pushback buffer
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 9035
diff changeset
    50
     * @exception IllegalArgumentException if {@code size <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public PushbackReader(Reader in, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new IllegalArgumentException("size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.buf = new char[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.pos = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Creates a new pushback reader with a one-character pushback buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param   in  The reader from which characters will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public PushbackReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this(in, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /** Checks to make sure that the stream has not been closed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Reads a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return     The character read, or -1 if the end of the stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *             reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (pos < buf.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                return buf[pos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Reads characters into a portion of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param      cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param      off   Offset at which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param      len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return     The number of characters read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception  IOException  If an I/O error occurs
30443
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   105
     * @exception  IndexOutOfBoundsException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    } else if ((off < 0) || (off > cbuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    if (len < avail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        avail = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    System.arraycopy(buf, pos, cbuf, off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    pos += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    off += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    len -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    len = super.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        return (avail == 0) ? -1 : avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    return avail + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Pushes back a single character by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * pushback buffer. After this method returns, the next character to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * will have the value <code>(char)c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param  c  The int value representing a character to be pushed back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception  IOException  If the pushback buffer is full,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *                          or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void unread(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (pos == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                throw new IOException("Pushback buffer overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            buf[--pos] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
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
     * Pushes back a portion of an array of characters by copying it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * front of the pushback buffer.  After this method returns, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * character to be read will have the value <code>cbuf[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * character after that will have the value <code>cbuf[off+1]</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param  cbuf  Character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  off   Offset of first character to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param  len   Number of characters to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @exception  IOException  If there is insufficient room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *                          buffer, or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    public void unread(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (len > pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                throw new IOException("Pushback buffer overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            pos -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            System.arraycopy(cbuf, off, buf, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Pushes back an array of characters by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * pushback buffer.  After this method returns, the next character to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * read will have the value <code>cbuf[0]</code>, the character after that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * will have the value <code>cbuf[1]</code>, and so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param  cbuf  Character array to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @exception  IOException  If there is insufficient room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *                          buffer, or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void unread(char cbuf[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        unread(cbuf, 0, cbuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Tells whether this stream is ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            return (pos < buf.length) || super.ready();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Marks the present position in the stream. The <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * for class <code>PushbackReader</code> always throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception  IOException  Always, since mark is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Resets the stream. The <code>reset</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>PushbackReader</code> always throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception  IOException  Always, since reset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Tells whether this stream supports the mark() operation, which it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return false;
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
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * it. Once the stream has been closed, further read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * unread(), ready(), or skip() invocations will throw an IOException.
34694
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   244
     * Closing a previously closed stream has no effect. This method will block
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   245
     * while there is another thread blocking on the reader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public void close() throws IOException {
34694
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   250
        synchronized (lock) {
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   251
            super.close();
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   252
            buf = null;
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   253
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Skips characters.  This method will block until some characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * available, an I/O error occurs, or the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param  n  The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return    The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (n < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new IllegalArgumentException("skip value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                if (n <= avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    pos = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    n -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return avail + super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}