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 * <h5 class='section'>Notes:</h5><ul> 021 * <li class='warn'>This class is not thread safe. 022 * </ul> 023 * 024 * <h5 class='section'>See Also:</h5><ul> 025 026 * </ul> 027 */ 028public final class StringBuilderWriter extends Writer { 029 030 private StringBuilder sb; 031 032 /** 033 * Create a new string writer using the default initial string-builder size. 034 */ 035 public StringBuilderWriter() { 036 sb = new StringBuilder(); 037 lock = null; 038 } 039 040 /** 041 * Create a new string writer around an existing string builder. 042 * 043 * @param sb The string builder being wrapped. 044 */ 045 public StringBuilderWriter(StringBuilder sb) { 046 this.sb = sb; 047 lock = null; 048 } 049 050 /** 051 * Create a new string writer using the specified initial string-builder size. 052 * 053 * @param initialSize 054 * The number of <tt>char</tt> values that will fit into this buffer before it is automatically expanded. 055 * @throws IllegalArgumentException If <tt>initialSize</tt> is negative. 056 */ 057 public StringBuilderWriter(int initialSize) { 058 sb = new StringBuilder(initialSize); 059 lock = null; 060 } 061 062 @Override /* Writer */ 063 public void write(int c) { 064 sb.append((char) c); 065 } 066 067 @Override /* Writer */ 068 public void write(char cbuf[], int start, int length) { 069 sb.append(cbuf, start, length); 070 } 071 072 @Override /* Writer */ 073 public void write(String str) { 074 sb.append(str); 075 } 076 077 @Override /* Writer */ 078 public void write(String str, int off, int len) { 079 sb.append(str.substring(off, off + len)); 080 } 081 082 @Override /* Writer */ 083 public StringBuilderWriter append(CharSequence csq) { 084 if (csq == null) 085 write("null"); 086 else 087 write(csq.toString()); 088 return this; 089 } 090 091 @Override /* Writer */ 092 public StringBuilderWriter append(CharSequence csq, int start, int end) { 093 CharSequence cs = (csq == null ? "null" : csq); 094 write(cs.subSequence(start, end).toString()); 095 return this; 096 } 097 098 @Override /* Writer */ 099 public StringBuilderWriter append(char c) { 100 write(c); 101 return this; 102 } 103 104 @Override /* Object */ 105 public String toString() { 106 return sb.toString(); 107 } 108 109 @Override /* Writer */ 110 public void flush() {} 111 112 @Override /* Writer */ 113 public void close() throws IOException {} 114}