ReusableStringWriter.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. using System;
  20. using System.Text;
  21. using System.IO;
  22. namespace log4net.Util
  23. {
  24. /// <summary>
  25. /// A <see cref="StringWriter"/> that can be <see cref="Reset"/> and reused
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// A <see cref="StringWriter"/> that can be <see cref="Reset"/> and reused.
  30. /// This uses a single buffer for string operations.
  31. /// </para>
  32. /// </remarks>
  33. /// <author>Nicko Cadell</author>
  34. public class ReusableStringWriter : StringWriter
  35. {
  36. #region Constructor
  37. /// <summary>
  38. /// Create an instance of <see cref="ReusableStringWriter"/>
  39. /// </summary>
  40. /// <param name="formatProvider">the format provider to use</param>
  41. /// <remarks>
  42. /// <para>
  43. /// Create an instance of <see cref="ReusableStringWriter"/>
  44. /// </para>
  45. /// </remarks>
  46. public ReusableStringWriter(IFormatProvider formatProvider) : base(formatProvider)
  47. {
  48. }
  49. #endregion
  50. /// <summary>
  51. /// Override Dispose to prevent closing of writer
  52. /// </summary>
  53. /// <param name="disposing">flag</param>
  54. /// <remarks>
  55. /// <para>
  56. /// Override Dispose to prevent closing of writer
  57. /// </para>
  58. /// </remarks>
  59. protected override void Dispose(bool disposing)
  60. {
  61. // Do not close the writer
  62. }
  63. /// <summary>
  64. /// Reset this string writer so that it can be reused.
  65. /// </summary>
  66. /// <param name="maxCapacity">the maximum buffer capacity before it is trimmed</param>
  67. /// <param name="defaultSize">the default size to make the buffer</param>
  68. /// <remarks>
  69. /// <para>
  70. /// Reset this string writer so that it can be reused.
  71. /// The internal buffers are cleared and reset.
  72. /// </para>
  73. /// </remarks>
  74. public void Reset(int maxCapacity, int defaultSize)
  75. {
  76. // Reset working string buffer
  77. StringBuilder sb = this.GetStringBuilder();
  78. sb.Length = 0;
  79. // Check if over max size
  80. if (sb.Capacity > maxCapacity)
  81. {
  82. sb.Capacity = defaultSize;
  83. }
  84. }
  85. }
  86. }