1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/io/StringWriter.java Sat Sep 28 02:02:00 2013 +0200
1.3 @@ -0,0 +1,236 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2004, 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 that collects its output in a string buffer, which can
1.34 + * then be used to construct a string.
1.35 + * <p>
1.36 + * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
1.37 + * can be called after the stream has been closed without generating an
1.38 + * <tt>IOException</tt>.
1.39 + *
1.40 + * @author Mark Reinhold
1.41 + * @since JDK1.1
1.42 + */
1.43 +
1.44 +public class StringWriter extends Writer {
1.45 +
1.46 + private StringBuffer buf;
1.47 +
1.48 + /**
1.49 + * Create a new string writer using the default initial string-buffer
1.50 + * size.
1.51 + */
1.52 + public StringWriter() {
1.53 + buf = new StringBuffer();
1.54 + lock = buf;
1.55 + }
1.56 +
1.57 + /**
1.58 + * Create a new string writer using the specified initial string-buffer
1.59 + * size.
1.60 + *
1.61 + * @param initialSize
1.62 + * The number of <tt>char</tt> values that will fit into this buffer
1.63 + * before it is automatically expanded
1.64 + *
1.65 + * @throws IllegalArgumentException
1.66 + * If <tt>initialSize</tt> is negative
1.67 + */
1.68 + public StringWriter(int initialSize) {
1.69 + if (initialSize < 0) {
1.70 + throw new IllegalArgumentException("Negative buffer size");
1.71 + }
1.72 + buf = new StringBuffer(initialSize);
1.73 + lock = buf;
1.74 + }
1.75 +
1.76 + /**
1.77 + * Write a single character.
1.78 + */
1.79 + public void write(int c) {
1.80 + buf.append((char) c);
1.81 + }
1.82 +
1.83 + /**
1.84 + * Write a portion of an array of characters.
1.85 + *
1.86 + * @param cbuf Array of characters
1.87 + * @param off Offset from which to start writing characters
1.88 + * @param len Number of characters to write
1.89 + */
1.90 + public void write(char cbuf[], int off, int len) {
1.91 + if ((off < 0) || (off > cbuf.length) || (len < 0) ||
1.92 + ((off + len) > cbuf.length) || ((off + len) < 0)) {
1.93 + throw new IndexOutOfBoundsException();
1.94 + } else if (len == 0) {
1.95 + return;
1.96 + }
1.97 + buf.append(cbuf, off, len);
1.98 + }
1.99 +
1.100 + /**
1.101 + * Write a string.
1.102 + */
1.103 + public void write(String str) {
1.104 + buf.append(str);
1.105 + }
1.106 +
1.107 + /**
1.108 + * Write a portion of a string.
1.109 + *
1.110 + * @param str String to be written
1.111 + * @param off Offset from which to start writing characters
1.112 + * @param len Number of characters to write
1.113 + */
1.114 + public void write(String str, int off, int len) {
1.115 + buf.append(str.substring(off, off + len));
1.116 + }
1.117 +
1.118 + /**
1.119 + * Appends the specified character sequence to this writer.
1.120 + *
1.121 + * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
1.122 + * behaves in exactly the same way as the invocation
1.123 + *
1.124 + * <pre>
1.125 + * out.write(csq.toString()) </pre>
1.126 + *
1.127 + * <p> Depending on the specification of <tt>toString</tt> for the
1.128 + * character sequence <tt>csq</tt>, the entire sequence may not be
1.129 + * appended. For instance, invoking the <tt>toString</tt> method of a
1.130 + * character buffer will return a subsequence whose content depends upon
1.131 + * the buffer's position and limit.
1.132 + *
1.133 + * @param csq
1.134 + * The character sequence to append. If <tt>csq</tt> is
1.135 + * <tt>null</tt>, then the four characters <tt>"null"</tt> are
1.136 + * appended to this writer.
1.137 + *
1.138 + * @return This writer
1.139 + *
1.140 + * @since 1.5
1.141 + */
1.142 + public StringWriter append(CharSequence csq) {
1.143 + if (csq == null)
1.144 + write("null");
1.145 + else
1.146 + write(csq.toString());
1.147 + return this;
1.148 + }
1.149 +
1.150 + /**
1.151 + * Appends a subsequence of the specified character sequence to this writer.
1.152 + *
1.153 + * <p> An invocation of this method of the form <tt>out.append(csq, start,
1.154 + * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
1.155 + * exactly the same way as the invocation
1.156 + *
1.157 + * <pre>
1.158 + * out.write(csq.subSequence(start, end).toString()) </pre>
1.159 + *
1.160 + * @param csq
1.161 + * The character sequence from which a subsequence will be
1.162 + * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
1.163 + * will be appended as if <tt>csq</tt> contained the four
1.164 + * characters <tt>"null"</tt>.
1.165 + *
1.166 + * @param start
1.167 + * The index of the first character in the subsequence
1.168 + *
1.169 + * @param end
1.170 + * The index of the character following the last character in the
1.171 + * subsequence
1.172 + *
1.173 + * @return This writer
1.174 + *
1.175 + * @throws IndexOutOfBoundsException
1.176 + * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
1.177 + * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
1.178 + * <tt>csq.length()</tt>
1.179 + *
1.180 + * @since 1.5
1.181 + */
1.182 + public StringWriter append(CharSequence csq, int start, int end) {
1.183 + CharSequence cs = (csq == null ? "null" : csq);
1.184 + write(cs.subSequence(start, end).toString());
1.185 + return this;
1.186 + }
1.187 +
1.188 + /**
1.189 + * Appends the specified character to this writer.
1.190 + *
1.191 + * <p> An invocation of this method of the form <tt>out.append(c)</tt>
1.192 + * behaves in exactly the same way as the invocation
1.193 + *
1.194 + * <pre>
1.195 + * out.write(c) </pre>
1.196 + *
1.197 + * @param c
1.198 + * The 16-bit character to append
1.199 + *
1.200 + * @return This writer
1.201 + *
1.202 + * @since 1.5
1.203 + */
1.204 + public StringWriter append(char c) {
1.205 + write(c);
1.206 + return this;
1.207 + }
1.208 +
1.209 + /**
1.210 + * Return the buffer's current value as a string.
1.211 + */
1.212 + public String toString() {
1.213 + return buf.toString();
1.214 + }
1.215 +
1.216 + /**
1.217 + * Return the string buffer itself.
1.218 + *
1.219 + * @return StringBuffer holding the current buffer value.
1.220 + */
1.221 + public StringBuffer getBuffer() {
1.222 + return buf;
1.223 + }
1.224 +
1.225 + /**
1.226 + * Flush the stream.
1.227 + */
1.228 + public void flush() {
1.229 + }
1.230 +
1.231 + /**
1.232 + * Closing a <tt>StringWriter</tt> has no effect. The methods in this
1.233 + * class can be called after the stream has been closed without generating
1.234 + * an <tt>IOException</tt>.
1.235 + */
1.236 + public void close() throws IOException {
1.237 + }
1.238 +
1.239 +}