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