rt/emul/compact/src/main/java/java/io/StringReader.java
changeset 1985 cd1cc103a03c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/io/StringReader.java	Tue Jan 17 07:04:06 2017 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.io;
    1.30 +
    1.31 +
    1.32 +/**
    1.33 + * A character stream whose source is a string.
    1.34 + *
    1.35 + * @author      Mark Reinhold
    1.36 + * @since       JDK1.1
    1.37 + */
    1.38 +
    1.39 +public class StringReader extends Reader {
    1.40 +
    1.41 +    private String str;
    1.42 +    private int length;
    1.43 +    private int next = 0;
    1.44 +    private int mark = 0;
    1.45 +
    1.46 +    /**
    1.47 +     * Creates a new string reader.
    1.48 +     *
    1.49 +     * @param s  String providing the character stream.
    1.50 +     */
    1.51 +    public StringReader(String s) {
    1.52 +        this.str = s;
    1.53 +        this.length = s.length();
    1.54 +    }
    1.55 +
    1.56 +    /** Check to make sure that the stream has not been closed */
    1.57 +    private void ensureOpen() throws IOException {
    1.58 +        if (str == null)
    1.59 +            throw new IOException("Stream closed");
    1.60 +    }
    1.61 +
    1.62 +    /**
    1.63 +     * Reads a single character.
    1.64 +     *
    1.65 +     * @return     The character read, or -1 if the end of the stream has been
    1.66 +     *             reached
    1.67 +     *
    1.68 +     * @exception  IOException  If an I/O error occurs
    1.69 +     */
    1.70 +    public int read() throws IOException {
    1.71 +        synchronized (lock) {
    1.72 +            ensureOpen();
    1.73 +            if (next >= length)
    1.74 +                return -1;
    1.75 +            return str.charAt(next++);
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    /**
    1.80 +     * Reads characters into a portion of an array.
    1.81 +     *
    1.82 +     * @param      cbuf  Destination buffer
    1.83 +     * @param      off   Offset at which to start writing characters
    1.84 +     * @param      len   Maximum number of characters to read
    1.85 +     *
    1.86 +     * @return     The number of characters read, or -1 if the end of the
    1.87 +     *             stream has been reached
    1.88 +     *
    1.89 +     * @exception  IOException  If an I/O error occurs
    1.90 +     */
    1.91 +    public int read(char cbuf[], int off, int len) throws IOException {
    1.92 +        synchronized (lock) {
    1.93 +            ensureOpen();
    1.94 +            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
    1.95 +                ((off + len) > cbuf.length) || ((off + len) < 0)) {
    1.96 +                throw new IndexOutOfBoundsException();
    1.97 +            } else if (len == 0) {
    1.98 +                return 0;
    1.99 +            }
   1.100 +            if (next >= length)
   1.101 +                return -1;
   1.102 +            int n = Math.min(length - next, len);
   1.103 +            str.getChars(next, next + n, cbuf, off);
   1.104 +            next += n;
   1.105 +            return n;
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Skips the specified number of characters in the stream. Returns
   1.111 +     * the number of characters that were skipped.
   1.112 +     *
   1.113 +     * <p>The <code>ns</code> parameter may be negative, even though the
   1.114 +     * <code>skip</code> method of the {@link Reader} superclass throws
   1.115 +     * an exception in this case. Negative values of <code>ns</code> cause the
   1.116 +     * stream to skip backwards. Negative return values indicate a skip
   1.117 +     * backwards. It is not possible to skip backwards past the beginning of
   1.118 +     * the string.
   1.119 +     *
   1.120 +     * <p>If the entire string has been read or skipped, then this method has
   1.121 +     * no effect and always returns 0.
   1.122 +     *
   1.123 +     * @exception  IOException  If an I/O error occurs
   1.124 +     */
   1.125 +    public long skip(long ns) throws IOException {
   1.126 +        synchronized (lock) {
   1.127 +            ensureOpen();
   1.128 +            if (next >= length)
   1.129 +                return 0;
   1.130 +            // Bound skip by beginning and end of the source
   1.131 +            long n = Math.min(length - next, ns);
   1.132 +            n = Math.max(-next, n);
   1.133 +            next += n;
   1.134 +            return n;
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * Tells whether this stream is ready to be read.
   1.140 +     *
   1.141 +     * @return True if the next read() is guaranteed not to block for input
   1.142 +     *
   1.143 +     * @exception  IOException  If the stream is closed
   1.144 +     */
   1.145 +    public boolean ready() throws IOException {
   1.146 +        synchronized (lock) {
   1.147 +        ensureOpen();
   1.148 +        return true;
   1.149 +        }
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * Tells whether this stream supports the mark() operation, which it does.
   1.154 +     */
   1.155 +    public boolean markSupported() {
   1.156 +        return true;
   1.157 +    }
   1.158 +
   1.159 +    /**
   1.160 +     * Marks the present position in the stream.  Subsequent calls to reset()
   1.161 +     * will reposition the stream to this point.
   1.162 +     *
   1.163 +     * @param  readAheadLimit  Limit on the number of characters that may be
   1.164 +     *                         read while still preserving the mark.  Because
   1.165 +     *                         the stream's input comes from a string, there
   1.166 +     *                         is no actual limit, so this argument must not
   1.167 +     *                         be negative, but is otherwise ignored.
   1.168 +     *
   1.169 +     * @exception  IllegalArgumentException  If readAheadLimit is < 0
   1.170 +     * @exception  IOException  If an I/O error occurs
   1.171 +     */
   1.172 +    public void mark(int readAheadLimit) throws IOException {
   1.173 +        if (readAheadLimit < 0){
   1.174 +            throw new IllegalArgumentException("Read-ahead limit < 0");
   1.175 +        }
   1.176 +        synchronized (lock) {
   1.177 +            ensureOpen();
   1.178 +            mark = next;
   1.179 +        }
   1.180 +    }
   1.181 +
   1.182 +    /**
   1.183 +     * Resets the stream to the most recent mark, or to the beginning of the
   1.184 +     * string if it has never been marked.
   1.185 +     *
   1.186 +     * @exception  IOException  If an I/O error occurs
   1.187 +     */
   1.188 +    public void reset() throws IOException {
   1.189 +        synchronized (lock) {
   1.190 +            ensureOpen();
   1.191 +            next = mark;
   1.192 +        }
   1.193 +    }
   1.194 +
   1.195 +    /**
   1.196 +     * Closes the stream and releases any system resources associated with
   1.197 +     * it. Once the stream has been closed, further read(),
   1.198 +     * ready(), mark(), or reset() invocations will throw an IOException.
   1.199 +     * Closing a previously closed stream has no effect.
   1.200 +     */
   1.201 +    public void close() {
   1.202 +        str = null;
   1.203 +    }
   1.204 +}