001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.io.output;
018
019import java.io.Serializable;
020import java.io.StringWriter;
021import java.io.Writer;
022
023/**
024 * {@link Writer} implementation that outputs to a {@link StringBuilder}.
025 * <p>
026 * <strong>NOTE:</strong> This implementation, as an alternative to {@link StringWriter}, provides an <em>un-synchronized</em> implementation for better
027 * performance for use in a single thread. For safe usage with multiple {@link Thread}s, a {@link StringWriter} should be used.
028 * </p>
029 * <h2>Deprecating Serialization</h2>
030 * <p>
031 * <em>Serialization is deprecated and will be removed in 3.0.</em>
032 * </p>
033 *
034 * @since 2.0
035 */
036public class StringBuilderWriter extends Writer implements Serializable {
037
038    private static final long serialVersionUID = -146927496096066153L;
039
040    /** The append target. */
041    private final StringBuilder builder;
042
043    /**
044     * Constructs a new {@link StringBuilder} instance with default capacity.
045     */
046    public StringBuilderWriter() {
047        this.builder = new StringBuilder();
048    }
049
050    /**
051     * Constructs a new {@link StringBuilder} instance with the specified capacity.
052     *
053     * @param capacity The initial capacity of the underlying {@link StringBuilder}
054     */
055    public StringBuilderWriter(final int capacity) {
056        this.builder = new StringBuilder(capacity);
057    }
058
059    /**
060     * Constructs a new instance with the specified {@link StringBuilder}.
061     *
062     * <p>If {@code builder} is null a new instance with default capacity will be created.</p>
063     *
064     * @param builder The String builder. May be null.
065     */
066    public StringBuilderWriter(final StringBuilder builder) {
067        this.builder = builder != null ? builder : new StringBuilder();
068    }
069
070    /**
071     * Appends a single character to this Writer.
072     *
073     * @param value The character to append
074     * @return This writer instance
075     */
076    @Override
077    public Writer append(final char value) {
078        builder.append(value);
079        return this;
080    }
081
082    /**
083     * Appends a character sequence to this Writer.
084     *
085     * @param value The character to append
086     * @return This writer instance
087     */
088    @Override
089    public Writer append(final CharSequence value) {
090        builder.append(value);
091        return this;
092    }
093
094    /**
095     * Appends a portion of a character sequence to the {@link StringBuilder}.
096     *
097     * @param value The character to append
098     * @param start The index of the first character
099     * @param end The index of the last character + 1
100     * @return This writer instance
101     */
102    @Override
103    public Writer append(final CharSequence value, final int start, final int end) {
104        builder.append(value, start, end);
105        return this;
106    }
107
108    /**
109     * Closing this writer has no effect.
110     */
111    @Override
112    public void close() {
113        // no-op
114    }
115
116    /**
117     * Flushing this writer has no effect.
118     */
119    @Override
120    public void flush() {
121        // no-op
122    }
123
124    /**
125     * Gets the underlying builder.
126     *
127     * @return The underlying builder
128     */
129    public StringBuilder getBuilder() {
130        return builder;
131    }
132
133    /**
134     * Returns {@link StringBuilder#toString()}.
135     *
136     * @return The contents of the String builder.
137     */
138    @Override
139    public String toString() {
140        return builder.toString();
141    }
142
143    /**
144     * Writes a portion of a character array to the {@link StringBuilder}.
145     *
146     * @param value The value to write
147     * @param offset The index of the first character
148     * @param length The number of characters to write
149     */
150    @Override
151    public void write(final char[] value, final int offset, final int length) {
152        if (value != null) {
153            builder.append(value, offset, length);
154        }
155    }
156
157    /**
158     * Writes a String to the {@link StringBuilder}.
159     *
160     * @param value The value to write
161     */
162    @Override
163    public void write(final String value) {
164        if (value != null) {
165            builder.append(value);
166        }
167    }
168}