jdk/src/share/classes/java/io/PushbackReader.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @since       JDK1.1
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @exception IllegalArgumentException if size is <= 0
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    } else if ((off < 0) || (off > cbuf.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    if (len < avail)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        avail = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    System.arraycopy(buf, pos, cbuf, off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    pos += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    off += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    len -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    len = super.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        return (avail == 0) ? -1 : avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    return avail + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
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
     * Pushes back a single character by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * pushback buffer. After this method returns, the next character to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * will have the value <code>(char)c</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param  c  The int value representing a character to be pushed back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception  IOException  If the pushback buffer is full,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *                          or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void unread(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (pos == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                throw new IOException("Pushback buffer overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            buf[--pos] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
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
     * Pushes back a portion of an array of characters by copying it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * front of the pushback buffer.  After this method returns, the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * character to be read will have the value <code>cbuf[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * character after that will have the value <code>cbuf[off+1]</code>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param  cbuf  Character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param  off   Offset of first character to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  len   Number of characters to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception  IOException  If there is insufficient room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *                          buffer, or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void unread(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            if (len > pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new IOException("Pushback buffer overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            pos -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            System.arraycopy(cbuf, off, buf, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
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
     * Pushes back an array of characters by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * pushback buffer.  After this method returns, the next character to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * read will have the value <code>cbuf[0]</code>, the character after that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * will have the value <code>cbuf[1]</code>, and so forth.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param  cbuf  Character array to push back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @exception  IOException  If there is insufficient room in the pushback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *                          buffer, or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public void unread(char cbuf[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        unread(cbuf, 0, cbuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Tells whether this stream is ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return (pos < buf.length) || super.ready();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
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
     * Marks the present position in the stream. The <code>mark</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * for class <code>PushbackReader</code> always throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @exception  IOException  Always, since mark is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Resets the stream. The <code>reset</code> method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <code>PushbackReader</code> always throws an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception  IOException  Always, since reset is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Tells whether this stream supports the mark() operation, which it does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * it. Once the stream has been closed, further read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * unread(), ready(), or skip() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Skips characters.  This method will block until some characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * available, an I/O error occurs, or the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param  n  The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return    The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (n < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new IllegalArgumentException("skip value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                if (n <= avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    pos = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    n -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return avail + super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
}