src/java.base/share/classes/java/io/Reader.java
author bpb
Wed, 06 Dec 2017 14:39:15 -0800
changeset 48086 50ddd5e1ede1
parent 47216 71c04702a3d5
child 49262 1b3ee04e3e54
permissions -rw-r--r--
8191706: Add Reader::transferTo(Writer) Reviewed-by: alanb, bpb, briangoetz Contributed-by: Patrick Reinhart <patrick@reini.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48086
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2017, 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
48086
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
    29
import java.util.Objects;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Abstract class for reading character streams.  The only methods that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * subclass must implement are read(char[], int, int) and close().  Most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * subclasses, however, will override some of the methods defined here in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to provide higher efficiency, additional functionality, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see BufferedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see   LineNumberReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see CharArrayReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see InputStreamReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see   FileReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see FilterReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see   PushbackReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see PipedReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see StringReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author      Mark Reinhold
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 14342
diff changeset
    50
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public abstract class Reader implements Readable, Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
48086
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
    55
    private static final int TRANSFER_BUFFER_SIZE = 8192;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The object used to synchronize operations on this stream.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * efficiency, a character-stream object may use an object other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * itself to protect critical sections.  A subclass should therefore use
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
    61
     * the object in this field rather than {@code this} or a synchronized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new character-stream reader whose critical sections will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * synchronize on the reader itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected Reader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.lock = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Creates a new character-stream reader whose critical sections will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * synchronize on the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param lock  The Object to synchronize on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected Reader(Object lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (lock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.lock = lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Attempts to read characters into the specified character buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The buffer is used as a repository of characters as-is: the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * changes made are the results of a put operation. No flipping or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * rewinding of the buffer is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param target the buffer to read characters into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @return The number of characters added to the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *         -1 if this source of characters is at its end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @throws NullPointerException if target is null
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 5506
diff changeset
    98
     * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public int read(java.nio.CharBuffer target) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int len = target.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        char[] cbuf = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int n = read(cbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            target.put(cbuf, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Reads a single character.  This method will block until a character is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * available, an I/O error occurs, or the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <p> Subclasses that intend to support efficient single-character input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * should override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return     The character read, as an integer in the range 0 to 65535
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
   118
     *             ({@code 0x00-0xffff}), or -1 if the end of the stream has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *             been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        char cb[] = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (read(cb, 0, 1) == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            return cb[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Reads characters into an array.  This method will block until some input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * is available, an I/O error occurs, or the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param       cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @return      The number of characters read, or -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *              if the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *              has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception   IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public int read(char cbuf[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return read(cbuf, 0, cbuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Reads characters into a portion of an array.  This method will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * until some input is available, an I/O error occurs, or the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @param      cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param      off   Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param      len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return     The number of characters read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @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
   160
     * @exception  IndexOutOfBoundsException
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   161
     *             If {@code off} is negative, or {@code len} is negative,
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   162
     *             or {@code len} is greater than {@code cbuf.length - off}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
   164
    public abstract int read(char cbuf[], int off, int len) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /** Maximum skip-buffer size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private static final int maxSkipBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /** Skip buffer, null until allocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private char skipBuffer[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Skips characters.  This method will block until some characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * available, an I/O error occurs, or the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param  n  The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return    The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (n < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new IllegalArgumentException("skip value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int nn = (int) Math.min(n, maxSkipBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if ((skipBuffer == null) || (skipBuffer.length < nn))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                skipBuffer = new char[nn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            long r = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            while (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (nc == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                r -= nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return n - r;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Tells whether this stream is ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return True if the next read() is guaranteed not to block for input,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * false otherwise.  Note that returning false does not guarantee that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * next read will block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Tells whether this stream supports the mark() operation. The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * implementation always returns false. Subclasses should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @return true if and only if this stream supports the mark operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * will attempt to reposition the stream to this point.  Not all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * character-input streams support the mark() operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param  readAheadLimit  Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *                         read while still preserving the mark.  After
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *                         reading this many characters, attempting to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *                         reset the stream may fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception  IOException  If the stream does not support mark(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *                          or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        throw new IOException("mark() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Resets the stream.  If the stream has been marked, then attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * reposition it at the mark.  If the stream has not been marked, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * attempt to reset it in some way appropriate to the particular stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * for example by repositioning it to its starting point.  Not all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * character-input streams support the reset() operation, and some support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * reset() without supporting mark().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @exception  IOException  If the stream has not been marked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *                          or if the mark has been invalidated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *                          or if the stream does not support reset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *                          or if some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        throw new IOException("reset() not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * it.  Once the stream has been closed, further read(), ready(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * mark(), reset(), or skip() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
   267
     public abstract void close() throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
48086
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   269
    /**
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   270
     * Reads all characters from this reader and writes the characters to the
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   271
     * given writer in the order that they are read. On return, this reader
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   272
     * will be at end of the stream. This method does not close either reader
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   273
     * or writer.
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   274
     * <p>
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   275
     * This method may block indefinitely reading from the reader, or
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   276
     * writing to the writer. The behavior for the case where the reader
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   277
     * and/or writer is <i>asynchronously closed</i>, or the thread
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   278
     * interrupted during the transfer, is highly reader and writer
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   279
     * specific, and therefore not specified.
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   280
     * <p>
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   281
     * If an I/O error occurs reading from the reader or writing to the
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   282
     * writer, then it may do so after some characters have been read or
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   283
     * written. Consequently the reader may not be at end of the stream and
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   284
     * one, or both, streams may be in an inconsistent state. It is strongly
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   285
     * recommended that both streams be promptly closed if an I/O error occurs.
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   286
     *
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   287
     * @param  out the writer, non-null
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   288
     * @return the number of characters transferred
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   289
     * @throws IOException if an I/O error occurs when reading or writing
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   290
     * @throws NullPointerException if {@code out} is {@code null}
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   291
     *
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   292
     * @since 10
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   293
     */
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   294
    public long transferTo(Writer out) throws IOException {
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   295
        Objects.requireNonNull(out, "out");
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   296
        long transferred = 0;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   297
        char[] buffer = new char[TRANSFER_BUFFER_SIZE];
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   298
        int nRead;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   299
        while ((nRead = read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   300
            out.write(buffer, 0, nRead);
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   301
            transferred += nRead;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   302
        }
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   303
        return transferred;
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   304
    }
50ddd5e1ede1 8191706: Add Reader::transferTo(Writer)
bpb
parents: 47216
diff changeset
   305
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
}