0N/A/*
2362N/A * Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/A
0N/A/**
0N/A * A character stream that collects its output in a string buffer, which can
0N/A * then be used to construct a string.
0N/A * <p>
0N/A * Closing a <tt>StringWriter</tt> has no effect. The methods in this class
0N/A * can be called after the stream has been closed without generating an
0N/A * <tt>IOException</tt>.
0N/A *
0N/A * @author Mark Reinhold
0N/A * @since JDK1.1
0N/A */
0N/A
0N/Apublic class StringWriter extends Writer {
0N/A
0N/A private StringBuffer buf;
0N/A
0N/A /**
0N/A * Create a new string writer using the default initial string-buffer
0N/A * size.
0N/A */
0N/A public StringWriter() {
0N/A buf = new StringBuffer();
0N/A lock = buf;
0N/A }
0N/A
0N/A /**
0N/A * Create a new string writer using the specified initial string-buffer
0N/A * size.
0N/A *
0N/A * @param initialSize
0N/A * The number of <tt>char</tt> values that will fit into this buffer
0N/A * before it is automatically expanded
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If <tt>initialSize</tt> is negative
0N/A */
0N/A public StringWriter(int initialSize) {
0N/A if (initialSize < 0) {
0N/A throw new IllegalArgumentException("Negative buffer size");
0N/A }
0N/A buf = new StringBuffer(initialSize);
0N/A lock = buf;
0N/A }
0N/A
0N/A /**
0N/A * Write a single character.
0N/A */
0N/A public void write(int c) {
0N/A buf.append((char) c);
0N/A }
0N/A
0N/A /**
0N/A * Write a portion of an array of characters.
0N/A *
0N/A * @param cbuf Array of characters
0N/A * @param off Offset from which to start writing characters
0N/A * @param len Number of characters to write
0N/A */
0N/A public void write(char cbuf[], int off, int len) {
0N/A if ((off < 0) || (off > cbuf.length) || (len < 0) ||
0N/A ((off + len) > cbuf.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A } else if (len == 0) {
0N/A return;
0N/A }
0N/A buf.append(cbuf, off, len);
0N/A }
0N/A
0N/A /**
0N/A * Write a string.
0N/A */
0N/A public void write(String str) {
0N/A buf.append(str);
0N/A }
0N/A
0N/A /**
0N/A * Write a portion of a string.
0N/A *
0N/A * @param str String to be written
0N/A * @param off Offset from which to start writing characters
0N/A * @param len Number of characters to write
0N/A */
0N/A public void write(String str, int off, int len) {
0N/A buf.append(str.substring(off, off + len));
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified character sequence to this writer.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.write(csq.toString()) </pre>
0N/A *
0N/A * <p> Depending on the specification of <tt>toString</tt> for the
0N/A * character sequence <tt>csq</tt>, the entire sequence may not be
0N/A * appended. For instance, invoking the <tt>toString</tt> method of a
0N/A * character buffer will return a subsequence whose content depends upon
0N/A * the buffer's position and limit.
0N/A *
0N/A * @param csq
0N/A * The character sequence to append. If <tt>csq</tt> is
0N/A * <tt>null</tt>, then the four characters <tt>"null"</tt> are
0N/A * appended to this writer.
0N/A *
0N/A * @return This writer
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public StringWriter append(CharSequence csq) {
0N/A if (csq == null)
0N/A write("null");
0N/A else
0N/A write(csq.toString());
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends a subsequence of the specified character sequence to this writer.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(csq, start,
0N/A * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in
0N/A * exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.write(csq.subSequence(start, end).toString()) </pre>
0N/A *
0N/A * @param csq
0N/A * The character sequence from which a subsequence will be
0N/A * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
0N/A * will be appended as if <tt>csq</tt> contained the four
0N/A * characters <tt>"null"</tt>.
0N/A *
0N/A * @param start
0N/A * The index of the first character in the subsequence
0N/A *
0N/A * @param end
0N/A * The index of the character following the last character in the
0N/A * subsequence
0N/A *
0N/A * @return This writer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
0N/A * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
0N/A * <tt>csq.length()</tt>
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public StringWriter append(CharSequence csq, int start, int end) {
0N/A CharSequence cs = (csq == null ? "null" : csq);
0N/A write(cs.subSequence(start, end).toString());
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Appends the specified character to this writer.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>out.append(c)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * out.write(c) </pre>
0N/A *
0N/A * @param c
0N/A * The 16-bit character to append
0N/A *
0N/A * @return This writer
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public StringWriter append(char c) {
0N/A write(c);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Return the buffer's current value as a string.
0N/A */
0N/A public String toString() {
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Return the string buffer itself.
0N/A *
0N/A * @return StringBuffer holding the current buffer value.
0N/A */
0N/A public StringBuffer getBuffer() {
0N/A return buf;
0N/A }
0N/A
0N/A /**
0N/A * Flush the stream.
0N/A */
0N/A public void flush() {
0N/A }
0N/A
0N/A /**
0N/A * Closing a <tt>StringWriter</tt> has no effect. The methods in this
0N/A * class can be called after the stream has been closed without generating
0N/A * an <tt>IOException</tt>.
0N/A */
0N/A public void close() throws IOException {
0N/A }
0N/A
0N/A}