1 // downloaded from https://commons.apache.org/proper/commons-io/apidocs/src-html/org/apache/commons/io/output/StringBuilderWriter.html
2 // by knoxg on 2019-08-05
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 package com.randomnoun.common.io;
21
22 import java.io.Serializable;
23 import java.io.Writer;
24
25 /**
26 * {@link Writer} implementation that outputs to a {@link StringBuilder}.
27 * <p>
28 * <strong>NOTE:</strong> This implementation, as an alternative to
29 * <code>java.io.StringWriter</code>, provides an <i>un-synchronized</i>
30 * (i.e. for use in a single thread) implementation for better performance.
31 * For safe usage with multiple {@link Thread}s then
32 * <code>java.io.StringWriter</code> should be used.
33 *
34 * @since 2.0
35 */
36 public class StringBuilderWriter extends Writer implements Serializable {
37
38 private static final long serialVersionUID = -146927496096066153L;
39 private final StringBuilder builder;
40
41 /**
42 * Constructs a new {@link StringBuilder} instance with default capacity.
43 */
44 public StringBuilderWriter() {
45 this.builder = new StringBuilder();
46 }
47
48 /**
49 * Constructs a new {@link StringBuilder} instance with the specified capacity.
50 *
51 * @param capacity The initial capacity of the underlying {@link StringBuilder}
52 */
53 public StringBuilderWriter(final int capacity) {
54 this.builder = new StringBuilder(capacity);
55 }
56
57 /**
58 * Constructs a new instance with the specified {@link StringBuilder}.
59 *
60 * <p>If {@code builder} is null a new instance with default capacity will be created.</p>
61 *
62 * @param builder The String builder. May be null.
63 */
64 public StringBuilderWriter(final StringBuilder builder) {
65 this.builder = builder != null ? builder : new StringBuilder();
66 }
67
68 /**
69 * Appends a single character to this Writer.
70 *
71 * @param value The character to append
72 * @return This writer instance
73 */
74 @Override
75 public Writer append(final char value) {
76 builder.append(value);
77 return this;
78 }
79
80 /**
81 * Appends a character sequence to this Writer.
82 *
83 * @param value The character to append
84 * @return This writer instance
85 */
86 @Override
87 public Writer append(final CharSequence value) {
88 builder.append(value);
89 return this;
90 }
91
92 /**
93 * Appends a portion of a character sequence to the {@link StringBuilder}.
94 *
95 * @param value The character to append
96 * @param start The index of the first character
97 * @param end The index of the last character + 1
98 * @return This writer instance
99 */
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 }