copying the latest Sys.Web.Services from trunk.
[mono.git] / mcs / class / corlib / System.IO / StringWriter.cs
1 //\r
2 // System.IO.StringWriter\r
3 //\r
4 // Authors
5 //      Marcin Szczepanski (marcins@zipworld.com.au)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004 Novell (http://www.novell.com)\r
9 //\r
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System.Globalization;\r
35 using System.Text;\r
36 \r
37 namespace System.IO {\r
38
39         [Serializable]\r
40         [MonoTODO ("Fix serialization compatibility with MS.NET")]\r
41         public class StringWriter : TextWriter {\r
42                 \r
43                 private StringBuilder internalString;\r
44                 private bool disposed = false;\r
45 \r
46                 public StringWriter () : this (new StringBuilder ())
47                 {
48                 }\r
49 \r
50                 public StringWriter (IFormatProvider formatProvider) :
51                         this (new StringBuilder (), formatProvider)
52                 {
53                 }\r
54 \r
55                 public StringWriter (StringBuilder sb) :
56                         this (sb, null)
57                 {\r
58                 }\r
59 \r
60                 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
61                 {\r
62                         if (sb == null)\r
63                                 throw new ArgumentNullException ("sb");\r
64 \r
65                         internalString = sb;\r
66                         internalFormatProvider = formatProvider;\r
67                 }\r
68 \r
69                 public override System.Text.Encoding Encoding {\r
70                         get {\r
71                                 return System.Text.Encoding.Unicode;\r
72                         }\r
73                 }\r
74 \r
75                 public override void Close ()
76                 {\r
77                         Dispose (true);\r
78                         disposed = true;\r
79                 }\r
80 \r
81                 protected override void Dispose (bool disposing)\r
82                 {\r
83                         // MS.NET doesn't clear internal buffer.\r
84                         // internalString = null;\r
85                         base.Dispose (disposing);\r
86                         disposed = true;\r
87                 }\r
88 \r
89                 public virtual StringBuilder GetStringBuilder ()
90                 {\r
91                         return internalString;\r
92                 }\r
93 \r
94                 public override string ToString ()
95                 {\r
96                         return internalString.ToString ();\r
97                 }\r
98 \r
99                 public override void Write (char value)
100                 {\r
101                         if (disposed) {\r
102                                 throw new ObjectDisposedException ("StringReader", 
103                                         Locale.GetText ("Cannot write to a closed StringWriter"));
104                         }\r
105 \r
106                         internalString.Append (value);\r
107                 }\r
108 \r
109                 public override void Write (string value)
110                 {\r
111                         if (disposed) {\r
112                                 throw new ObjectDisposedException ("StringReader", 
113                                         Locale.GetText ("Cannot write to a closed StringWriter"));
114                         }\r
115
116                         internalString.Append (value);\r
117                 }\r
118 \r
119                 public override void Write (char[] buffer, int index, int count)
120                 {\r
121                         if (disposed) {\r
122                                 throw new ObjectDisposedException ("StringReader", 
123                                         Locale.GetText ("Cannot write to a closed StringWriter"));
124                         }\r
125                         if (buffer == null)\r
126                                 throw new ArgumentNullException ("buffer");
127                         if (index < 0)\r
128                                 throw new ArgumentOutOfRangeException ("index", "< 0");
129                         if (count < 0)\r
130                                 throw new ArgumentOutOfRangeException ("count", "< 0");
131                         // re-ordered to avoid possible integer overflow
132                         if (index > buffer.Length - count)\r
133                                 throw new ArgumentException ("index + count > buffer.Length");\r
134
135                         internalString.Append (buffer, index, count);\r
136                 }\r
137         }\r
138 }\r
139               \r