2 // System.IO.StringWriter
\r
5 // Marcin Szczepanski (marcins@zipworld.com.au)
\r
6 // Sebastien Pouliot <sebastien@ximian.com>
\r
7 // Marek Safar (marek.safar@gmail.com)
\r
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\r
11 // Copyright 2011 Xamarin Inc.
\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
21 // The above copyright notice and this permission notice shall be
\r
22 // included in all copies or substantial portions of the Software.
\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
33 using System.Globalization;
\r
35 using System.Runtime.InteropServices;
\r
37 using System.Threading.Tasks;
\r
44 [MonoLimitation ("Serialization format not compatible with .NET")]
\r
45 public class StringWriter : TextWriter
\r
47 private StringBuilder internalString;
\r
48 private bool disposed;
\r
50 public StringWriter ()
\r
51 : this (new StringBuilder ())
\r
55 public StringWriter (IFormatProvider formatProvider)
\r
56 : this (new StringBuilder (), formatProvider)
\r
60 public StringWriter (StringBuilder sb)
\r
65 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
\r
68 throw new ArgumentNullException ("sb");
\r
70 internalString = sb;
\r
71 internalFormatProvider = formatProvider;
\r
74 public override Encoding Encoding {
\r
76 return Encoding.Unicode;
\r
80 public override void Close ()
\r
86 protected override void Dispose (bool disposing)
\r
88 // MS.NET doesn't clear internal buffer.
\r
89 // internalString = null;
\r
90 base.Dispose (disposing);
\r
94 public virtual StringBuilder GetStringBuilder ()
\r
96 return internalString;
\r
99 public override string ToString ()
\r
101 return internalString.ToString ();
\r
104 public override void Write (char value)
\r
107 throw new ObjectDisposedException ("StringReader",
\r
108 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
111 internalString.Append (value);
\r
114 public override void Write (string value)
\r
117 throw new ObjectDisposedException ("StringReader",
\r
118 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
121 internalString.Append (value);
\r
124 public override void Write (char[] buffer, int index, int count)
\r
127 throw new ObjectDisposedException ("StringReader",
\r
128 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
130 if (buffer == null)
\r
131 throw new ArgumentNullException ("buffer");
\r
133 throw new ArgumentOutOfRangeException ("index", "< 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
140 internalString.Append (buffer, index, count);
\r
144 public override Task FlushAsync ()
\r
146 // it appears to do nothing
\r
147 return TaskConstants.Finished;
\r
151 // All async methods return finished task with a result as it's faster
\r
152 // than setting up async call
\r
154 public override Task WriteAsync (char value)
\r
157 return TaskConstants.Finished;
\r
160 public override Task WriteAsync (char[] buffer, int index, int count)
\r
162 Write (buffer, index, count);
\r
163 return TaskConstants.Finished;
\r
166 public override Task WriteAsync (string value)
\r
169 return TaskConstants.Finished;
\r
172 public override Task WriteLineAsync (char value)
\r
175 return TaskConstants.Finished;
\r
178 public override Task WriteLineAsync (char[] buffer, int index, int count)
\r
180 WriteLine (buffer, index, count);
\r
181 return TaskConstants.Finished;
\r
184 public override Task WriteLineAsync (string value)
\r
187 return TaskConstants.Finished;
\r