Merge pull request #297 from ermshiperete/4959
[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 //      Marek Safar (marek.safar@gmail.com)\r
8 //\r
9 //\r
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
11 // Copyright 2011 Xamarin Inc.\r
12 //\r
13 // Permission is hereby granted, free of charge, to any person obtaining\r
14 // a copy of this software and associated documentation files (the\r
15 // "Software"), to deal in the Software without restriction, including\r
16 // without limitation the rights to use, copy, modify, merge, publish,\r
17 // distribute, sublicense, and/or sell copies of the Software, and to\r
18 // permit persons to whom the Software is furnished to do so, subject to\r
19 // the following conditions:\r
20 // \r
21 // The above copyright notice and this permission notice shall be\r
22 // included in all copies or substantial portions of the Software.\r
23 // \r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
31 //\r
32 \r
33 using System.Globalization;\r
34 using System.Text;\r
35 using System.Runtime.InteropServices;\r
36 #if NET_4_5\r
37 using System.Threading.Tasks;\r
38 #endif\r
39 \r
40 namespace System.IO\r
41 {\r
42         [Serializable]\r
43         [ComVisible (true)]\r
44         [MonoLimitation ("Serialization format not compatible with .NET")]\r
45         public class StringWriter : TextWriter\r
46         {\r
47                 private StringBuilder internalString;\r
48                 private bool disposed;\r
49 \r
50                 public StringWriter ()\r
51                         : this (new StringBuilder ())\r
52                 {\r
53                 }\r
54 \r
55                 public StringWriter (IFormatProvider formatProvider)\r
56                         : this (new StringBuilder (), formatProvider)\r
57                 {\r
58                 }\r
59 \r
60                 public StringWriter (StringBuilder sb)\r
61                         : this (sb, null)\r
62                 {\r
63                 }\r
64 \r
65                 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)\r
66                 {\r
67                         if (sb == null)\r
68                                 throw new ArgumentNullException ("sb");\r
69 \r
70                         internalString = sb;\r
71                         internalFormatProvider = formatProvider;\r
72                 }\r
73 \r
74                 public override Encoding Encoding {\r
75                         get {\r
76                                 return Encoding.Unicode;\r
77                         }\r
78                 }\r
79 \r
80                 public override void Close ()\r
81                 {\r
82                         Dispose (true);\r
83                         disposed = true;\r
84                 }\r
85 \r
86                 protected override void Dispose (bool disposing)\r
87                 {\r
88                         // MS.NET doesn't clear internal buffer.\r
89                         // internalString = null;\r
90                         base.Dispose (disposing);\r
91                         disposed = true;\r
92                 }\r
93 \r
94                 public virtual StringBuilder GetStringBuilder ()\r
95                 {\r
96                         return internalString;\r
97                 }\r
98 \r
99                 public override string ToString ()\r
100                 {\r
101                         return internalString.ToString ();\r
102                 }\r
103 \r
104                 public override void Write (char value)\r
105                 {\r
106                         if (disposed) {\r
107                                 throw new ObjectDisposedException ("StringReader",\r
108                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
109                         }\r
110 \r
111                         internalString.Append (value);\r
112                 }\r
113 \r
114                 public override void Write (string value)\r
115                 {\r
116                         if (disposed) {\r
117                                 throw new ObjectDisposedException ("StringReader",\r
118                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
119                         }\r
120 \r
121                         internalString.Append (value);\r
122                 }\r
123 \r
124                 public override void Write (char[] buffer, int index, int count)\r
125                 {\r
126                         if (disposed) {\r
127                                 throw new ObjectDisposedException ("StringReader",\r
128                                         Locale.GetText ("Cannot write to a closed StringWriter"));\r
129                         }\r
130                         if (buffer == null)\r
131                                 throw new ArgumentNullException ("buffer");\r
132                         if (index < 0)\r
133                                 throw new ArgumentOutOfRangeException ("index", "< 0");\r
134                         if (count < 0)\r
135                                 throw new ArgumentOutOfRangeException ("count", "< 0");\r
136                         // re-ordered to avoid possible integer overflow\r
137                         if (index > buffer.Length - count)\r
138                                 throw new ArgumentException ("index + count > buffer.Length");\r
139 \r
140                         internalString.Append (buffer, index, count);\r
141                 }\r
142 \r
143 #if NET_4_5\r
144                 public override Task FlushAsync ()\r
145                 {\r
146                         // it appears to do nothing\r
147                         return TaskConstants.Finished;\r
148                 }\r
149 \r
150                 //\r
151                 // All async methods return finished task with a result as it's faster\r
152                 // than setting up async call\r
153                 //\r
154                 public override Task WriteAsync (char value)\r
155                 {\r
156                         Write (value);\r
157                         return TaskConstants.Finished;\r
158                 }\r
159 \r
160                 public override Task WriteAsync (char[] buffer, int index, int count)\r
161                 {\r
162                         Write (buffer, index, count);\r
163                         return TaskConstants.Finished;\r
164                 }\r
165 \r
166                 public override Task WriteAsync (string value)\r
167                 {\r
168                         Write (value);\r
169                         return TaskConstants.Finished;\r
170                 }\r
171 \r
172                 public override Task WriteLineAsync (char value)\r
173                 {\r
174                         WriteLine (value);\r
175                         return TaskConstants.Finished;\r
176                 }\r
177 \r
178                 public override Task WriteLineAsync (char[] buffer, int index, int count)\r
179                 {\r
180                         WriteLine (buffer, index, count);\r
181                         return TaskConstants.Finished;\r
182                 }\r
183 \r
184                 public override Task WriteLineAsync (string value)\r
185                 {\r
186                         WriteLine (value);\r
187                         return TaskConstants.Finished;\r
188                 }\r
189 #endif\r
190         }\r
191 }