StringBuilderWriter.java

  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. import java.io.Serializable;
  19. import java.io.StringWriter;
  20. import java.io.Writer;

  21. /**
  22.  * {@link Writer} implementation that outputs to a {@link StringBuilder}.
  23.  * <p>
  24.  * <strong>NOTE:</strong> This implementation, as an alternative to {@link StringWriter}, provides an <em>un-synchronized</em> implementation for better
  25.  * performance for use in a single thread. For safe usage with multiple {@link Thread}s, a {@link StringWriter} should be used.
  26.  * </p>
  27.  * <h2>Deprecating Serialization</h2>
  28.  * <p>
  29.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  30.  * </p>
  31.  *
  32.  * @since 2.0
  33.  */
  34. public class StringBuilderWriter extends Writer implements Serializable {

  35.     private static final long serialVersionUID = -146927496096066153L;

  36.     /** The append target. */
  37.     private final StringBuilder builder;

  38.     /**
  39.      * Constructs a new {@link StringBuilder} instance with default capacity.
  40.      */
  41.     public StringBuilderWriter() {
  42.         this.builder = new StringBuilder();
  43.     }

  44.     /**
  45.      * Constructs a new {@link StringBuilder} instance with the specified capacity.
  46.      *
  47.      * @param capacity The initial capacity of the underlying {@link StringBuilder}
  48.      */
  49.     public StringBuilderWriter(final int capacity) {
  50.         this.builder = new StringBuilder(capacity);
  51.     }

  52.     /**
  53.      * Constructs a new instance with the specified {@link StringBuilder}.
  54.      *
  55.      * <p>If {@code builder} is null a new instance with default capacity will be created.</p>
  56.      *
  57.      * @param builder The String builder. May be null.
  58.      */
  59.     public StringBuilderWriter(final StringBuilder builder) {
  60.         this.builder = builder != null ? builder : new StringBuilder();
  61.     }

  62.     /**
  63.      * Appends a single character to this Writer.
  64.      *
  65.      * @param value The character to append
  66.      * @return This writer instance
  67.      */
  68.     @Override
  69.     public Writer append(final char value) {
  70.         builder.append(value);
  71.         return this;
  72.     }

  73.     /**
  74.      * Appends a character sequence to this Writer.
  75.      *
  76.      * @param value The character to append
  77.      * @return This writer instance
  78.      */
  79.     @Override
  80.     public Writer append(final CharSequence value) {
  81.         builder.append(value);
  82.         return this;
  83.     }

  84.     /**
  85.      * Appends a portion of a character sequence to the {@link StringBuilder}.
  86.      *
  87.      * @param value The character to append
  88.      * @param start The index of the first character
  89.      * @param end The index of the last character + 1
  90.      * @return This writer instance
  91.      */
  92.     @Override
  93.     public Writer append(final CharSequence value, final int start, final int end) {
  94.         builder.append(value, start, end);
  95.         return this;
  96.     }

  97.     /**
  98.      * Closing this writer has no effect.
  99.      */
  100.     @Override
  101.     public void close() {
  102.         // no-op
  103.     }

  104.     /**
  105.      * Flushing this writer has no effect.
  106.      */
  107.     @Override
  108.     public void flush() {
  109.         // no-op
  110.     }

  111.     /**
  112.      * Gets the underlying builder.
  113.      *
  114.      * @return The underlying builder
  115.      */
  116.     public StringBuilder getBuilder() {
  117.         return builder;
  118.     }

  119.     /**
  120.      * Returns {@link StringBuilder#toString()}.
  121.      *
  122.      * @return The contents of the String builder.
  123.      */
  124.     @Override
  125.     public String toString() {
  126.         return builder.toString();
  127.     }

  128.     /**
  129.      * Writes a portion of a character array to the {@link StringBuilder}.
  130.      *
  131.      * @param value The value to write
  132.      * @param offset The index of the first character
  133.      * @param length The number of characters to write
  134.      */
  135.     @Override
  136.     public void write(final char[] value, final int offset, final int length) {
  137.         if (value != null) {
  138.             builder.append(value, offset, length);
  139.         }
  140.     }

  141.     /**
  142.      * Writes a String to the {@link StringBuilder}.
  143.      *
  144.      * @param value The value to write
  145.      */
  146.     @Override
  147.     public void write(final String value) {
  148.         if (value != null) {
  149.             builder.append(value);
  150.         }
  151.     }
  152. }