PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/core/StringTextWriter.cs

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