jdk/src/share/classes/java/io/StringReader.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 18156 edb590d448c5
child 24865 09b1d992ca72
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18156
diff changeset
     2
 * Copyright (c) 1996, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since       JDK1.1
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @exception  IOException  If an I/O error occurs
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (next >= length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            int n = Math.min(length - next, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            str.getChars(next, next + n, cbuf, off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            next += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
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
     * Skips the specified number of characters in the stream. Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * the number of characters that were skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>The <code>ns</code> parameter may be negative, even though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>skip</code> method of the {@link Reader} superclass throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * an exception in this case. Negative values of <code>ns</code> cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * stream to skip backwards. Negative return values indicate a skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * backwards. It is not possible to skip backwards past the beginning of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>If the entire string has been read or skipped, then this method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * no effect and always returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public long skip(long ns) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (next >= length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // Bound skip by beginning and end of the source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            long n = Math.min(length - next, ns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            n = Math.max(-next, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            next += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
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
     * Tells whether this stream is ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return True if the next read() is guaranteed not to block for input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @exception  IOException  If the stream is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
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
     * Tells whether this stream supports the mark() operation, which it does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * will reposition the stream to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param  readAheadLimit  Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                         read while still preserving the mark.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                         the stream's input comes from a string, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *                         is no actual limit, so this argument must not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *                         be negative, but is otherwise ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   166
     * @exception  IllegalArgumentException  If {@code readAheadLimit < 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (readAheadLimit < 0){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            throw new IllegalArgumentException("Read-ahead limit < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            mark = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
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
     * Resets the stream to the most recent mark, or to the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * string if it has never been marked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            next = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
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
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * it. Once the stream has been closed, further read(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * ready(), mark(), or reset() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}