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