PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/StringTextWriter.cs

#
C# | 128 lines | 90 code | 21 blank | 17 comment | 2 complexity | 1e2b200cc75d6ad7ea146ede5d530cb3 MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // This is free software licensed under the NUnit license. You
  3. // may obtain a copy of the license as well as information regarding
  4. // copyright ownership at http://nunit.org.
  5. // ****************************************************************
  6. using System.IO;
  7. using System.Text;
  8. namespace NUnit.Core
  9. {
  10. // TODO: This class is not currently being used. Review to
  11. // see if we will use it again, otherwise drop it.
  12. #region StringTextWriter
  13. /// <summary>
  14. /// Use this wrapper to ensure that only strings get passed accross the AppDomain
  15. /// boundary. Otherwise tests will break when non-remotable objects are passed to
  16. /// Console.Write/WriteLine.
  17. /// </summary>
  18. public class StringTextWriter : TextWriter
  19. {
  20. public StringTextWriter( TextWriter aTextWriter )
  21. {
  22. theTextWriter = aTextWriter;
  23. }
  24. protected TextWriter theTextWriter;
  25. override public void Write(char aChar)
  26. {
  27. theTextWriter.Write(aChar);
  28. }
  29. override public void Write(string aString)
  30. {
  31. theTextWriter.Write(aString);
  32. }
  33. override public void WriteLine(string aString)
  34. {
  35. theTextWriter.WriteLine(aString);
  36. }
  37. override public System.Text.Encoding Encoding
  38. {
  39. get { return theTextWriter.Encoding; }
  40. }
  41. public override void Close()
  42. {
  43. this.Flush();
  44. theTextWriter.Close ();
  45. }
  46. public override void Flush()
  47. {
  48. theTextWriter.Flush ();
  49. }
  50. }
  51. #endregion
  52. #region BufferedStringTextWriter
  53. /// <summary>
  54. /// This wrapper derives from StringTextWriter and adds buffering
  55. /// to improve cross-domain performance. The buffer is flushed whenever
  56. /// it reaches or exceeds a maximum size or when Flush is called.
  57. /// </summary>
  58. public class BufferedStringTextWriter : StringTextWriter
  59. {
  60. public BufferedStringTextWriter( TextWriter aTextWriter ) : base( aTextWriter ){ }
  61. private static readonly int MAX_BUFFER = 1000;
  62. private StringBuilder sb = new StringBuilder( MAX_BUFFER );
  63. override public void Write(char aChar)
  64. {
  65. lock( sb )
  66. {
  67. sb.Append( aChar );
  68. this.CheckBuffer();
  69. }
  70. }
  71. override public void Write(string aString)
  72. {
  73. lock( sb )
  74. {
  75. sb.Append( aString );
  76. this.CheckBuffer();
  77. }
  78. }
  79. override public void WriteLine(string aString)
  80. {
  81. lock( sb )
  82. {
  83. sb.Append( aString );
  84. sb.Append( '\n' );
  85. this.CheckBuffer();
  86. }
  87. }
  88. override public void Flush()
  89. {
  90. if ( sb.Length > 0 )
  91. {
  92. lock( sb )
  93. {
  94. theTextWriter.Write( sb.ToString() );
  95. sb.Length = 0;
  96. }
  97. }
  98. theTextWriter.Flush();
  99. }
  100. private void CheckBuffer()
  101. {
  102. if ( sb.Length >= MAX_BUFFER )
  103. this.Flush();
  104. }
  105. }
  106. #endregion
  107. }