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