Adding StringWriter and reader as it is used in Javac jdk7-b147
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 28 Sep 2013 02:02:00 +0200
branchjdk7-b147
changeset 1314e3db9cca817b
parent 1292 9cf04876e4a5
child 1315 6ce019139b13
child 1318 1af7f8903b62
Adding StringWriter and reader as it is used in Javac
rt/emul/compact/src/main/java/java/io/StringReader.java
rt/emul/compact/src/main/java/java/io/StringWriter.java
     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	Sat Sep 28 02:02:00 2013 +0200
     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 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rt/emul/compact/src/main/java/java/io/StringWriter.java	Sat Sep 28 02:02:00 2013 +0200
     2.3 @@ -0,0 +1,236 @@
     2.4 +/*
     2.5 + * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.  Oracle designates this
    2.11 + * particular file as subject to the "Classpath" exception as provided
    2.12 + * by Oracle in the LICENSE file that accompanied this code.
    2.13 + *
    2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.17 + * version 2 for more details (a copy is included in the LICENSE file that
    2.18 + * accompanied this code).
    2.19 + *
    2.20 + * You should have received a copy of the GNU General Public License version
    2.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.23 + *
    2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.25 + * or visit www.oracle.com if you need additional information or have any
    2.26 + * questions.
    2.27 + */
    2.28 +
    2.29 +package java.io;
    2.30 +
    2.31 +
    2.32 +/**
    2.33 + * A character stream that collects its output in a string buffer, which can
    2.34 + * then be used to construct a string.
    2.35 + * <p>
    2.36 + * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
    2.37 + * can be called after the stream has been closed without generating an
    2.38 + * <tt>IOException</tt>.
    2.39 + *
    2.40 + * @author      Mark Reinhold
    2.41 + * @since       JDK1.1
    2.42 + */
    2.43 +
    2.44 +public class StringWriter extends Writer {
    2.45 +
    2.46 +    private StringBuffer buf;
    2.47 +
    2.48 +    /**
    2.49 +     * Create a new string writer using the default initial string-buffer
    2.50 +     * size.
    2.51 +     */
    2.52 +    public StringWriter() {
    2.53 +        buf = new StringBuffer();
    2.54 +        lock = buf;
    2.55 +    }
    2.56 +
    2.57 +    /**
    2.58 +     * Create a new string writer using the specified initial string-buffer
    2.59 +     * size.
    2.60 +     *
    2.61 +     * @param initialSize
    2.62 +     *        The number of <tt>char</tt> values that will fit into this buffer
    2.63 +     *        before it is automatically expanded
    2.64 +     *
    2.65 +     * @throws IllegalArgumentException
    2.66 +     *         If <tt>initialSize</tt> is negative
    2.67 +     */
    2.68 +    public StringWriter(int initialSize) {
    2.69 +        if (initialSize < 0) {
    2.70 +            throw new IllegalArgumentException("Negative buffer size");
    2.71 +        }
    2.72 +        buf = new StringBuffer(initialSize);
    2.73 +        lock = buf;
    2.74 +    }
    2.75 +
    2.76 +    /**
    2.77 +     * Write a single character.
    2.78 +     */
    2.79 +    public void write(int c) {
    2.80 +        buf.append((char) c);
    2.81 +    }
    2.82 +
    2.83 +    /**
    2.84 +     * Write a portion of an array of characters.
    2.85 +     *
    2.86 +     * @param  cbuf  Array of characters
    2.87 +     * @param  off   Offset from which to start writing characters
    2.88 +     * @param  len   Number of characters to write
    2.89 +     */
    2.90 +    public void write(char cbuf[], int off, int len) {
    2.91 +        if ((off < 0) || (off > cbuf.length) || (len < 0) ||
    2.92 +            ((off + len) > cbuf.length) || ((off + len) < 0)) {
    2.93 +            throw new IndexOutOfBoundsException();
    2.94 +        } else if (len == 0) {
    2.95 +            return;
    2.96 +        }
    2.97 +        buf.append(cbuf, off, len);
    2.98 +    }
    2.99 +
   2.100 +    /**
   2.101 +     * Write a string.
   2.102 +     */
   2.103 +    public void write(String str) {
   2.104 +        buf.append(str);
   2.105 +    }
   2.106 +
   2.107 +    /**
   2.108 +     * Write a portion of a string.
   2.109 +     *
   2.110 +     * @param  str  String to be written
   2.111 +     * @param  off  Offset from which to start writing characters
   2.112 +     * @param  len  Number of characters to write
   2.113 +     */
   2.114 +    public void write(String str, int off, int len)  {
   2.115 +        buf.append(str.substring(off, off + len));
   2.116 +    }
   2.117 +
   2.118 +    /**
   2.119 +     * Appends the specified character sequence to this writer.
   2.120 +     *
   2.121 +     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
   2.122 +     * behaves in exactly the same way as the invocation
   2.123 +     *
   2.124 +     * <pre>
   2.125 +     *     out.write(csq.toString()) </pre>
   2.126 +     *
   2.127 +     * <p> Depending on the specification of <tt>toString</tt> for the
   2.128 +     * character sequence <tt>csq</tt>, the entire sequence may not be
   2.129 +     * appended. For instance, invoking the <tt>toString</tt> method of a
   2.130 +     * character buffer will return a subsequence whose content depends upon
   2.131 +     * the buffer's position and limit.
   2.132 +     *
   2.133 +     * @param  csq
   2.134 +     *         The character sequence to append.  If <tt>csq</tt> is
   2.135 +     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
   2.136 +     *         appended to this writer.
   2.137 +     *
   2.138 +     * @return  This writer
   2.139 +     *
   2.140 +     * @since  1.5
   2.141 +     */
   2.142 +    public StringWriter append(CharSequence csq) {
   2.143 +        if (csq == null)
   2.144 +            write("null");
   2.145 +        else
   2.146 +            write(csq.toString());
   2.147 +        return this;
   2.148 +    }
   2.149 +
   2.150 +    /**
   2.151 +     * Appends a subsequence of the specified character sequence to this writer.
   2.152 +     *
   2.153 +     * <p> An invocation of this method of the form <tt>out.append(csq, start,
   2.154 +     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
   2.155 +     * exactly the same way as the invocation
   2.156 +     *
   2.157 +     * <pre>
   2.158 +     *     out.write(csq.subSequence(start, end).toString()) </pre>
   2.159 +     *
   2.160 +     * @param  csq
   2.161 +     *         The character sequence from which a subsequence will be
   2.162 +     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
   2.163 +     *         will be appended as if <tt>csq</tt> contained the four
   2.164 +     *         characters <tt>"null"</tt>.
   2.165 +     *
   2.166 +     * @param  start
   2.167 +     *         The index of the first character in the subsequence
   2.168 +     *
   2.169 +     * @param  end
   2.170 +     *         The index of the character following the last character in the
   2.171 +     *         subsequence
   2.172 +     *
   2.173 +     * @return  This writer
   2.174 +     *
   2.175 +     * @throws  IndexOutOfBoundsException
   2.176 +     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
   2.177 +     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
   2.178 +     *          <tt>csq.length()</tt>
   2.179 +     *
   2.180 +     * @since  1.5
   2.181 +     */
   2.182 +    public StringWriter append(CharSequence csq, int start, int end) {
   2.183 +        CharSequence cs = (csq == null ? "null" : csq);
   2.184 +        write(cs.subSequence(start, end).toString());
   2.185 +        return this;
   2.186 +    }
   2.187 +
   2.188 +    /**
   2.189 +     * Appends the specified character to this writer.
   2.190 +     *
   2.191 +     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
   2.192 +     * behaves in exactly the same way as the invocation
   2.193 +     *
   2.194 +     * <pre>
   2.195 +     *     out.write(c) </pre>
   2.196 +     *
   2.197 +     * @param  c
   2.198 +     *         The 16-bit character to append
   2.199 +     *
   2.200 +     * @return  This writer
   2.201 +     *
   2.202 +     * @since 1.5
   2.203 +     */
   2.204 +    public StringWriter append(char c) {
   2.205 +        write(c);
   2.206 +        return this;
   2.207 +    }
   2.208 +
   2.209 +    /**
   2.210 +     * Return the buffer's current value as a string.
   2.211 +     */
   2.212 +    public String toString() {
   2.213 +        return buf.toString();
   2.214 +    }
   2.215 +
   2.216 +    /**
   2.217 +     * Return the string buffer itself.
   2.218 +     *
   2.219 +     * @return StringBuffer holding the current buffer value.
   2.220 +     */
   2.221 +    public StringBuffer getBuffer() {
   2.222 +        return buf;
   2.223 +    }
   2.224 +
   2.225 +    /**
   2.226 +     * Flush the stream.
   2.227 +     */
   2.228 +    public void flush() {
   2.229 +    }
   2.230 +
   2.231 +    /**
   2.232 +     * Closing a <tt>StringWriter</tt> has no effect. The methods in this
   2.233 +     * class can be called after the stream has been closed without generating
   2.234 +     * an <tt>IOException</tt>.
   2.235 +     */
   2.236 +    public void close() throws IOException {
   2.237 +    }
   2.238 +
   2.239 +}