rt/emul/compact/src/main/java/java/io/StringReader.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 28 Sep 2013 02:02:00 +0200
branchjdk7-b147
changeset 1314 e3db9cca817b
permissions -rw-r--r--
Adding StringWriter and reader as it is used in Javac
jtulach@1314
     1
/*
jtulach@1314
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
jtulach@1314
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@1314
     4
 *
jtulach@1314
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@1314
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@1314
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@1314
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@1314
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@1314
    10
 *
jtulach@1314
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@1314
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@1314
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@1314
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@1314
    15
 * accompanied this code).
jtulach@1314
    16
 *
jtulach@1314
    17
 * You should have received a copy of the GNU General Public License version
jtulach@1314
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@1314
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@1314
    20
 *
jtulach@1314
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@1314
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@1314
    23
 * questions.
jtulach@1314
    24
 */
jtulach@1314
    25
jtulach@1314
    26
package java.io;
jtulach@1314
    27
jtulach@1314
    28
jtulach@1314
    29
/**
jtulach@1314
    30
 * A character stream whose source is a string.
jtulach@1314
    31
 *
jtulach@1314
    32
 * @author      Mark Reinhold
jtulach@1314
    33
 * @since       JDK1.1
jtulach@1314
    34
 */
jtulach@1314
    35
jtulach@1314
    36
public class StringReader extends Reader {
jtulach@1314
    37
jtulach@1314
    38
    private String str;
jtulach@1314
    39
    private int length;
jtulach@1314
    40
    private int next = 0;
jtulach@1314
    41
    private int mark = 0;
jtulach@1314
    42
jtulach@1314
    43
    /**
jtulach@1314
    44
     * Creates a new string reader.
jtulach@1314
    45
     *
jtulach@1314
    46
     * @param s  String providing the character stream.
jtulach@1314
    47
     */
jtulach@1314
    48
    public StringReader(String s) {
jtulach@1314
    49
        this.str = s;
jtulach@1314
    50
        this.length = s.length();
jtulach@1314
    51
    }
jtulach@1314
    52
jtulach@1314
    53
    /** Check to make sure that the stream has not been closed */
jtulach@1314
    54
    private void ensureOpen() throws IOException {
jtulach@1314
    55
        if (str == null)
jtulach@1314
    56
            throw new IOException("Stream closed");
jtulach@1314
    57
    }
jtulach@1314
    58
jtulach@1314
    59
    /**
jtulach@1314
    60
     * Reads a single character.
jtulach@1314
    61
     *
jtulach@1314
    62
     * @return     The character read, or -1 if the end of the stream has been
jtulach@1314
    63
     *             reached
jtulach@1314
    64
     *
jtulach@1314
    65
     * @exception  IOException  If an I/O error occurs
jtulach@1314
    66
     */
jtulach@1314
    67
    public int read() throws IOException {
jtulach@1314
    68
        synchronized (lock) {
jtulach@1314
    69
            ensureOpen();
jtulach@1314
    70
            if (next >= length)
jtulach@1314
    71
                return -1;
jtulach@1314
    72
            return str.charAt(next++);
jtulach@1314
    73
        }
jtulach@1314
    74
    }
jtulach@1314
    75
jtulach@1314
    76
    /**
jtulach@1314
    77
     * Reads characters into a portion of an array.
jtulach@1314
    78
     *
jtulach@1314
    79
     * @param      cbuf  Destination buffer
jtulach@1314
    80
     * @param      off   Offset at which to start writing characters
jtulach@1314
    81
     * @param      len   Maximum number of characters to read
jtulach@1314
    82
     *
jtulach@1314
    83
     * @return     The number of characters read, or -1 if the end of the
jtulach@1314
    84
     *             stream has been reached
jtulach@1314
    85
     *
jtulach@1314
    86
     * @exception  IOException  If an I/O error occurs
jtulach@1314
    87
     */
jtulach@1314
    88
    public int read(char cbuf[], int off, int len) throws IOException {
jtulach@1314
    89
        synchronized (lock) {
jtulach@1314
    90
            ensureOpen();
jtulach@1314
    91
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
jtulach@1314
    92
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
jtulach@1314
    93
                throw new IndexOutOfBoundsException();
jtulach@1314
    94
            } else if (len == 0) {
jtulach@1314
    95
                return 0;
jtulach@1314
    96
            }
jtulach@1314
    97
            if (next >= length)
jtulach@1314
    98
                return -1;
jtulach@1314
    99
            int n = Math.min(length - next, len);
jtulach@1314
   100
            str.getChars(next, next + n, cbuf, off);
jtulach@1314
   101
            next += n;
jtulach@1314
   102
            return n;
jtulach@1314
   103
        }
jtulach@1314
   104
    }
jtulach@1314
   105
jtulach@1314
   106
    /**
jtulach@1314
   107
     * Skips the specified number of characters in the stream. Returns
jtulach@1314
   108
     * the number of characters that were skipped.
jtulach@1314
   109
     *
jtulach@1314
   110
     * <p>The <code>ns</code> parameter may be negative, even though the
jtulach@1314
   111
     * <code>skip</code> method of the {@link Reader} superclass throws
jtulach@1314
   112
     * an exception in this case. Negative values of <code>ns</code> cause the
jtulach@1314
   113
     * stream to skip backwards. Negative return values indicate a skip
jtulach@1314
   114
     * backwards. It is not possible to skip backwards past the beginning of
jtulach@1314
   115
     * the string.
jtulach@1314
   116
     *
jtulach@1314
   117
     * <p>If the entire string has been read or skipped, then this method has
jtulach@1314
   118
     * no effect and always returns 0.
jtulach@1314
   119
     *
jtulach@1314
   120
     * @exception  IOException  If an I/O error occurs
jtulach@1314
   121
     */
jtulach@1314
   122
    public long skip(long ns) throws IOException {
jtulach@1314
   123
        synchronized (lock) {
jtulach@1314
   124
            ensureOpen();
jtulach@1314
   125
            if (next >= length)
jtulach@1314
   126
                return 0;
jtulach@1314
   127
            // Bound skip by beginning and end of the source
jtulach@1314
   128
            long n = Math.min(length - next, ns);
jtulach@1314
   129
            n = Math.max(-next, n);
jtulach@1314
   130
            next += n;
jtulach@1314
   131
            return n;
jtulach@1314
   132
        }
jtulach@1314
   133
    }
jtulach@1314
   134
jtulach@1314
   135
    /**
jtulach@1314
   136
     * Tells whether this stream is ready to be read.
jtulach@1314
   137
     *
jtulach@1314
   138
     * @return True if the next read() is guaranteed not to block for input
jtulach@1314
   139
     *
jtulach@1314
   140
     * @exception  IOException  If the stream is closed
jtulach@1314
   141
     */
jtulach@1314
   142
    public boolean ready() throws IOException {
jtulach@1314
   143
        synchronized (lock) {
jtulach@1314
   144
        ensureOpen();
jtulach@1314
   145
        return true;
jtulach@1314
   146
        }
jtulach@1314
   147
    }
jtulach@1314
   148
jtulach@1314
   149
    /**
jtulach@1314
   150
     * Tells whether this stream supports the mark() operation, which it does.
jtulach@1314
   151
     */
jtulach@1314
   152
    public boolean markSupported() {
jtulach@1314
   153
        return true;
jtulach@1314
   154
    }
jtulach@1314
   155
jtulach@1314
   156
    /**
jtulach@1314
   157
     * Marks the present position in the stream.  Subsequent calls to reset()
jtulach@1314
   158
     * will reposition the stream to this point.
jtulach@1314
   159
     *
jtulach@1314
   160
     * @param  readAheadLimit  Limit on the number of characters that may be
jtulach@1314
   161
     *                         read while still preserving the mark.  Because
jtulach@1314
   162
     *                         the stream's input comes from a string, there
jtulach@1314
   163
     *                         is no actual limit, so this argument must not
jtulach@1314
   164
     *                         be negative, but is otherwise ignored.
jtulach@1314
   165
     *
jtulach@1314
   166
     * @exception  IllegalArgumentException  If readAheadLimit is < 0
jtulach@1314
   167
     * @exception  IOException  If an I/O error occurs
jtulach@1314
   168
     */
jtulach@1314
   169
    public void mark(int readAheadLimit) throws IOException {
jtulach@1314
   170
        if (readAheadLimit < 0){
jtulach@1314
   171
            throw new IllegalArgumentException("Read-ahead limit < 0");
jtulach@1314
   172
        }
jtulach@1314
   173
        synchronized (lock) {
jtulach@1314
   174
            ensureOpen();
jtulach@1314
   175
            mark = next;
jtulach@1314
   176
        }
jtulach@1314
   177
    }
jtulach@1314
   178
jtulach@1314
   179
    /**
jtulach@1314
   180
     * Resets the stream to the most recent mark, or to the beginning of the
jtulach@1314
   181
     * string if it has never been marked.
jtulach@1314
   182
     *
jtulach@1314
   183
     * @exception  IOException  If an I/O error occurs
jtulach@1314
   184
     */
jtulach@1314
   185
    public void reset() throws IOException {
jtulach@1314
   186
        synchronized (lock) {
jtulach@1314
   187
            ensureOpen();
jtulach@1314
   188
            next = mark;
jtulach@1314
   189
        }
jtulach@1314
   190
    }
jtulach@1314
   191
jtulach@1314
   192
    /**
jtulach@1314
   193
     * Closes the stream and releases any system resources associated with
jtulach@1314
   194
     * it. Once the stream has been closed, further read(),
jtulach@1314
   195
     * ready(), mark(), or reset() invocations will throw an IOException.
jtulach@1314
   196
     * Closing a previously closed stream has no effect.
jtulach@1314
   197
     */
jtulach@1314
   198
    public void close() {
jtulach@1314
   199
        str = null;
jtulach@1314
   200
    }
jtulach@1314
   201
}