Merge pull request #2216 from akoeplinger/fix-array-sort
[mono.git] / mcs / class / corlib / System / Console.cs
index bfe6929b50032d6f7dc51d2ce922ff1de8462f84..206c3429d8d5632e17fc9fca10d52920a5a03b68 100644 (file)
@@ -7,7 +7,7 @@
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 // (C) 2004,2005 Novell, Inc. (http://www.novell.com)
-//
+// Copyright 2013 Xamarin Inc. (http://www.xamarin.com)
 
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -39,7 +39,7 @@ using System.Text;
 
 namespace System
 {
-       public static class Console
+       public static partial class Console
        {
 #if !NET_2_1
                private class WindowsConsole
@@ -94,12 +94,6 @@ namespace System
                private static TextWriter stderr;
                private static TextReader stdin;
 
-#if NET_4_5 && !MOBILE
-               static TextWriter console_stdout;
-               static TextWriter console_stderr;
-               static TextReader console_stdin;
-#endif
-
                static Console ()
                {
 #if NET_2_1
@@ -131,11 +125,11 @@ namespace System
                                // UTF-8 ZWNBSP (zero-width non-breaking space).
                                //
                                int code_page = 0;
-                               Encoding.InternalCodePage (ref code_page);
+                               EncodingHelper.InternalCodePage (ref code_page);
 
                                if (code_page != -1 && ((code_page & 0x0fffffff) == 3 // UTF8Encoding.UTF8_CODE_PAGE
                                        || ((code_page & 0x10000000) != 0)))
-                                       inputEncoding = outputEncoding = Encoding.UTF8Unmarked;
+                                       inputEncoding = outputEncoding = EncodingHelper.UTF8Unmarked;
                                else
                                        inputEncoding = outputEncoding = Encoding.Default;
                        }
@@ -147,33 +141,32 @@ namespace System
                {
 #if !NET_2_1
                        if (!Environment.IsRunningOnWindows && ConsoleDriver.IsConsole) {
-                               StreamWriter w = new CStreamWriter (OpenStandardOutput (0), outputEncoding);
+                               StreamWriter w = new CStreamWriter (OpenStandardOutput (0), outputEncoding, true);
                                w.AutoFlush = true;
-                               stdout = TextWriter.Synchronized (w, true);
+                               stdout = TextWriter.Synchronized (w);
 
-                               w = new CStreamWriter (OpenStandardOutput (0), outputEncoding);
+                               w = new CStreamWriter (OpenStandardOutput (0), outputEncoding, true);
                                w.AutoFlush = true;
-                               stderr = TextWriter.Synchronized (w, true);
+                               stderr = TextWriter.Synchronized (w);
                                
                                stdin = new CStreamReader (OpenStandardInput (0), inputEncoding);
                        } else {
 #endif
-#if FULL_AOT_RUNTIME
-                               Type nslogwriter = Type.GetType ("MonoTouch.Foundation.NSLogWriter, monotouch, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null");
-                               stdout = (TextWriter) Activator.CreateInstance (nslogwriter);
+#if MONOTOUCH
+                               stdout = new NSLogWriter ();
 #else
                                stdout = new UnexceptionalStreamWriter (OpenStandardOutput (0), outputEncoding);
                                ((StreamWriter)stdout).AutoFlush = true;
 #endif
-                               stdout = TextWriter.Synchronized (stdout, true);
+                               stdout = TextWriter.Synchronized (stdout);
 
-#if FULL_AOT_RUNTIME
-                               stderr = (TextWriter) Activator.CreateInstance (nslogwriter);
+#if MONOTOUCH
+                               stderr = new NSLogWriter ();
 #else
                                stderr = new UnexceptionalStreamWriter (OpenStandardError (0), outputEncoding); 
                                ((StreamWriter)stderr).AutoFlush = true;
 #endif
-                               stderr = TextWriter.Synchronized (stderr, true);
+                               stderr = TextWriter.Synchronized (stderr);
 
                                stdin = new UnexceptionalStreamReader (OpenStandardInput (0), inputEncoding);
                                stdin = TextReader.Synchronized (stdin);
@@ -181,12 +174,6 @@ namespace System
                        }
 #endif
 
-#if NET_4_5 && !MOBILE
-                       console_stderr = stderr;
-                       console_stdout = stdout;
-                       console_stdin = stdin;
-#endif
-
 #if MONODROID
                        if (LogcatTextWriter.IsRunningOnAndroid ()) {
                                stdout = TextWriter.Synchronized (new LogcatTextWriter ("mono-stdout", stdout));
@@ -220,9 +207,10 @@ namespace System
                private static Stream Open (IntPtr handle, FileAccess access, int bufferSize)
                {
                        try {
-                               return new FileStream (handle, access, false, bufferSize, false, bufferSize == 0);
+                               // TODO: Should use __ConsoleStream from reference sources
+                               return new FileStream (handle, access, false, bufferSize, false, true);
                        } catch (IOException) {
-                               return new NullStream ();
+                               return Stream.Null;
                        }
                }
 
@@ -671,25 +659,23 @@ namespace System
                        set { ConsoleDriver.WindowWidth = value; }
                }
 
-#if NET_4_5
                public static bool IsErrorRedirected {
                        get {
-                               return stderr != console_stderr || ConsoleDriver.IsErrorRedirected;
+                               return ConsoleDriver.IsErrorRedirected;
                        }
                }
 
                public static bool IsOutputRedirected {
                        get {
-                               return stdout != console_stdout || ConsoleDriver.IsOutputRedirected;
+                               return ConsoleDriver.IsOutputRedirected;
                        }
                }
 
                public static bool IsInputRedirected {
                        get {
-                               return stdin != console_stdin || ConsoleDriver.IsInputRedirected;
+                               return ConsoleDriver.IsInputRedirected;
                        }
                }
-#endif
 
                public static void Beep ()
                {