/Dependencies/log4net/Util/ReusableStringWriter.cs
C# | 94 lines | 28 code | 8 blank | 58 comment | 1 complexity | bf626b977164836c23b53921c8aa23ba MD5 | raw file
1#region Apache License 2// 3// Licensed to the Apache Software Foundation (ASF) under one or more 4// contributor license agreements. See the NOTICE file distributed with 5// this work for additional information regarding copyright ownership. 6// The ASF licenses this file to you under the Apache License, Version 2.0 7// (the "License"); you may not use this file except in compliance with 8// the License. You may obtain a copy of the License at 9// 10// http://www.apache.org/licenses/LICENSE-2.0 11// 12// Unless required by applicable law or agreed to in writing, software 13// distributed under the License is distributed on an "AS IS" BASIS, 14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15// See the License for the specific language governing permissions and 16// limitations under the License. 17// 18#endregion 19 20using System; 21using System.Text; 22using System.IO; 23 24namespace log4net.Util 25{ 26 /// <summary> 27 /// A <see cref="StringWriter"/> that can be <see cref="Reset"/> and reused 28 /// </summary> 29 /// <remarks> 30 /// <para> 31 /// A <see cref="StringWriter"/> that can be <see cref="Reset"/> and reused. 32 /// This uses a single buffer for string operations. 33 /// </para> 34 /// </remarks> 35 /// <author>Nicko Cadell</author> 36 public class ReusableStringWriter : StringWriter 37 { 38 #region Constructor 39 40 /// <summary> 41 /// Create an instance of <see cref="ReusableStringWriter"/> 42 /// </summary> 43 /// <param name="formatProvider">the format provider to use</param> 44 /// <remarks> 45 /// <para> 46 /// Create an instance of <see cref="ReusableStringWriter"/> 47 /// </para> 48 /// </remarks> 49 public ReusableStringWriter(IFormatProvider formatProvider) : base(formatProvider) 50 { 51 } 52 53 #endregion 54 55 /// <summary> 56 /// Override Dispose to prevent closing of writer 57 /// </summary> 58 /// <param name="disposing">flag</param> 59 /// <remarks> 60 /// <para> 61 /// Override Dispose to prevent closing of writer 62 /// </para> 63 /// </remarks> 64 protected override void Dispose(bool disposing) 65 { 66 // Do not close the writer 67 } 68 69 /// <summary> 70 /// Reset this string writer so that it can be reused. 71 /// </summary> 72 /// <param name="maxCapacity">the maximum buffer capacity before it is trimmed</param> 73 /// <param name="defaultSize">the default size to make the buffer</param> 74 /// <remarks> 75 /// <para> 76 /// Reset this string writer so that it can be reused. 77 /// The internal buffers are cleared and reset. 78 /// </para> 79 /// </remarks> 80 public void Reset(int maxCapacity, int defaultSize) 81 { 82 // Reset working string buffer 83 StringBuilder sb = this.GetStringBuilder(); 84 85 sb.Length = 0; 86 87 // Check if over max size 88 if (sb.Capacity > maxCapacity) 89 { 90 sb.Capacity = defaultSize; 91 } 92 } 93 } 94}