Merge pull request #325 from adbre/iss5464
[mono.git] / mcs / class / corlib / System / CStreamWriter.cs
index c9d77f0ca68e23bdd3540cc3d0c87050bd360096..85436f8be30a8f46802e781b6ad687e44fff7e9a 100644 (file)
@@ -31,7 +31,7 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-#if NET_2_0
+#if !NET_2_1
 using System;
 using System.Text;
 
@@ -49,22 +49,51 @@ namespace System.IO {
                {
                        if (count <= 0)
                                return;
+                       
+                       if (!driver.Initialized) {
+                               try {
+                                       base.Write (buffer, index, count);
+                               } catch (IOException) {
+                               }
+                               
+                               return;
+                       }
+                       
+                       lock (this) {
+                               int last = index + count;
+                               int i = index;
+                               int n = 0;
+                               char c;
+
+                               do {
+                                       c = buffer [i++];
 
-                       if (driver.Initialized){
-                               lock (this){
-                                       for (int i = 0; i < count; i++) {
-                                               try {
-                                                       char val = buffer [index + i];
-                                                       if (driver.NotifyWrite (val))
-                                                               InternalWriteChar (val);
-                                               } catch (IOException){
+                                       if (driver.IsSpecialKey (c)) {
+                                               // flush what we have
+                                               if (n > 0) {
+                                                       try {
+                                                               base.Write (buffer, index, n);
+                                                       } catch (IOException) {
+                                                       }
+                                                       
+                                                       n = 0;
                                                }
+
+                                               // write the special key
+                                               driver.WriteSpecialKey (c);
+
+                                               index = i;
+                                       } else {
+                                               n++;
+                                       }
+                               } while (i < last);
+
+                               if (n > 0) {
+                                       // write out the remainder of the buffer
+                                       try {
+                                               base.Write (buffer, index, n);
+                                       } catch (IOException) {
                                        }
-                               }
-                       } else {
-                               try {
-                                       base.Write (buffer, index, count);
-                               } catch (IOException){
                                }
                        }
                }
@@ -73,22 +102,26 @@ namespace System.IO {
                {
                        lock (this) {
                                try {
-                                       if (driver.NotifyWrite (val))
+                                       if (driver.IsSpecialKey (val))
+                                               driver.WriteSpecialKey (val);
+                                       else
                                                InternalWriteChar (val);
                                } catch (IOException) {
                                }
                        }
                }
-
+/*
                public void WriteKey (ConsoleKeyInfo key)
                {
                        lock (this) {
                                ConsoleKeyInfo copy = new ConsoleKeyInfo (key);
-                               if (driver.NotifyWrite (copy))
+                               if (driver.IsSpecialKey (copy))
+                                       driver.WriteSpecialKey (copy);
+                               else
                                        InternalWriteChar (copy.KeyChar);
                        }
                }
-
+*/
                public void InternalWriteString (string val)
                {
                        try {
@@ -105,6 +138,14 @@ namespace System.IO {
                        }
                }
 
+               public void InternalWriteChars (char [] buffer, int n)
+               {
+                       try {
+                               base.Write (buffer, 0, n);
+                       } catch (IOException) {
+                       }
+               }
+
                public override void Write (char [] val)
                {
                        Write (val, 0, val.Length);