2008-01-15 Stephane Delcroix <sdelcroix@novell.com>
[mono.git] / mcs / class / corlib / System.IO / StringWriter.cs
index 7e283b67b49cd7b704d57e5c4c11c16f38f15f8f..2b1bdc193e1f449096cf68f821dcec84703907ab 100644 (file)
 //\r
 // System.IO.StringWriter\r
 //\r
-// Author: Marcin Szczepanski (marcins@zipworld.com.au)\r
+// Authors\r
+//     Marcin Szczepanski (marcins@zipworld.com.au)\r
+//     Sebastien Pouliot  <sebastien@ximian.com>\r
+//\r
+// Copyright (C) 2004 Novell (http://www.novell.com)\r
+//\r
+\r
+//\r
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining\r
+// a copy of this software and associated documentation files (the\r
+// "Software"), to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify, merge, publish,\r
+// distribute, sublicense, and/or sell copies of the Software, and to\r
+// permit persons to whom the Software is furnished to do so, subject to\r
+// the following conditions:\r
+// \r
+// The above copyright notice and this permission notice shall be\r
+// included in all copies or substantial portions of the Software.\r
+// \r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
 //\r
 \r
+using System.Globalization;\r
 using System.Text;\r
 \r
+#if NET_2_0\r
+using System.Runtime.InteropServices;\r
+#endif\r
+\r
 namespace System.IO {\r
-               [Serializable]\r
-        public class StringWriter : TextWriter {\r
+\r
+       [Serializable]\r
+#if NET_2_0\r
+       [ComVisible (true)]\r
+#endif\r
+       [MonoTODO ("Serialization format not compatible with .NET")]\r
+       public class StringWriter : TextWriter {\r
                 \r
-                protected StringBuilder internalString;\r
+                private StringBuilder internalString;\r
+               private bool disposed = false;\r
 \r
-                public StringWriter() {\r
-                        internalString = new StringBuilder();\r
-                }\r
+                public StringWriter () : this (new StringBuilder ())\r
+               {\r
+               }\r
 \r
-                public StringWriter( IFormatProvider formatProvider ) {\r
-                        internalFormatProvider = formatProvider;\r
-                }\r
+                public StringWriter (IFormatProvider formatProvider) :\r
+                       this (new StringBuilder (), formatProvider)\r
+               {\r
+               }\r
 \r
-                public StringWriter( StringBuilder sb ) {\r
-                        internalString = sb;\r
-                }\r
+                public StringWriter (StringBuilder sb) :\r
+                       this (sb, null)\r
+               {\r
+               }\r
 \r
-                public StringWriter( StringBuilder sb, IFormatProvider formatProvider ) {\r
-                        internalString = sb;\r
-                        internalFormatProvider = formatProvider;\r
-                }\r
+                public StringWriter (StringBuilder sb, IFormatProvider formatProvider)\r
+               {\r
+                       if (sb == null)\r
+                               throw new ArgumentNullException ("sb");\r
+\r
+                       internalString = sb;\r
+                       internalFormatProvider = formatProvider;\r
+               }\r
 \r
                 public override System.Text.Encoding Encoding {\r
                         get {\r
-                                // TODO: Implement\r
-                                return null;\r
+                                return System.Text.Encoding.Unicode;\r
                         }\r
-                }\r
-\r
-                public override void Close() {\r
-                        Dispose( true );\r
-                }\r
-\r
-                protected override void Dispose( bool disposing ) { }\r
-\r
-                public virtual StringBuilder GetStringBuilder() {\r
-                        return internalString;\r
-                }\r
+               }\r
 \r
-                public override string ToString() {\r
-                        return internalString.ToString();\r
+                public override void Close ()\r
+               {\r
+                       Dispose (true);\r
+                       disposed = true;\r
                 }\r
 \r
-                public override void Write( char value ) {\r
-                        internalString.Append( value );\r
+                protected override void Dispose (bool disposing)\r
+               {\r
+                       // MS.NET doesn't clear internal buffer.\r
+                       // internalString = null;\r
+                       base.Dispose (disposing);\r
+                       disposed = true;\r
+               }\r
+\r
+                public virtual StringBuilder GetStringBuilder ()\r
+               {\r
+                       return internalString;\r
+               }\r
+\r
+                public override string ToString ()\r
+               {\r
+                       return internalString.ToString ();\r
                 }\r
 \r
-                public override void Write( string value ) {\r
-                        internalString.Append( value );\r
-                }\r
-\r
-                public override void Write( char[] buffer, int index, int count ) {\r
-                        if( buffer == null ) {\r
-                                throw new ArgumentNullException();\r
-                        } else if( index < 0 || count < 0 ) {\r
-                                throw new ArgumentOutOfRangeException();\r
-                        } else if( index > buffer.Length || index + count > buffer.Length ) {\r
-                                throw new ArgumentException();\r
-                        }\r
-                        \r
-                        char[] writeBuffer = new char[ count ];\r
-\r
-                        Array.Copy( buffer, index, writeBuffer, 0, count );\r
-\r
-                        internalString.Append( writeBuffer );\r
-                }\r
-                          \r
-        }\r
+                public override void Write (char value)\r
+               {\r
+                       if (disposed) {\r
+                               throw new ObjectDisposedException ("StringReader", \r
+                                       Locale.GetText ("Cannot write to a closed StringWriter"));\r
+                       }\r
+\r
+                        internalString.Append (value);\r
+               }\r
+\r
+                public override void Write (string value)\r
+               {\r
+                       if (disposed) {\r
+                               throw new ObjectDisposedException ("StringReader", \r
+                                       Locale.GetText ("Cannot write to a closed StringWriter"));\r
+                       }\r
+\r
+                       internalString.Append (value);\r
+               }\r
+\r
+                public override void Write (char[] buffer, int index, int count)\r
+               {\r
+                       if (disposed) {\r
+                               throw new ObjectDisposedException ("StringReader", \r
+                                       Locale.GetText ("Cannot write to a closed StringWriter"));\r
+                       }\r
+                       if (buffer == null)\r
+                               throw new ArgumentNullException ("buffer");\r
+                       if (index < 0)\r
+                               throw new ArgumentOutOfRangeException ("index", "< 0");\r
+                       if (count < 0)\r
+                               throw new ArgumentOutOfRangeException ("count", "< 0");\r
+                       // re-ordered to avoid possible integer overflow\r
+                        if (index > buffer.Length - count)\r
+                               throw new ArgumentException ("index + count > buffer.Length");\r
+\r
+                       internalString.Append (buffer, index, count);\r
+               }\r
+       }\r
 }\r
-                        \r
-                        \r
+              \r