View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.io.output;
18  
19  import java.io.Serializable;
20  import java.io.Writer;
21  
22  /**
23   * {@link Writer} implementation that outputs to a {@link StringBuilder}.
24   * <p>
25   * <strong>NOTE:</strong> This implementation, as an alternative to
26   * <code>java.io.StringWriter</code>, provides an <i>un-synchronized</i>
27   * (i.e. for use in a single thread) implementation for better performance.
28   * For safe usage with multiple {@link Thread}s then
29   * <code>java.io.StringWriter</code> should be used.
30   *
31   * @version $Id: StringBuilderWriter.java 1722253 2015-12-30 00:36:12Z ggregory $
32   * @since 2.0
33   */
34  public class StringBuilderWriter extends Writer implements Serializable {
35  
36      private static final long serialVersionUID = -146927496096066153L;
37      private final StringBuilder builder;
38  
39      /**
40       * Constructs a new {@link StringBuilder} instance with default capacity.
41       */
42      public StringBuilderWriter() {
43          this.builder = new StringBuilder();
44      }
45  
46      /**
47       * Constructs a new {@link StringBuilder} instance with the specified capacity.
48       *
49       * @param capacity The initial capacity of the underlying {@link StringBuilder}
50       */
51      public StringBuilderWriter(final int capacity) {
52          this.builder = new StringBuilder(capacity);
53      }
54  
55      /**
56       * Constructs a new instance with the specified {@link StringBuilder}.
57       * 
58       * <p>If {@code builder} is null a new instance with default capacity will be created.</p>
59       *
60       * @param builder The String builder. May be null.
61       */
62      public StringBuilderWriter(final StringBuilder builder) {
63          this.builder = builder != null ? builder : new StringBuilder();
64      }
65  
66      /**
67       * Appends a single character to this Writer.
68       *
69       * @param value The character to append
70       * @return This writer instance
71       */
72      @Override
73      public Writer append(final char value) {
74          builder.append(value);
75          return this;
76      }
77  
78      /**
79       * Appends a character sequence to this Writer.
80       *
81       * @param value The character to append
82       * @return This writer instance
83       */
84      @Override
85      public Writer append(final CharSequence value) {
86          builder.append(value);
87          return this;
88      }
89  
90      /**
91       * Appends a portion of a character sequence to the {@link StringBuilder}.
92       *
93       * @param value The character to append
94       * @param start The index of the first character
95       * @param end The index of the last character + 1
96       * @return This writer instance
97       */
98      @Override
99      public Writer append(final CharSequence value, final int start, final int end) {
100         builder.append(value, start, end);
101         return this;
102     }
103 
104     /**
105      * Closing this writer has no effect. 
106      */
107     @Override
108     public void close() {
109         // no-op
110     }
111 
112     /**
113      * Flushing this writer has no effect. 
114      */
115     @Override
116     public void flush() {
117         // no-op
118     }
119 
120 
121     /**
122      * Writes a String to the {@link StringBuilder}.
123      * 
124      * @param value The value to write
125      */
126     @Override
127     public void write(final String value) {
128         if (value != null) {
129             builder.append(value);
130         }
131     }
132 
133     /**
134      * Writes a portion of a character array to the {@link StringBuilder}.
135      *
136      * @param value The value to write
137      * @param offset The index of the first character
138      * @param length The number of characters to write
139      */
140     @Override
141     public void write(final char[] value, final int offset, final int length) {
142         if (value != null) {
143             builder.append(value, offset, length);
144         }
145     }
146 
147     /**
148      * Returns the underlying builder.
149      *
150      * @return The underlying builder
151      */
152     public StringBuilder getBuilder() {
153         return builder;
154     }
155 
156     /**
157      * Returns {@link StringBuilder#toString()}.
158      *
159      * @return The contents of the String builder.
160      */
161     @Override
162     public String toString() {
163         return builder.toString();
164     }
165 }