jtulach@1314: /* jtulach@1314: * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved. jtulach@1314: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1314: * jtulach@1314: * This code is free software; you can redistribute it and/or modify it jtulach@1314: * under the terms of the GNU General Public License version 2 only, as jtulach@1314: * published by the Free Software Foundation. Oracle designates this jtulach@1314: * particular file as subject to the "Classpath" exception as provided jtulach@1314: * by Oracle in the LICENSE file that accompanied this code. jtulach@1314: * jtulach@1314: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1314: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1314: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1314: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1314: * accompanied this code). jtulach@1314: * jtulach@1314: * You should have received a copy of the GNU General Public License version jtulach@1314: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1314: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1314: * jtulach@1314: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1314: * or visit www.oracle.com if you need additional information or have any jtulach@1314: * questions. jtulach@1314: */ jtulach@1314: jtulach@1314: package java.io; jtulach@1314: jtulach@1314: jtulach@1314: /** jtulach@1314: * A character stream that collects its output in a string buffer, which can jtulach@1314: * then be used to construct a string. jtulach@1314: *

jtulach@1314: * Closing a StringWriter has no effect. The methods in this class jtulach@1314: * can be called after the stream has been closed without generating an jtulach@1314: * IOException. jtulach@1314: * jtulach@1314: * @author Mark Reinhold jtulach@1314: * @since JDK1.1 jtulach@1314: */ jtulach@1314: jtulach@1314: public class StringWriter extends Writer { jtulach@1314: jtulach@1314: private StringBuffer buf; jtulach@1314: jtulach@1314: /** jtulach@1314: * Create a new string writer using the default initial string-buffer jtulach@1314: * size. jtulach@1314: */ jtulach@1314: public StringWriter() { jtulach@1314: buf = new StringBuffer(); jtulach@1314: lock = buf; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Create a new string writer using the specified initial string-buffer jtulach@1314: * size. jtulach@1314: * jtulach@1314: * @param initialSize jtulach@1314: * The number of char values that will fit into this buffer jtulach@1314: * before it is automatically expanded jtulach@1314: * jtulach@1314: * @throws IllegalArgumentException jtulach@1314: * If initialSize is negative jtulach@1314: */ jtulach@1314: public StringWriter(int initialSize) { jtulach@1314: if (initialSize < 0) { jtulach@1314: throw new IllegalArgumentException("Negative buffer size"); jtulach@1314: } jtulach@1314: buf = new StringBuffer(initialSize); jtulach@1314: lock = buf; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Write a single character. jtulach@1314: */ jtulach@1314: public void write(int c) { jtulach@1314: buf.append((char) c); jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Write a portion of an array of characters. jtulach@1314: * jtulach@1314: * @param cbuf Array of characters jtulach@1314: * @param off Offset from which to start writing characters jtulach@1314: * @param len Number of characters to write jtulach@1314: */ jtulach@1314: public void write(char cbuf[], int off, int len) { jtulach@1314: if ((off < 0) || (off > cbuf.length) || (len < 0) || jtulach@1314: ((off + len) > cbuf.length) || ((off + len) < 0)) { jtulach@1314: throw new IndexOutOfBoundsException(); jtulach@1314: } else if (len == 0) { jtulach@1314: return; jtulach@1314: } jtulach@1314: buf.append(cbuf, off, len); jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Write a string. jtulach@1314: */ jtulach@1314: public void write(String str) { jtulach@1314: buf.append(str); jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Write a portion of a string. jtulach@1314: * jtulach@1314: * @param str String to be written jtulach@1314: * @param off Offset from which to start writing characters jtulach@1314: * @param len Number of characters to write jtulach@1314: */ jtulach@1314: public void write(String str, int off, int len) { jtulach@1314: buf.append(str.substring(off, off + len)); jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Appends the specified character sequence to this writer. jtulach@1314: * jtulach@1314: *

An invocation of this method of the form out.append(csq) jtulach@1314: * behaves in exactly the same way as the invocation jtulach@1314: * jtulach@1314: *

jtulach@1314:      *     out.write(csq.toString()) 
jtulach@1314: * jtulach@1314: *

Depending on the specification of toString for the jtulach@1314: * character sequence csq, the entire sequence may not be jtulach@1314: * appended. For instance, invoking the toString method of a jtulach@1314: * character buffer will return a subsequence whose content depends upon jtulach@1314: * the buffer's position and limit. jtulach@1314: * jtulach@1314: * @param csq jtulach@1314: * The character sequence to append. If csq is jtulach@1314: * null, then the four characters "null" are jtulach@1314: * appended to this writer. jtulach@1314: * jtulach@1314: * @return This writer jtulach@1314: * jtulach@1314: * @since 1.5 jtulach@1314: */ jtulach@1314: public StringWriter append(CharSequence csq) { jtulach@1314: if (csq == null) jtulach@1314: write("null"); jtulach@1314: else jtulach@1314: write(csq.toString()); jtulach@1314: return this; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Appends a subsequence of the specified character sequence to this writer. jtulach@1314: * jtulach@1314: *

An invocation of this method of the form out.append(csq, start, jtulach@1314: * end) when csq is not null, behaves in jtulach@1314: * exactly the same way as the invocation jtulach@1314: * jtulach@1314: *

jtulach@1314:      *     out.write(csq.subSequence(start, end).toString()) 
jtulach@1314: * jtulach@1314: * @param csq jtulach@1314: * The character sequence from which a subsequence will be jtulach@1314: * appended. If csq is null, then characters jtulach@1314: * will be appended as if csq contained the four jtulach@1314: * characters "null". jtulach@1314: * jtulach@1314: * @param start jtulach@1314: * The index of the first character in the subsequence jtulach@1314: * jtulach@1314: * @param end jtulach@1314: * The index of the character following the last character in the jtulach@1314: * subsequence jtulach@1314: * jtulach@1314: * @return This writer jtulach@1314: * jtulach@1314: * @throws IndexOutOfBoundsException jtulach@1314: * If start or end are negative, start jtulach@1314: * is greater than end, or end is greater than jtulach@1314: * csq.length() jtulach@1314: * jtulach@1314: * @since 1.5 jtulach@1314: */ jtulach@1314: public StringWriter append(CharSequence csq, int start, int end) { jtulach@1314: CharSequence cs = (csq == null ? "null" : csq); jtulach@1314: write(cs.subSequence(start, end).toString()); jtulach@1314: return this; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Appends the specified character to this writer. jtulach@1314: * jtulach@1314: *

An invocation of this method of the form out.append(c) jtulach@1314: * behaves in exactly the same way as the invocation jtulach@1314: * jtulach@1314: *

jtulach@1314:      *     out.write(c) 
jtulach@1314: * jtulach@1314: * @param c jtulach@1314: * The 16-bit character to append jtulach@1314: * jtulach@1314: * @return This writer jtulach@1314: * jtulach@1314: * @since 1.5 jtulach@1314: */ jtulach@1314: public StringWriter append(char c) { jtulach@1314: write(c); jtulach@1314: return this; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Return the buffer's current value as a string. jtulach@1314: */ jtulach@1314: public String toString() { jtulach@1314: return buf.toString(); jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Return the string buffer itself. jtulach@1314: * jtulach@1314: * @return StringBuffer holding the current buffer value. jtulach@1314: */ jtulach@1314: public StringBuffer getBuffer() { jtulach@1314: return buf; jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Flush the stream. jtulach@1314: */ jtulach@1314: public void flush() { jtulach@1314: } jtulach@1314: jtulach@1314: /** jtulach@1314: * Closing a StringWriter has no effect. The methods in this jtulach@1314: * class can be called after the stream has been closed without generating jtulach@1314: * an IOException. jtulach@1314: */ jtulach@1314: public void close() throws IOException { jtulach@1314: } jtulach@1314: jtulach@1314: }