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