2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System.IO / TextWriter.cs
blob116e286165664916728ab08bd70fdf35c9a56717
1 //
2 // System.IO.TextWriter.cs
3 //
4 // Authors:
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Miguel de Icaza (miguel@gnome.org)
7 // Paolo Molaro (lupus@ximian.com)
8 //
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:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
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.
33 using System.Text;
35 using System.Runtime.InteropServices;
37 namespace System.IO {
39 [Serializable]
40 [ComVisible (true)]
41 #if NET_2_1
42 public abstract class TextWriter : IDisposable {
43 #else
44 public abstract class TextWriter : MarshalByRefObject, IDisposable {
45 #endif
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 {
65 get {
66 return internalFormatProvider;
70 public virtual string NewLine {
71 get {
72 return new string(CoreNewLine);
75 set {
76 if (value == null)
77 value = Environment.NewLine;
79 CoreNewLine = value.ToCharArray();
83 public virtual void Close () {
84 Dispose (true);
87 protected virtual void Dispose (bool disposing)
89 if (disposing){
90 // If we are explicitly disposed, we can avoid finalization.
91 GC.SuppressFinalize (this);
94 public void Dispose ()
96 Dispose (true);
98 // If we are explicitly disposed, we can avoid finalization.
99 GC.SuppressFinalize (this);
102 public virtual void Flush()
104 // do nothing
107 public static TextWriter Synchronized (TextWriter writer)
109 return Synchronized (writer, false);
112 internal static TextWriter Synchronized (TextWriter writer, bool neverClose)
114 if (writer == null)
115 throw new ArgumentNullException ("writer is null");
117 if (writer is SynchronizedWriter)
118 return writer;
120 return new SynchronizedWriter (writer, neverClose);
123 public virtual void Write (bool value)
125 Write (value.ToString ());
128 public virtual void Write (char value)
130 // Do nothing
133 public virtual void Write (char[] buffer)
135 if (buffer == null)
136 return;
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)
162 if (value != null)
163 Write (value.ToString ());
166 public virtual void Write (float value)
168 Write (value.ToString (internalFormatProvider));
171 public virtual void Write (string value)
173 if (value != null)
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)
201 if (buffer == null)
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 ()
226 Write (CoreNewLine);
229 public virtual void WriteLine (bool value)
231 Write (value);
232 WriteLine();
235 public virtual void WriteLine (char value)
237 Write (value);
238 WriteLine();
241 public virtual void WriteLine (char[] buffer)
243 Write (buffer);
244 WriteLine();
247 public virtual void WriteLine (decimal value)
249 Write (value);
250 WriteLine();
253 public virtual void WriteLine (double value)
255 Write (value);
256 WriteLine();
259 public virtual void WriteLine (int value)
261 Write (value);
262 WriteLine();
265 public virtual void WriteLine (long value)
267 Write (value);
268 WriteLine();
271 public virtual void WriteLine (object value)
273 Write (value);
274 WriteLine();
277 public virtual void WriteLine (float value)
279 Write (value);
280 WriteLine();
283 public virtual void WriteLine (string value)
285 Write (value);
286 WriteLine();
289 [CLSCompliant(false)]
290 public virtual void WriteLine (uint value)
292 Write (value);
293 WriteLine();
296 [CLSCompliant(false)]
297 public virtual void WriteLine (ulong value)
299 Write (value);
300 WriteLine();
303 public virtual void WriteLine (string format, object arg0)
305 Write (format, arg0);
306 WriteLine();
309 public virtual void WriteLine (string format, params object[] arg)
311 Write (format, arg);
312 WriteLine();
315 public virtual void WriteLine (char[] buffer, int index, int count)
317 Write (buffer, index, count);
318 WriteLine();
321 public virtual void WriteLine (string format, object arg0, object arg1)
323 Write (format, arg0, arg1);
324 WriteLine();
327 public virtual void WriteLine (string format, object arg0, object arg1, object arg2)
329 Write (format, arg0, arg1, arg2);
330 WriteLine();
334 // Null version of the TextWriter, for the `Null' instance variable
336 sealed class NullTextWriter : TextWriter {
337 public override Encoding Encoding {
338 get {
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.
358 [Serializable]
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 ()
376 if (neverClose)
377 return;
378 lock (this){
379 writer.Close ();
383 public override void Flush ()
385 lock (this){
386 writer.Flush ();
390 #region Write methods
391 public override void Write (bool value)
393 lock (this){
394 writer.Write (value);
398 public override void Write (char value)
400 lock (this){
401 writer.Write (value);
405 public override void Write (char [] value)
407 lock (this){
408 writer.Write (value);
412 public override void Write (Decimal value)
414 lock (this){
415 writer.Write (value);
419 public override void Write (int value)
421 lock (this){
422 writer.Write (value);
426 public override void Write (long value)
428 lock (this){
429 writer.Write (value);
433 public override void Write (object value)
435 lock (this){
436 writer.Write (value);
440 public override void Write (float value)
442 lock (this){
443 writer.Write (value);
447 public override void Write (string value)
449 lock (this){
450 writer.Write (value);
454 public override void Write (uint value)
456 lock (this){
457 writer.Write (value);
461 public override void Write (ulong value)
463 lock (this){
464 writer.Write (value);
468 public override void Write (string format, object value)
470 lock (this){
471 writer.Write (format, value);
475 public override void Write (string format, object [] value)
477 lock (this){
478 writer.Write (format, value);
482 public override void Write (char [] buffer, int index, int count)
484 lock (this){
485 writer.Write (buffer, index, count);
489 public override void Write (string format, object arg0, object arg1)
491 lock (this){
492 writer.Write (format, arg0, arg1);
496 public override void Write (string format, object arg0, object arg1, object arg2)
498 lock (this){
499 writer.Write (format, arg0, arg1, arg2);
502 #endregion
503 #region WriteLine methods
504 public override void WriteLine ()
506 lock (this){
507 writer.WriteLine ();
511 public override void WriteLine (bool value)
513 lock (this){
514 writer.WriteLine (value);
518 public override void WriteLine (char value)
520 lock (this){
521 writer.WriteLine (value);
525 public override void WriteLine (char [] value)
527 lock (this){
528 writer.WriteLine (value);
532 public override void WriteLine (Decimal value)
534 lock (this){
535 writer.WriteLine (value);
539 public override void WriteLine (double value)
541 lock (this){
542 writer.WriteLine (value);
546 public override void WriteLine (int value)
548 lock (this){
549 writer.WriteLine (value);
553 public override void WriteLine (long value)
555 lock (this){
556 writer.WriteLine (value);
560 public override void WriteLine (object value)
562 lock (this){
563 writer.WriteLine (value);
567 public override void WriteLine (float value)
569 lock (this){
570 writer.WriteLine (value);
574 public override void WriteLine (string value)
576 lock (this){
577 writer.WriteLine (value);
581 public override void WriteLine (uint value)
583 lock (this){
584 writer.WriteLine (value);
588 public override void WriteLine (ulong value)
590 lock (this){
591 writer.WriteLine (value);
595 public override void WriteLine (string format, object value)
597 lock (this){
598 writer.WriteLine (format, value);
602 public override void WriteLine (string format, object [] value)
604 lock (this){
605 writer.WriteLine (format, value);
609 public override void WriteLine (char [] buffer, int index, int count)
611 lock (this){
612 writer.WriteLine (buffer, index, count);
616 public override void WriteLine (string format, object arg0, object arg1)
618 lock (this){
619 writer.WriteLine (format, arg0, arg1);
623 public override void WriteLine (string format, object arg0, object arg1, object arg2)
625 lock (this){
626 writer.WriteLine (format, arg0, arg1, arg2);
629 #endregion
631 public override Encoding Encoding {
632 get {
633 lock (this){
634 return writer.Encoding;
639 public override IFormatProvider FormatProvider {
640 get {
641 lock (this){
642 return writer.FormatProvider;
647 public override string NewLine {
648 get {
649 lock (this){
650 return writer.NewLine;
654 set {
655 lock (this){
656 writer.NewLine = value;