use MOONLIGHT symbol
[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 using System.Runtime.InteropServices;\r
37 \r
38 namespace System.IO {\r
39 \r
40         [Serializable]\r
41         [ComVisible (true)]\r
42         [MonoLimitation ("Serialization format not compatible with .NET")]\r
43         public class StringWriter : TextWriter {\r
44                 \r
45                 private StringBuilder internalString;\r
46                 private bool disposed = false;\r
47 \r
48                 public StringWriter () : this (new StringBuilder ())\r
49                 {\r
50                 }\r
51 \r
52                 public StringWriter (IFormatProvider formatProvider) :\r
53                         this (new StringBuilder (), formatProvider)\r
54                 {\r
55                 }\r
56 \r
57                 public StringWriter (StringBuilder sb) :\r
58                         this (sb, null)\r
59                 {\r
60                 }\r
61 \r
62                 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)\r
63                 {\r
64                         if (sb == null)\r
65                                 throw new ArgumentNullException ("sb");\r
66 \r
67                         internalString = sb;\r
68                         internalFormatProvider = formatProvider;\r
69                 }\r
70 \r
71                 public override System.Text.Encoding Encoding {\r
72                         get {\r
73                                 return System.Text.Encoding.Unicode;\r
74                         }\r
75                 }\r
76 \r
77                 public override void Close ()\r
78                 {\r
79                         Dispose (true);\r
80                         disposed = true;\r
81                 }\r
82 \r
83                 protected override void Dispose (bool disposing)\r
84                 {\r
85                         // MS.NET doesn't clear internal buffer.\r
86                         // internalString = null;\r
87                         base.Dispose (disposing);\r
88                         disposed = true;\r
89                 }\r
90 \r
91                 public virtual StringBuilder GetStringBuilder ()\r
92                 {\r
93                         return internalString;\r
94                 }\r
95 \r
96                 public override string ToString ()\r
97                 {\r
98                         return internalString.ToString ();\r
99                 }\r
100 \r
101                 public override void Write (char value)\r
102                 {\r
103                         if (disposed) {\r
104                                 throw new ObjectDisposedException ("StringReader", \r
105                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
106                         }\r
107 \r
108                         internalString.Append (value);\r
109                 }\r
110 \r
111                 public override void Write (string value)\r
112                 {\r
113                         if (disposed) {\r
114                                 throw new ObjectDisposedException ("StringReader", \r
115                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
116                         }\r
117 \r
118                         internalString.Append (value);\r
119                 }\r
120 \r
121                 public override void Write (char[] buffer, int index, int count)\r
122                 {\r
123                         if (disposed) {\r
124                                 throw new ObjectDisposedException ("StringReader", \r
125                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
126                         }\r
127                         if (buffer == null)\r
128                                 throw new ArgumentNullException ("buffer");\r
129                         if (index < 0)\r
130                                 throw new ArgumentOutOfRangeException ("index", "< 0");\r
131                         if (count < 0)\r
132                                 throw new ArgumentOutOfRangeException ("count", "< 0");\r
133                         // re-ordered to avoid possible integer overflow\r
134                         if (index > buffer.Length - count)\r
135                                 throw new ArgumentException ("index + count > buffer.Length");\r
136 \r
137                         internalString.Append (buffer, index, count);\r
138                 }\r
139         }\r
140 }\r
141               \r