2008-08-21 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / TextWriter.cs
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 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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.
31 //
32
33 using System.Text;
34
35 #if NET_2_0
36 using System.Runtime.InteropServices;
37 #endif
38
39 namespace System.IO {
40
41         [Serializable]
42 #if NET_2_0
43         [ComVisible (true)]
44 #endif
45 #if NET_2_1
46         public abstract class TextWriter : IDisposable {
47 #else
48         public abstract class TextWriter : MarshalByRefObject, IDisposable {
49 #endif
50                 
51                 protected TextWriter() {
52                         CoreNewLine = System.Environment.NewLine.ToCharArray ();
53                 }
54                 
55                 protected TextWriter( IFormatProvider formatProvider ) {
56                         CoreNewLine = System.Environment.NewLine.ToCharArray ();
57                         internalFormatProvider = formatProvider;
58                 }
59
60                 protected char[] CoreNewLine;
61
62                 internal IFormatProvider internalFormatProvider;
63
64                 public static readonly TextWriter Null = new NullTextWriter ();
65
66                 public abstract Encoding Encoding { get; }
67
68                 public virtual IFormatProvider FormatProvider { 
69                         get {
70                                 return internalFormatProvider;
71                         } 
72                 }
73
74                 public virtual string NewLine { 
75                         get {
76                                 return new string(CoreNewLine);
77                         }
78                         
79                         set {
80                                 if (value == null)
81                                         value = Environment.NewLine;
82
83                                 CoreNewLine = value.ToCharArray();
84                         }
85                 }
86
87                 public virtual void Close () { 
88                         Dispose (true);
89                 }
90
91                 protected virtual void Dispose (bool disposing)
92                 {
93                         if (disposing){
94                                 // If we are explicitly disposed, we can avoid finalization.
95                                 GC.SuppressFinalize (this);
96                         }
97                 }
98 #if NET_2_0
99                 public void Dispose ()
100                 {
101                         Dispose (true);
102
103                         // If we are explicitly disposed, we can avoid finalization.
104                         GC.SuppressFinalize (this);
105                 }
106 #else
107                 void System.IDisposable.Dispose () {
108                         Dispose (true);
109                 }
110 #endif
111
112
113                 public virtual void Flush()
114                 {
115                         // do nothing
116                 }
117
118                 public static TextWriter Synchronized (TextWriter writer)
119                 {
120                         return Synchronized (writer, false);
121                 }
122
123                 internal static TextWriter Synchronized (TextWriter writer, bool neverClose)
124                 {
125                         if (writer == null)
126                                 throw new ArgumentNullException ("writer is null");
127
128                         if (writer is SynchronizedWriter)
129                                 return writer;
130                         
131                         return new SynchronizedWriter (writer, neverClose);
132                 }
133
134                 public virtual void Write (bool value)
135                 {
136                         Write (value.ToString ());
137                 }
138                 
139                 public virtual void Write (char value)
140                 {
141                         // Do nothing
142                 }
143
144                 public virtual void Write (char[] buffer)
145                 {
146                         if (buffer == null)
147                                 return;
148                         Write (buffer, 0, buffer.Length);
149                 }
150                 
151                 public virtual void Write (decimal value)
152                 {
153                         Write (value.ToString (internalFormatProvider));
154                 }
155                 
156                 public virtual void Write (double value)
157                 {
158                         Write (value.ToString (internalFormatProvider));
159                 }
160
161                 public virtual void Write (int value)
162                 {
163                         Write (value.ToString (internalFormatProvider));
164                 }
165                 
166                 public virtual void Write (long value)
167                 {
168                         Write (value.ToString (internalFormatProvider));
169                 }
170                 
171                 public virtual void Write (object value)
172                 {
173                         if (value != null)
174                                 Write (value.ToString ());
175                 }
176                 
177                 public virtual void Write (float value)
178                 {
179                         Write (value.ToString (internalFormatProvider));
180                 }
181                 
182                 public virtual void Write (string value)
183                 {
184                         if (value != null)
185                                 Write (value.ToCharArray ());
186                 }
187                 
188                 [CLSCompliant(false)]
189                 public virtual void Write (uint value)
190                 {
191                         Write (value.ToString (internalFormatProvider));
192                 }
193                 
194                 [CLSCompliant(false)]
195                 public virtual void Write (ulong value)
196                 {
197                         Write (value.ToString (internalFormatProvider));
198                 }
199                 
200                 public virtual void Write (string format, object arg0)
201                 {
202                         Write (String.Format (format, arg0));
203                 }
204                 
205                 public virtual void Write (string format, params object[] arg)
206                 {
207                         Write (String.Format (format, arg));
208                 }
209                 
210                 public virtual void Write (char[] buffer, int index, int count)
211                 {
212                         if (buffer == null)
213                                 throw new ArgumentNullException ("buffer");
214                         if (index < 0 || index > buffer.Length)
215                                 throw new ArgumentOutOfRangeException ("index");
216                         // re-ordered to avoid possible integer overflow
217                         if (count < 0 || (index > buffer.Length - count))
218                                 throw new ArgumentOutOfRangeException ("count");
219
220                         for (; count > 0; --count, ++index) {
221                                 Write (buffer [index]);
222                         }
223                 }
224                 
225                 public virtual void Write (string format, object arg0, object arg1)
226                 {
227                         Write (String.Format (format, arg0, arg1));
228                 }
229                 
230                 public virtual void Write (string format, object arg0, object arg1, object arg2 )
231                 {
232                         Write (String.Format (format, arg0, arg1, arg2));
233                 }
234                 
235                 public virtual void WriteLine ()
236                 {
237                         Write (CoreNewLine);
238                 }
239                 
240                 public virtual void WriteLine (bool value)
241                 {
242                         Write (value);
243                         WriteLine();
244                 }
245                 
246                 public virtual void WriteLine (char value)
247                 {
248                         Write (value);
249                         WriteLine();
250                 }
251                 
252                 public virtual void WriteLine (char[] buffer)
253                 {
254                         Write (buffer);
255                         WriteLine();
256                 }
257                 
258                 public virtual void WriteLine (decimal value)
259                 {
260                         Write (value);
261                         WriteLine();
262                 }
263                 
264                 public virtual void WriteLine (double value)
265                 {
266                         Write (value);
267                         WriteLine();
268                 }
269                 
270                 public virtual void WriteLine (int value)
271                 {
272                         Write (value);
273                         WriteLine();
274                 }
275                 
276                 public virtual void WriteLine (long value)
277                 {
278                         Write (value);
279                         WriteLine();
280                 }
281                 
282                 public virtual void WriteLine (object value)
283                 {
284                         Write (value);
285                         WriteLine();
286                 }
287                 
288                 public virtual void WriteLine (float value)
289                 {
290                         Write (value);
291                         WriteLine();
292                 }
293                 
294                 public virtual void WriteLine (string value)
295                 {
296                         Write (value);
297                         WriteLine();
298                 }
299                 
300                 [CLSCompliant(false)]
301                 public virtual void WriteLine (uint value)
302                 {
303                         Write (value);
304                         WriteLine();
305                 }
306                 
307                 [CLSCompliant(false)]
308                 public virtual void WriteLine (ulong value)
309                 {
310                         Write (value);
311                         WriteLine();
312                 }
313                 
314                 public virtual void WriteLine (string format, object arg0)
315                 {
316                         Write (format, arg0);
317                         WriteLine();
318                 }
319                 
320                 public virtual void WriteLine (string format, params object[] arg)
321                 {
322                         Write (format, arg);
323                         WriteLine();
324                 }
325                 
326                 public virtual void WriteLine (char[] buffer, int index, int count)
327                 {
328                         Write (buffer, index, count);
329                         WriteLine();
330                 }
331                 
332                 public virtual void WriteLine (string format, object arg0, object arg1)
333                 {
334                         Write (format, arg0, arg1);
335                         WriteLine();
336                 }
337                 
338                 public virtual void WriteLine (string format, object arg0, object arg1, object arg2)
339                 {
340                         Write (format, arg0, arg1, arg2);
341                         WriteLine();
342                 }
343
344                 //
345                 // Null version of the TextWriter, for the `Null' instance variable
346                 //
347                 sealed class NullTextWriter : TextWriter {
348                         public override Encoding Encoding {
349                                 get {
350                                         return Encoding.Default;
351                                 }
352                         }
353                         
354                         public override void Write (string s)
355                         {
356                         }
357                         public override void Write (char value)
358                         {
359                         }
360                         public override void Write (char[] value, int index, int count)
361                         {
362                         }
363                 }
364         }
365
366         //
367         // Sychronized version of the TextWriter.
368         //
369         [Serializable]
370         internal class SynchronizedWriter : TextWriter {
371                 private TextWriter writer;
372                 private bool neverClose;
373
374                 public SynchronizedWriter (TextWriter writer)
375                         : this (writer, false)
376                 {
377                 }
378
379                 public SynchronizedWriter (TextWriter writer, bool neverClose)
380                 {
381                         this.writer = writer;
382                         this.neverClose = neverClose;
383                 }
384
385                 public override void Close ()
386                 {
387                         if (neverClose)
388                                 return;
389                         lock (this){
390                                 writer.Close ();
391                         }
392                 }
393
394                 public override void Flush ()
395                 {
396                         lock (this){
397                                 writer.Flush ();
398                         }
399                 }
400
401 #region Write methods
402                 public override void Write (bool value)
403                 {
404                         lock (this){
405                                 writer.Write (value);
406                         }
407                 }
408                 
409                 public override void Write (char value)
410                 {
411                         lock (this){
412                                 writer.Write (value);
413                         }
414                 }
415
416                 public override void Write (char [] value)
417                 {
418                         lock (this){
419                                 writer.Write (value);
420                         }
421                 }
422
423                 public override void Write (Decimal value)
424                 {
425                         lock (this){
426                                 writer.Write (value);
427                         }
428                 }
429
430                 public override void Write (int value)
431                 {
432                         lock (this){
433                                 writer.Write (value);
434                         }
435                 }
436
437                 public override void Write (long value)
438                 {
439                         lock (this){
440                                 writer.Write (value);
441                         }
442                 }
443                 
444                 public override void Write (object value)
445                 {
446                         lock (this){
447                                 writer.Write (value);
448                         }
449                 }
450
451                 public override void Write (float value)
452                 {
453                         lock (this){
454                                 writer.Write (value);
455                         }
456                 }
457                 
458                 public override void Write (string value)
459                 {
460                         lock (this){
461                                 writer.Write (value);
462                         }
463                 }
464                 
465                 public override void Write (uint value)
466                 {
467                         lock (this){
468                                 writer.Write (value);
469                         }
470                 }
471                 
472                 public override void Write (ulong value)
473                 {
474                         lock (this){
475                                 writer.Write (value);
476                         }
477                 }
478                 
479                 public override void Write (string format, object value)
480                 {
481                         lock (this){
482                                 writer.Write (format, value);
483                         }
484                 }
485                 
486                 public override void Write (string format, object [] value)
487                 {
488                         lock (this){
489                                 writer.Write (format, value);
490                         }
491                 }
492
493                 public override void Write (char [] buffer, int index, int count)
494                 {
495                         lock (this){
496                                 writer.Write (buffer, index, count);
497                         }
498                 }
499                 
500                 public override void Write (string format, object arg0, object arg1)
501                 {
502                         lock (this){
503                                 writer.Write (format, arg0, arg1);
504                         }
505                 }
506                 
507                 public override void Write (string format, object arg0, object arg1, object arg2)
508                 {
509                         lock (this){
510                                 writer.Write (format, arg0, arg1, arg2);
511                         }
512                 }
513 #endregion
514 #region WriteLine methods
515                 public override void WriteLine ()
516                 {
517                         lock (this){
518                                 writer.WriteLine ();
519                         }
520                 }
521
522                 public override void WriteLine (bool value)
523                 {
524                         lock (this){
525                                 writer.WriteLine (value);
526                         }
527                 }
528
529                 public override void WriteLine (char value)
530                 {
531                         lock (this){
532                                 writer.WriteLine (value);
533                         }
534                 }
535
536                 public override void WriteLine (char [] value)
537                 {
538                         lock (this){
539                                 writer.WriteLine (value);
540                         }
541                 }
542
543                 public override void WriteLine (Decimal value)
544                 {
545                         lock (this){
546                                 writer.WriteLine (value);
547                         }
548                 }
549
550                 public override void WriteLine (double value)
551                 {
552                         lock (this){
553                                 writer.WriteLine (value);
554                         }
555                 }
556
557                 public override void WriteLine (int value)
558                 {
559                         lock (this){
560                                 writer.WriteLine (value);
561                         }
562                 }
563
564                 public override void WriteLine (long value)
565                 {
566                         lock (this){
567                                 writer.WriteLine (value);
568                         }
569                 }
570
571                 public override void WriteLine (object value)
572                 {
573                         lock (this){
574                                 writer.WriteLine (value);
575                         }
576                 }
577
578                 public override void WriteLine (float value)
579                 {
580                         lock (this){
581                                 writer.WriteLine (value);
582                         }
583                 }
584
585                 public override void WriteLine (string value)
586                 {
587                         lock (this){
588                                 writer.WriteLine (value);
589                         }
590                 }
591
592                 public override void WriteLine (uint value)
593                 {
594                         lock (this){
595                                 writer.WriteLine (value);
596                         }
597                 }
598
599                 public override void WriteLine (ulong value)
600                 {
601                         lock (this){
602                                 writer.WriteLine (value);
603                         }
604                 }
605
606                 public override void WriteLine (string format, object value)
607                 {
608                         lock (this){
609                                 writer.WriteLine (format, value);
610                         }
611                 }
612
613                 public override void WriteLine (string format, object [] value)
614                 {
615                         lock (this){
616                                 writer.WriteLine (format, value);
617                         }
618                 }
619
620                 public override void WriteLine (char [] buffer, int index, int count)
621                 {
622                         lock (this){
623                                 writer.WriteLine (buffer, index, count);
624                         }
625                 }
626                 
627                 public override void WriteLine (string format, object arg0, object arg1)
628                 {
629                         lock (this){
630                                 writer.WriteLine (format, arg0, arg1);
631                         }
632                 }
633
634                 public override void WriteLine (string format, object arg0, object arg1, object arg2)
635                 {
636                         lock (this){
637                                 writer.WriteLine (format, arg0, arg1, arg2);
638                         }
639                 }
640 #endregion
641                 
642                 public override Encoding Encoding {
643                         get {
644                                 lock (this){
645                                         return writer.Encoding;
646                                 }
647                         }
648                 }
649
650                 public override IFormatProvider FormatProvider {
651                         get {
652                                 lock (this){
653                                         return writer.FormatProvider;
654                                 }
655                         }
656                 }
657
658                 public override string NewLine {
659                         get {
660                                 lock (this){
661                                         return writer.NewLine;
662                                 }
663                         }
664
665                         set {
666                                 lock (this){
667                                         writer.NewLine = value;
668                                 }
669                         }
670                 }
671         }
672 }