2 // System.IO.StringWriter
\r
5 // Marcin Szczepanski (marcins@zipworld.com.au)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2004 Novell (http://www.novell.com)
\r
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Globalization;
\r
37 namespace System.IO {
\r
40 [MonoTODO ("Fix serialization compatibility with MS.NET")]
\r
41 public class StringWriter : TextWriter {
\r
43 private StringBuilder internalString;
\r
44 private bool disposed = false;
\r
46 public StringWriter () : this (new StringBuilder ())
50 public StringWriter (IFormatProvider formatProvider) :
51 this (new StringBuilder (), formatProvider)
55 public StringWriter (StringBuilder sb) :
60 public StringWriter (StringBuilder sb, IFormatProvider formatProvider)
63 throw new ArgumentNullException ("sb");
\r
65 internalString = sb;
\r
66 internalFormatProvider = formatProvider;
\r
69 public override System.Text.Encoding Encoding {
\r
71 return System.Text.Encoding.Unicode;
\r
75 public override void Close ()
81 protected override void Dispose (bool disposing)
\r
83 // MS.NET doesn't clear internal buffer.
\r
84 // internalString = null;
\r
85 base.Dispose (disposing);
\r
89 public virtual StringBuilder GetStringBuilder ()
91 return internalString;
\r
94 public override string ToString ()
96 return internalString.ToString ();
\r
99 public override void Write (char value)
102 throw new ObjectDisposedException ("StringReader",
103 Locale.GetText ("Cannot write to a closed StringWriter"));
106 internalString.Append (value);
\r
109 public override void Write (string value)
112 throw new ObjectDisposedException ("StringReader",
113 Locale.GetText ("Cannot write to a closed StringWriter"));
116 internalString.Append (value);
\r
119 public override void Write (char[] buffer, int index, int count)
122 throw new ObjectDisposedException ("StringReader",
123 Locale.GetText ("Cannot write to a closed StringWriter"));
125 if (buffer == null)
\r
126 throw new ArgumentNullException ("buffer");
128 throw new ArgumentOutOfRangeException ("index", "< 0");
130 throw new ArgumentOutOfRangeException ("count", "< 0");
131 // re-ordered to avoid possible integer overflow
132 if (index > buffer.Length - count)
\r
133 throw new ArgumentException ("index + count > buffer.Length");
\r
135 internalString.Append (buffer, index, count);
\r