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.directory.api.util;
18  
19  
20  import java.io.Writer;
21  
22  
23  /**
24   * {@link Writer} implementation that outputs to a {@link StringBuilder}.
25   * <p>
26   * <strong>NOTE:</strong> This implementation, as an alternative to
27   * <code>java.io.StringWriter</code>, provides an <i>un-synchronized</i>
28   * (i.e. for use in a single thread) implementation for better performance.
29   * For safe usage with multiple {@link Thread}s then
30   * <code>java.io.StringWriter</code> should be used.
31   *
32   * @version $Id: StringBuilderWriter.java 1415850 2012-11-30 20:51:39Z ggregory $
33   * @since 2.0
34   */
35  public class StringBuilderWriter extends Writer
36  {
37  
38      private final StringBuilder builder;
39  
40  
41      /**
42       * Construct a new {@link StringBuilder} instance with default capacity.
43       */
44      public StringBuilderWriter()
45      {
46          builder = new StringBuilder();
47      }
48  
49  
50      /**
51       * Construct a new {@link StringBuilder} instance with the specified capacity.
52       *
53       * @param capacity The initial capacity of the underlying {@link StringBuilder}
54       */
55      public StringBuilderWriter( int capacity )
56      {
57          builder = new StringBuilder( capacity );
58      }
59  
60  
61      /**
62       * Construct a new instance with the specified {@link StringBuilder}.
63       *
64       * @param builder The String builder
65       */
66      public StringBuilderWriter( StringBuilder builder )
67      {
68          this.builder = builder != null ? builder : new StringBuilder();
69      }
70  
71  
72      /**
73       * Append a single character to this Writer.
74       *
75       * @param value The character to append
76       * @return This writer instance
77       */
78      @Override
79      public Writer append( char value )
80      {
81          builder.append( value );
82          
83          return this;
84      }
85  
86  
87      /**
88       * Append a character sequence to this Writer.
89       *
90       * @param value The character to append
91       * @return This writer instance
92       */
93      @Override
94      public Writer append( CharSequence value )
95      {
96          builder.append( value );
97          
98          return this;
99      }
100 
101 
102     /**
103      * Append a portion of a character sequence to the {@link StringBuilder}.
104      *
105      * @param value The character to append
106      * @param start The index of the first character
107      * @param end The index of the last character + 1
108      * @return This writer instance
109      */
110     @Override
111     public Writer append( CharSequence value, int start, int end )
112     {
113         builder.append( value, start, end );
114         
115         return this;
116     }
117 
118 
119     /**
120      * Closing this writer has no effect. 
121      */
122     @Override
123     public void close()
124     {
125     }
126 
127 
128     /**
129      * Flushing this writer has no effect. 
130      */
131     @Override
132     public void flush()
133     {
134     }
135 
136 
137     /**
138      * Write a String to the {@link StringBuilder}.
139      * 
140      * @param value The value to write
141      */
142     @Override
143     public void write( String value )
144     {
145         if ( value != null )
146         {
147             builder.append( value );
148         }
149     }
150 
151 
152     /**
153      * Write a portion of a character array to the {@link StringBuilder}.
154      *
155      * @param value The value to write
156      * @param offset The index of the first character
157      * @param length The number of characters to write
158      */
159     @Override
160     public void write( char[] value, int offset, int length )
161     {
162         if ( value != null )
163         {
164             builder.append( value, offset, length );
165         }
166     }
167 
168 
169     /**
170      * Return the underlying builder.
171      *
172      * @return The underlying builder
173      */
174     public StringBuilder getBuilder()
175     {
176         return builder;
177     }
178 
179 
180     /**
181      * Returns {@link StringBuilder#toString()}.
182      *
183      * @return The contents of the String builder.
184      */
185     @Override
186     public String toString()
187     {
188         return builder.toString();
189     }
190 }