2 // System.IO.TextWriter.cs
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Miguel de Icaza (miguel@gnome.org)
7 // Paolo Molaro (lupus@ximian.com)
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System
.Runtime
.InteropServices
;
42 public abstract class TextWriter
: IDisposable
{
44 public abstract class TextWriter
: MarshalByRefObject
, IDisposable
{
47 protected TextWriter() {
48 CoreNewLine
= System
.Environment
.NewLine
.ToCharArray ();
51 protected TextWriter( IFormatProvider formatProvider
) {
52 CoreNewLine
= System
.Environment
.NewLine
.ToCharArray ();
53 internalFormatProvider
= formatProvider
;
56 protected char[] CoreNewLine
;
58 internal IFormatProvider internalFormatProvider
;
60 public static readonly TextWriter Null
= new NullTextWriter ();
62 public abstract Encoding Encoding { get; }
64 public virtual IFormatProvider FormatProvider
{
66 return internalFormatProvider
;
70 public virtual string NewLine
{
72 return new string(CoreNewLine
);
77 value = Environment
.NewLine
;
79 CoreNewLine
= value.ToCharArray();
83 public virtual void Close () {
87 protected virtual void Dispose (bool disposing
)
90 // If we are explicitly disposed, we can avoid finalization.
91 GC
.SuppressFinalize (this);
94 public void Dispose ()
98 // If we are explicitly disposed, we can avoid finalization.
99 GC
.SuppressFinalize (this);
102 public virtual void Flush()
107 public static TextWriter
Synchronized (TextWriter writer
)
109 return Synchronized (writer
, false);
112 internal static TextWriter
Synchronized (TextWriter writer
, bool neverClose
)
115 throw new ArgumentNullException ("writer is null");
117 if (writer
is SynchronizedWriter
)
120 return new SynchronizedWriter (writer
, neverClose
);
123 public virtual void Write (bool value)
125 Write (value.ToString ());
128 public virtual void Write (char value)
133 public virtual void Write (char[] buffer
)
137 Write (buffer
, 0, buffer
.Length
);
140 public virtual void Write (decimal value)
142 Write (value.ToString (internalFormatProvider
));
145 public virtual void Write (double value)
147 Write (value.ToString (internalFormatProvider
));
150 public virtual void Write (int value)
152 Write (value.ToString (internalFormatProvider
));
155 public virtual void Write (long value)
157 Write (value.ToString (internalFormatProvider
));
160 public virtual void Write (object value)
163 Write (value.ToString ());
166 public virtual void Write (float value)
168 Write (value.ToString (internalFormatProvider
));
171 public virtual void Write (string value)
174 Write (value.ToCharArray ());
177 [CLSCompliant(false)]
178 public virtual void Write (uint value)
180 Write (value.ToString (internalFormatProvider
));
183 [CLSCompliant(false)]
184 public virtual void Write (ulong value)
186 Write (value.ToString (internalFormatProvider
));
189 public virtual void Write (string format
, object arg0
)
191 Write (String
.Format (format
, arg0
));
194 public virtual void Write (string format
, params object[] arg
)
196 Write (String
.Format (format
, arg
));
199 public virtual void Write (char[] buffer
, int index
, int count
)
202 throw new ArgumentNullException ("buffer");
203 if (index
< 0 || index
> buffer
.Length
)
204 throw new ArgumentOutOfRangeException ("index");
205 // re-ordered to avoid possible integer overflow
206 if (count
< 0 || (index
> buffer
.Length
- count
))
207 throw new ArgumentOutOfRangeException ("count");
209 for (; count
> 0; --count
, ++index
) {
210 Write (buffer
[index
]);
214 public virtual void Write (string format
, object arg0
, object arg1
)
216 Write (String
.Format (format
, arg0
, arg1
));
219 public virtual void Write (string format
, object arg0
, object arg1
, object arg2
)
221 Write (String
.Format (format
, arg0
, arg1
, arg2
));
224 public virtual void WriteLine ()
229 public virtual void WriteLine (bool value)
235 public virtual void WriteLine (char value)
241 public virtual void WriteLine (char[] buffer
)
247 public virtual void WriteLine (decimal value)
253 public virtual void WriteLine (double value)
259 public virtual void WriteLine (int value)
265 public virtual void WriteLine (long value)
271 public virtual void WriteLine (object value)
277 public virtual void WriteLine (float value)
283 public virtual void WriteLine (string value)
289 [CLSCompliant(false)]
290 public virtual void WriteLine (uint value)
296 [CLSCompliant(false)]
297 public virtual void WriteLine (ulong value)
303 public virtual void WriteLine (string format
, object arg0
)
305 Write (format
, arg0
);
309 public virtual void WriteLine (string format
, params object[] arg
)
315 public virtual void WriteLine (char[] buffer
, int index
, int count
)
317 Write (buffer
, index
, count
);
321 public virtual void WriteLine (string format
, object arg0
, object arg1
)
323 Write (format
, arg0
, arg1
);
327 public virtual void WriteLine (string format
, object arg0
, object arg1
, object arg2
)
329 Write (format
, arg0
, arg1
, arg2
);
334 // Null version of the TextWriter, for the `Null' instance variable
336 sealed class NullTextWriter
: TextWriter
{
337 public override Encoding Encoding
{
339 return Encoding
.Default
;
343 public override void Write (string s
)
346 public override void Write (char value)
349 public override void Write (char[] value, int index
, int count
)
356 // Sychronized version of the TextWriter.
359 internal class SynchronizedWriter
: TextWriter
{
360 private TextWriter writer
;
361 private bool neverClose
;
363 public SynchronizedWriter (TextWriter writer
)
364 : this (writer
, false)
368 public SynchronizedWriter (TextWriter writer
, bool neverClose
)
370 this.writer
= writer
;
371 this.neverClose
= neverClose
;
374 public override void Close ()
383 public override void Flush ()
390 #region Write methods
391 public override void Write (bool value)
394 writer
.Write (value);
398 public override void Write (char value)
401 writer
.Write (value);
405 public override void Write (char [] value)
408 writer
.Write (value);
412 public override void Write (Decimal
value)
415 writer
.Write (value);
419 public override void Write (int value)
422 writer
.Write (value);
426 public override void Write (long value)
429 writer
.Write (value);
433 public override void Write (object value)
436 writer
.Write (value);
440 public override void Write (float value)
443 writer
.Write (value);
447 public override void Write (string value)
450 writer
.Write (value);
454 public override void Write (uint value)
457 writer
.Write (value);
461 public override void Write (ulong value)
464 writer
.Write (value);
468 public override void Write (string format
, object value)
471 writer
.Write (format
, value);
475 public override void Write (string format
, object [] value)
478 writer
.Write (format
, value);
482 public override void Write (char [] buffer
, int index
, int count
)
485 writer
.Write (buffer
, index
, count
);
489 public override void Write (string format
, object arg0
, object arg1
)
492 writer
.Write (format
, arg0
, arg1
);
496 public override void Write (string format
, object arg0
, object arg1
, object arg2
)
499 writer
.Write (format
, arg0
, arg1
, arg2
);
503 #region WriteLine methods
504 public override void WriteLine ()
511 public override void WriteLine (bool value)
514 writer
.WriteLine (value);
518 public override void WriteLine (char value)
521 writer
.WriteLine (value);
525 public override void WriteLine (char [] value)
528 writer
.WriteLine (value);
532 public override void WriteLine (Decimal
value)
535 writer
.WriteLine (value);
539 public override void WriteLine (double value)
542 writer
.WriteLine (value);
546 public override void WriteLine (int value)
549 writer
.WriteLine (value);
553 public override void WriteLine (long value)
556 writer
.WriteLine (value);
560 public override void WriteLine (object value)
563 writer
.WriteLine (value);
567 public override void WriteLine (float value)
570 writer
.WriteLine (value);
574 public override void WriteLine (string value)
577 writer
.WriteLine (value);
581 public override void WriteLine (uint value)
584 writer
.WriteLine (value);
588 public override void WriteLine (ulong value)
591 writer
.WriteLine (value);
595 public override void WriteLine (string format
, object value)
598 writer
.WriteLine (format
, value);
602 public override void WriteLine (string format
, object [] value)
605 writer
.WriteLine (format
, value);
609 public override void WriteLine (char [] buffer
, int index
, int count
)
612 writer
.WriteLine (buffer
, index
, count
);
616 public override void WriteLine (string format
, object arg0
, object arg1
)
619 writer
.WriteLine (format
, arg0
, arg1
);
623 public override void WriteLine (string format
, object arg0
, object arg1
, object arg2
)
626 writer
.WriteLine (format
, arg0
, arg1
, arg2
);
631 public override Encoding Encoding
{
634 return writer
.Encoding
;
639 public override IFormatProvider FormatProvider
{
642 return writer
.FormatProvider
;
647 public override string NewLine
{
650 return writer
.NewLine
;
656 writer
.NewLine
= value;