src/java.base/share/classes/java/io/StringReader.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
child 58288 48e480e56aad
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 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;
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 whose source is a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @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
    33
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class StringReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private int next = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private int mark = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Creates a new string reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @param s  String providing the character stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public StringReader(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        this.str = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.length = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /** Check to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (str == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Reads a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @return     The character read, or -1 if the end of the stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *             reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    65
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (next >= length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return str.charAt(next++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
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 characters into a portion of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param      cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param      off   Offset at which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param      len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return     The number of characters read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    86
     * @throws     IOException  If an I/O error occurs
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    87
     * @throws     IndexOutOfBoundsException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (next >= length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            int n = Math.min(length - next, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            str.getChars(next, next + n, cbuf, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            next += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Skips the specified number of characters in the stream. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the number of characters that were skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>The <code>ns</code> parameter may be negative, even though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>skip</code> method of the {@link Reader} superclass throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * an exception in this case. Negative values of <code>ns</code> cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * stream to skip backwards. Negative return values indicate a skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * backwards. It is not possible to skip backwards past the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>If the entire string has been read or skipped, then this method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * no effect and always returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   121
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public long skip(long ns) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (next >= length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            // Bound skip by beginning and end of the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            long n = Math.min(length - next, ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            n = Math.max(-next, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            next += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Tells whether this stream is ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return True if the next read() is guaranteed not to block for input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   141
     * @throws     IOException  If the stream is closed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        synchronized (lock) {
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 34694
diff changeset
   145
            ensureOpen();
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 34694
diff changeset
   146
            return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Tells whether this stream supports the mark() operation, which it does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * will reposition the stream to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param  readAheadLimit  Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                         read while still preserving the mark.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                         the stream's input comes from a string, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                         is no actual limit, so this argument must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *                         be negative, but is otherwise ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   167
     * @throws     IllegalArgumentException  If {@code readAheadLimit < 0}
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   168
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (readAheadLimit < 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IllegalArgumentException("Read-ahead limit < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            mark = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Resets the stream to the most recent mark, or to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * string if it has never been marked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   184
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            next = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * it. Once the stream has been closed, further read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * ready(), mark(), or reset() invocations will throw an IOException.
34694
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   197
     * Closing a previously closed stream has no effect. This method will block
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   198
     * while there is another thread blocking on the reader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public void close() {
34694
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   201
        synchronized (lock) {
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   202
            str = null;
8566f3f715b0 8143394: PushbackReader throws NullPointerException
bpb
parents: 30443
diff changeset
   203
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}