2 // System.IO.StringWriter
\r
5 // Marcin Szczepanski (marcins@zipworld.com.au)
\r
6 // Sebastien Pouliot <sebastien@ximian.com>
\r
8 // Copyright (C) 2004 Novell (http://www.novell.com)
\r
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
\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
22 // The above copyright notice and this permission notice shall be
\r
23 // included in all copies or substantial portions of the Software.
\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
34 using System.Globalization;
\r
36 using System.Runtime.InteropServices;
\r
38 namespace System.IO {
\r
42 [MonoLimitation ("Serialization format not compatible with .NET")]
\r
43 public class StringWriter : TextWriter {
\r
45 private StringBuilder internalString;
\r
46 private bool disposed = false;
\r
48 public StringWriter () : this (new StringBuilder ())
\r
52 public StringWriter (IFormatProvider formatProvider) :
\r
53 this (new StringBuilder (), formatProvider)
\r
57 public StringWriter (StringBuilder sb) :
\r
62 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
\r
65 throw new ArgumentNullException ("sb");
\r
67 internalString = sb;
\r
68 internalFormatProvider = formatProvider;
\r
71 public override System.Text.Encoding Encoding {
\r
73 return System.Text.Encoding.Unicode;
\r
77 public override void Close ()
\r
83 protected override void Dispose (bool disposing)
\r
85 // MS.NET doesn't clear internal buffer.
\r
86 // internalString = null;
\r
87 base.Dispose (disposing);
\r
91 public virtual StringBuilder GetStringBuilder ()
\r
93 return internalString;
\r
96 public override string ToString ()
\r
98 return internalString.ToString ();
\r
101 public override void Write (char value)
\r
104 throw new ObjectDisposedException ("StringReader",
\r
105 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
108 internalString.Append (value);
\r
111 public override void Write (string value)
\r
114 throw new ObjectDisposedException ("StringReader",
\r
115 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
118 internalString.Append (value);
\r
121 public override void Write (char[] buffer, int index, int count)
\r
124 throw new ObjectDisposedException ("StringReader",
\r
125 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
127 if (buffer == null)
\r
128 throw new ArgumentNullException ("buffer");
\r
130 throw new ArgumentOutOfRangeException ("index", "< 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
137 internalString.Append (buffer, index, count);
\r