001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import java.io.*;
016
017/**
018 * Similar to {@link StringWriter}, but uses a {@link StringBuilder} instead to avoid synchronization overhead.
019 *
020 * <p>
021 * Note that this class is NOT thread safe.
022 */
023public final class StringBuilderWriter extends Writer {
024
025   private StringBuilder sb;
026
027   /**
028    * Create a new string writer using the default initial string-builder size.
029    */
030   public StringBuilderWriter() {
031      sb = new StringBuilder();
032      lock = null;
033   }
034
035   /**
036    * Create a new string writer around an existing string builder.
037    *
038    * @param sb The string builder being wrapped.
039    */
040   public StringBuilderWriter(StringBuilder sb) {
041      this.sb = sb;
042      lock = null;
043   }
044
045   /**
046    * Create a new string writer using the specified initial string-builder size.
047    *
048    * @param initialSize
049    *    The number of <tt>char</tt> values that will fit into this buffer before it is automatically expanded.
050    * @throws IllegalArgumentException If <tt>initialSize</tt> is negative.
051    */
052   public StringBuilderWriter(int initialSize) {
053      sb = new StringBuilder(initialSize);
054      lock = null;
055   }
056
057   @Override /* Writer */
058   public void write(int c) {
059      sb.append((char) c);
060   }
061
062   @Override /* Writer */
063   public void write(char cbuf[], int start, int length) {
064      sb.append(cbuf, start, length);
065   }
066
067   @Override /* Writer */
068   public void write(String str) {
069      sb.append(str);
070   }
071
072   @Override /* Writer */
073   public void write(String str, int off, int len) {
074      sb.append(str.substring(off, off + len));
075   }
076
077   @Override /* Writer */
078   public StringBuilderWriter append(CharSequence csq) {
079      if (csq == null)
080         write("null");
081      else
082         write(csq.toString());
083      return this;
084   }
085
086   @Override /* Writer */
087   public StringBuilderWriter append(CharSequence csq, int start, int end) {
088      CharSequence cs = (csq == null ? "null" : csq);
089      write(cs.subSequence(start, end).toString());
090      return this;
091   }
092
093   @Override /* Writer */
094   public StringBuilderWriter append(char c) {
095      write(c);
096      return this;
097   }
098
099   @Override /* Object */
100   public String toString() {
101      return sb.toString();
102   }
103
104   @Override /* Writer */
105   public void flush() {}
106
107   @Override /* Writer */
108   public void close() throws IOException {}
109}