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