TextWriterAdapter.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. using System.Globalization;
  23. namespace log4net.Util
  24. {
  25. /// <summary>
  26. /// Adapter that extends <see cref="TextWriter"/> and forwards all
  27. /// messages to an instance of <see cref="TextWriter"/>.
  28. /// </summary>
  29. /// <remarks>
  30. /// <para>
  31. /// Adapter that extends <see cref="TextWriter"/> and forwards all
  32. /// messages to an instance of <see cref="TextWriter"/>.
  33. /// </para>
  34. /// </remarks>
  35. /// <author>Nicko Cadell</author>
  36. public abstract class TextWriterAdapter : TextWriter
  37. {
  38. #region Private Member Variables
  39. /// <summary>
  40. /// The writer to forward messages to
  41. /// </summary>
  42. private TextWriter m_writer;
  43. #endregion
  44. #region Constructors
  45. /// <summary>
  46. /// Create an instance of <see cref="TextWriterAdapter"/> that forwards all
  47. /// messages to a <see cref="TextWriter"/>.
  48. /// </summary>
  49. /// <param name="writer">The <see cref="TextWriter"/> to forward to</param>
  50. /// <remarks>
  51. /// <para>
  52. /// Create an instance of <see cref="TextWriterAdapter"/> that forwards all
  53. /// messages to a <see cref="TextWriter"/>.
  54. /// </para>
  55. /// </remarks>
  56. protected TextWriterAdapter(TextWriter writer) : base(CultureInfo.InvariantCulture)
  57. {
  58. m_writer = writer;
  59. }
  60. #endregion
  61. #region Protected Instance Properties
  62. /// <summary>
  63. /// Gets or sets the underlying <see cref="TextWriter" />.
  64. /// </summary>
  65. /// <value>
  66. /// The underlying <see cref="TextWriter" />.
  67. /// </value>
  68. /// <remarks>
  69. /// <para>
  70. /// Gets or sets the underlying <see cref="TextWriter" />.
  71. /// </para>
  72. /// </remarks>
  73. protected TextWriter Writer
  74. {
  75. get { return m_writer; }
  76. set { m_writer = value; }
  77. }
  78. #endregion Protected Instance Properties
  79. #region Public Properties
  80. /// <summary>
  81. /// The Encoding in which the output is written
  82. /// </summary>
  83. /// <value>
  84. /// The <see cref="Encoding"/>
  85. /// </value>
  86. /// <remarks>
  87. /// <para>
  88. /// The Encoding in which the output is written
  89. /// </para>
  90. /// </remarks>
  91. override public Encoding Encoding
  92. {
  93. get { return m_writer.Encoding; }
  94. }
  95. /// <summary>
  96. /// Gets an object that controls formatting
  97. /// </summary>
  98. /// <value>
  99. /// The format provider
  100. /// </value>
  101. /// <remarks>
  102. /// <para>
  103. /// Gets an object that controls formatting
  104. /// </para>
  105. /// </remarks>
  106. override public IFormatProvider FormatProvider
  107. {
  108. get { return m_writer.FormatProvider; }
  109. }
  110. /// <summary>
  111. /// Gets or sets the line terminator string used by the TextWriter
  112. /// </summary>
  113. /// <value>
  114. /// The line terminator to use
  115. /// </value>
  116. /// <remarks>
  117. /// <para>
  118. /// Gets or sets the line terminator string used by the TextWriter
  119. /// </para>
  120. /// </remarks>
  121. override public String NewLine
  122. {
  123. get { return m_writer.NewLine; }
  124. set { m_writer.NewLine = value; }
  125. }
  126. #endregion
  127. #region Public Methods
  128. /// <summary>
  129. /// Closes the writer and releases any system resources associated with the writer
  130. /// </summary>
  131. /// <remarks>
  132. /// <para>
  133. /// </para>
  134. /// </remarks>
  135. #if NETSTANDARD1_3
  136. virtual public void Close()
  137. {
  138. m_writer.Dispose();
  139. }
  140. #else
  141. override public void Close()
  142. {
  143. m_writer.Close();
  144. }
  145. #endif
  146. /// <summary>
  147. /// Dispose this writer
  148. /// </summary>
  149. /// <param name="disposing">flag indicating if we are being disposed</param>
  150. /// <remarks>
  151. /// <para>
  152. /// Dispose this writer
  153. /// </para>
  154. /// </remarks>
  155. override protected void Dispose(bool disposing)
  156. {
  157. if (disposing)
  158. {
  159. ((IDisposable)m_writer).Dispose();
  160. }
  161. }
  162. /// <summary>
  163. /// Flushes any buffered output
  164. /// </summary>
  165. /// <remarks>
  166. /// <para>
  167. /// Clears all buffers for the writer and causes any buffered data to be written
  168. /// to the underlying device
  169. /// </para>
  170. /// </remarks>
  171. override public void Flush()
  172. {
  173. m_writer.Flush();
  174. }
  175. /// <summary>
  176. /// Writes a character to the wrapped TextWriter
  177. /// </summary>
  178. /// <param name="value">the value to write to the TextWriter</param>
  179. /// <remarks>
  180. /// <para>
  181. /// Writes a character to the wrapped TextWriter
  182. /// </para>
  183. /// </remarks>
  184. override public void Write(char value)
  185. {
  186. m_writer.Write(value);
  187. }
  188. /// <summary>
  189. /// Writes a character buffer to the wrapped TextWriter
  190. /// </summary>
  191. /// <param name="buffer">the data buffer</param>
  192. /// <param name="index">the start index</param>
  193. /// <param name="count">the number of characters to write</param>
  194. /// <remarks>
  195. /// <para>
  196. /// Writes a character buffer to the wrapped TextWriter
  197. /// </para>
  198. /// </remarks>
  199. override public void Write(char[] buffer, int index, int count)
  200. {
  201. m_writer.Write(buffer, index, count);
  202. }
  203. /// <summary>
  204. /// Writes a string to the wrapped TextWriter
  205. /// </summary>
  206. /// <param name="value">the value to write to the TextWriter</param>
  207. /// <remarks>
  208. /// <para>
  209. /// Writes a string to the wrapped TextWriter
  210. /// </para>
  211. /// </remarks>
  212. override public void Write(String value)
  213. {
  214. m_writer.Write(value);
  215. }
  216. #endregion
  217. }
  218. }