2005-02-10 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / nunit20 / core / StringTextWriter.cs
1 #region Copyright (c) 2002-2003, James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole, Philip A. Craig
2 /************************************************************************************
3 '
4 ' Copyright © 2002-2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
5 ' Copyright © 2000-2003 Philip A. Craig
6 '
7 ' This software is provided 'as-is', without any express or implied warranty. In no 
8 ' event will the authors be held liable for any damages arising from the use of this 
9 ' software.
10
11 ' Permission is granted to anyone to use this software for any purpose, including 
12 ' commercial applications, and to alter it and redistribute it freely, subject to the 
13 ' following restrictions:
14 '
15 ' 1. The origin of this software must not be misrepresented; you must not claim that 
16 ' you wrote the original software. If you use this software in a product, an 
17 ' acknowledgment (see the following) in the product documentation is required.
18 '
19 ' Portions Copyright © 2003 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov, Charlie Poole
20 ' or Copyright © 2000-2003 Philip A. Craig
21 '
22 ' 2. Altered source versions must be plainly marked as such, and must not be 
23 ' misrepresented as being the original software.
24 '
25 ' 3. This notice may not be removed or altered from any source distribution.
26 '
27 '***********************************************************************************/
28 #endregion
29
30 using System;
31 using System.IO;
32 using System.Text;
33
34 namespace NUnit.Core
35 {
36         #region StringTextWriter
37
38         /// <summary>
39         /// Use this wrapper to ensure that only strings get passed accross the AppDomain
40         /// boundary.  Otherwise tests will break when non-remotable objects are passed to
41         /// Console.Write/WriteLine.
42         /// </summary>
43         public class StringTextWriter : TextWriter
44         {
45                 public StringTextWriter( TextWriter aTextWriter )
46                 {
47                         theTextWriter = aTextWriter;
48                 }
49
50                 protected TextWriter theTextWriter;
51
52                 override public void Write(char aChar)
53                 {
54                         theTextWriter.Write(aChar);
55                 }
56
57                 override public void Write(string aString)
58                 {
59                         theTextWriter.Write(aString);
60                 }
61
62                 override public void WriteLine(string aString)
63                 {
64                         theTextWriter.WriteLine(aString);
65                 }
66
67                 override public System.Text.Encoding Encoding
68                 {
69                         get { return theTextWriter.Encoding; }
70                 }
71
72                 public override void Close()
73                 {
74                         this.Flush();
75                         theTextWriter.Close ();
76                 }
77
78                 public override void Flush()
79                 {
80                         theTextWriter.Flush ();
81                 }
82         }
83
84         #endregion
85
86         #region BufferedStringTextWriter
87
88         /// <summary>
89         /// This wrapper derives from StringTextWriter and adds buffering
90         /// to improve cross-domain performance. The buffer is flushed whenever
91         /// it reaches or exceeds a maximum size or when Flush is called.
92         /// </summary>
93         public class BufferedStringTextWriter : StringTextWriter
94         {
95                 public BufferedStringTextWriter( TextWriter aTextWriter ) : base( aTextWriter ){ }
96         
97                 private static readonly int MAX_BUFFER = 1000;
98                 private StringBuilder sb = new StringBuilder( MAX_BUFFER );
99
100                 override public void Write(char aChar)
101                 {
102                         lock( sb )
103                         {
104                                 sb.Append( aChar );
105                                 this.CheckBuffer();
106                         }
107                 }
108
109                 override public void Write(string aString)
110                 {
111                         lock( sb )
112                         {
113                                 sb.Append( aString );
114                                 this.CheckBuffer();
115                         }
116                 }
117
118                 override public void WriteLine(string aString)
119                 {
120                         lock( sb )
121                         {
122                                 sb.Append( aString );
123                                 sb.Append( '\n' );
124                                 this.CheckBuffer();
125                         }
126                 }
127
128                 override public void Flush()
129                 {
130                         if ( sb.Length > 0 )
131                         {
132                                 lock( sb )
133                                 {
134                                         theTextWriter.Write( sb.ToString() );
135                                         sb.Length = 0;
136                                 }
137                         }
138
139                         theTextWriter.Flush();
140                 }
141
142                 private void CheckBuffer()
143                 {
144                         if ( sb.Length >= MAX_BUFFER )
145                                 this.Flush();
146                 }
147         }
148
149         #endregion
150 }