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