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
38 using System.Runtime.InteropServices;
\r
41 namespace System.IO {
\r
47 [MonoTODO ("Serialization format not compatible with .NET")]
\r
48 public class StringWriter : TextWriter {
\r
50 private StringBuilder internalString;
\r
51 private bool disposed = false;
\r
53 public StringWriter () : this (new StringBuilder ())
\r
57 public StringWriter (IFormatProvider formatProvider) :
\r
58 this (new StringBuilder (), formatProvider)
\r
62 public StringWriter (StringBuilder sb) :
\r
67 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
\r
70 throw new ArgumentNullException ("sb");
\r
72 internalString = sb;
\r
73 internalFormatProvider = formatProvider;
\r
76 public override System.Text.Encoding Encoding {
\r
78 return System.Text.Encoding.Unicode;
\r
82 public override void Close ()
\r
88 protected override void Dispose (bool disposing)
\r
90 // MS.NET doesn't clear internal buffer.
\r
91 // internalString = null;
\r
92 base.Dispose (disposing);
\r
96 public virtual StringBuilder GetStringBuilder ()
\r
98 return internalString;
\r
101 public override string ToString ()
\r
103 return internalString.ToString ();
\r
106 public override void Write (char value)
\r
109 throw new ObjectDisposedException ("StringReader",
\r
110 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
113 internalString.Append (value);
\r
116 public override void Write (string value)
\r
119 throw new ObjectDisposedException ("StringReader",
\r
120 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
123 internalString.Append (value);
\r
126 public override void Write (char[] buffer, int index, int count)
\r
129 throw new ObjectDisposedException ("StringReader",
\r
130 Locale.GetText ("Cannot write to a closed StringWriter"));
\r
132 if (buffer == null)
\r
133 throw new ArgumentNullException ("buffer");
\r
135 throw new ArgumentOutOfRangeException ("index", "< 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
142 internalString.Append (buffer, index, count);
\r