2004-08-13 Dick Porter <dick@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / StreamWriter.cs
index 78b1425382a7856721e452ff504c24d88bcedf66..117ab7f5d60d3956726125d94fccec22526b9832 100644 (file)
@@ -7,6 +7,29 @@
 //\r
 // (C) Ximian, Inc.  http://www.ximian.com\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.
+//
 \r
 using System.Text;\r
 using System;\r
@@ -100,7 +123,7 @@ namespace System.IO {
                        else\r
                                mode = FileMode.Create;\r
                        \r
-                       internalStream = new FileStream (path, mode, FileAccess.Write);\r
+                       internalStream = new FileStream (path, mode, FileAccess.Write, FileShare.Read);\r
 \r
                        if (append)\r
                                internalStream.Position = internalStream.Length;\r
@@ -115,6 +138,8 @@ namespace System.IO {
                                return iflush;\r
                        }\r
                        set {\r
+                               if (DisposedAlready)\r
+                                       throw new ObjectDisposedException("StreamWriter");\r
                                iflush = value;\r
                        }\r
                }\r
@@ -131,7 +156,8 @@ namespace System.IO {
                        }\r
                }\r
 \r
-               protected override void Dispose (bool disposing) {\r
+               protected override void Dispose (bool disposing) \r
+               {\r
                        if (!DisposedAlready && disposing && internalStream != null) {\r
                                Flush();\r
                                DisposedAlready = true;\r
@@ -144,7 +170,8 @@ namespace System.IO {
                        decode_buf = null;\r
                }\r
 \r
-               public override void Flush () {\r
+               public override void Flush ()\r
+               {\r
                        if (DisposedAlready)\r
                                throw new ObjectDisposedException("StreamWriter");\r
 \r
@@ -160,7 +187,8 @@ namespace System.IO {
                // Decode () is called when the buffer is full or we need to flash.\r
                // Decode () will use the encoding to get the bytes and but them inside\r
                // byte_buf. From byte_buf the data is finally outputted to the stream.\r
-               void FlushBytes () {\r
+               void FlushBytes () \r
+               {\r
                        // write the encoding preamble only at the start of the stream\r
                        if (!preamble_done && byte_pos > 0) {\r
                                byte[] preamble = internalEncoding.GetPreamble ();\r
@@ -172,7 +200,8 @@ namespace System.IO {
                        byte_pos = 0;\r
                }\r
                \r
-               void Decode () {\r
+               void Decode () \r
+               {\r
                        if (byte_pos > 0)\r
                                FlushBytes ();\r
                        if (decode_pos > 0) {\r
@@ -182,23 +211,27 @@ namespace System.IO {
                        }\r
                }\r
                \r
-               public override void Write (char[] buffer, int index, int count) {\r
+               public override void Write (char[] buffer, int index, int count) \r
+               {\r
                        if (DisposedAlready)\r
                                throw new ObjectDisposedException("StreamWriter");\r
-\r
                        if (buffer == null)\r
                                throw new ArgumentNullException ("buffer");\r
-                       if (index < 0 || index > buffer.Length)\r
-                               throw new ArgumentOutOfRangeException ("index");\r
-                       if (count < 0 || (index + count) > buffer.Length)\r
-                               throw new ArgumentOutOfRangeException ("count");\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
                        LowLevelWrite (buffer, index, count);\r
                        if (iflush)\r
                                Flush();\r
                }\r
                \r
-               void LowLevelWrite (char[] buffer, int index, int count) {\r
+               void LowLevelWrite (char[] buffer, int index, int count)\r
+               {\r
                        while (count > 0) {\r
                                int todo = decode_buf.Length - decode_pos;\r
                                if (todo == 0) {\r
@@ -216,6 +249,9 @@ namespace System.IO {
 \r
                public override void Write (char value)\r
                {\r
+                       if (DisposedAlready)\r
+                               throw new ObjectDisposedException("StreamWriter");\r
+\r
                        // the size of decode_buf is always > 0 and\r
                        // we check for overflow right away\r
                        if (decode_pos >= decode_buf.Length)\r
@@ -225,41 +261,36 @@ namespace System.IO {
                                Flush ();\r
                }\r
 \r
-               public override void Write (char [] value)\r
+               public override void Write (char[] value)\r
                {\r
-                       LowLevelWrite (value, 0, value.Length);\r
-                       if (iflush)\r
-                               Flush ();\r
-               }\r
-\r
-               public override void Write (string value) {\r
                        if (DisposedAlready)\r
                                throw new ObjectDisposedException("StreamWriter");\r
 \r
                        if (value != null)\r
-                               LowLevelWrite (value.ToCharArray (), 0, value.Length);\r
+                               LowLevelWrite (value, 0, value.Length);\r
                        if (iflush)\r
                                Flush ();\r
                }\r
 \r
-               public override void WriteLine (string value) {\r
+               public override void Write (string value) \r
+               {\r
                        if (DisposedAlready)\r
                                throw new ObjectDisposedException("StreamWriter");\r
 \r
                        if (value != null)\r
                                LowLevelWrite (value.ToCharArray (), 0, value.Length);\r
-                       string nl = NewLine;\r
-                               LowLevelWrite (nl.ToCharArray (), 0, nl.Length);\r
                        if (iflush)\r
                                Flush ();\r
                }\r
 \r
                public override void Close()\r
                {\r
+                        closed = true;\r
                        Dispose (true);\r
                }\r
 \r
-               ~StreamWriter() {\r
+               ~StreamWriter ()\r
+               {\r
                        Dispose(false);\r
                }\r
        }\r