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