New tests.
[mono.git] / mcs / class / corlib / System.IO / StreamReader.cs
index 50e92b5b1cdbc7a236f707b13660f8aa6b87c3fa..279922b43edb9c6eea0923df3a38070fdaf1864b 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   Dietmar Maurer (dietmar@ximian.com)
 //   Miguel de Icaza (miguel@ximian.com) 
+//   Marek Safar (marek.safar@gmail.com)
 //
 // (C) Ximian, Inc.  http://www.ximian.com
 // Copyright (C) 2004 Novell (http://www.novell.com)
@@ -38,6 +39,7 @@ using System.Runtime.InteropServices;
 
 namespace System.IO {
        [Serializable]
+       [ComVisible (true)]
        public class StreamReader : TextReader {
 
                const int DefaultBufferSize = 1024;
@@ -48,11 +50,16 @@ namespace System.IO {
                // The input buffer
                //
                byte [] input_buffer;
+               
+               // Input buffer ready for recycling
+               static byte [] input_buffer_recycle;
+               static object input_buffer_recycle_lock = new object ();
 
                //
                // The decoded buffer from the above input buffer
                //
                char [] decoded_buffer;
+               static char[] decoded_buffer_recycle;
 
                //
                // Decoded bytes in decoded_buffer.
@@ -76,6 +83,7 @@ namespace System.IO {
 
                Stream base_stream;
                bool mayBlock;
+               StringBuilder line_builder;
 
                private class NullStreamReader : StreamReader {
                        public override int Peek ()
@@ -114,40 +122,40 @@ namespace System.IO {
                        }
                }
 
-               public new static readonly StreamReader Null =  (StreamReader)(new NullStreamReader());
+               public new static readonly StreamReader Null =  new NullStreamReader ();
                
                internal StreamReader() {}
 
                public StreamReader(Stream stream)
                        : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
 
-               public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
-                       : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
+               public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
+                       : this (stream, Encoding.UTF8Unmarked, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }
 
                public StreamReader(Stream stream, Encoding encoding)
                        : this (stream, encoding, true, DefaultBufferSize) { }
 
-               public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
-                       : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
+               public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
+                       : this (stream, encoding, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }
                
-               public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+               public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
                {
-                       Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
+                       Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);
                }
 
                public StreamReader(string path)
                        : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
 
-               public StreamReader(string path, bool detect_encoding_from_bytemarks)
-                       : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
+               public StreamReader(string path, bool detectEncodingFromByteOrderMarks)
+                       : this (path, Encoding.UTF8Unmarked, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }
 
                public StreamReader(string path, Encoding encoding)
                        : this (path, encoding, true, DefaultFileBufferSize) { }
 
-               public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
-                       : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
+               public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
+                       : this (path, encoding, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }
                
-               public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+               public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
                {
                        if (null == path)
                                throw new ArgumentNullException("path");
@@ -157,20 +165,14 @@ namespace System.IO {
                                throw new ArgumentException("path contains invalid characters");
                        if (null == encoding)
                                throw new ArgumentNullException ("encoding");
-                       if (buffer_size <= 0)
-                               throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
-
-                       string DirName = Path.GetDirectoryName(path);
-                       if (DirName != String.Empty && !Directory.Exists(DirName))
-                               throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
-                       if (!File.Exists(path))
-                               throw new FileNotFoundException("File not found.", path);
+                       if (bufferSize <= 0)
+                               throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
 
                        Stream stream = (Stream) File.OpenRead (path);
-                       Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
+                       Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);
                }
 
-               internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
+               internal void Initialize (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
                {
                        if (null == stream)
                                throw new ArgumentNullException ("stream");
@@ -178,23 +180,54 @@ namespace System.IO {
                                throw new ArgumentNullException ("encoding");
                        if (!stream.CanRead)
                                throw new ArgumentException ("Cannot read stream");
-                       if (buffer_size <= 0)
-                               throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
+                       if (bufferSize <= 0)
+                               throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
 
-                       if (buffer_size < MinimumBufferSize)
-                               buffer_size = MinimumBufferSize;
+                       if (bufferSize < MinimumBufferSize)
+                               bufferSize = MinimumBufferSize;
+                       
+                       // since GetChars() might add flushed character, it 
+                       // should have additional char buffer for extra 1 
+                       // (probably 1 is ok, but might be insufficient. I'm not sure)
+                       var decoded_buffer_size = encoding.GetMaxCharCount (bufferSize) + 1;
+
+                       //
+                       // Instead of allocating a new default buffer use the
+                       // last one if there is any available
+                       //
+                       if (bufferSize <= DefaultBufferSize && input_buffer_recycle != null) {
+                               lock (input_buffer_recycle_lock) {
+                                       if (input_buffer_recycle != null) {
+                                               input_buffer = input_buffer_recycle;
+                                               input_buffer_recycle = null;
+                                       }
+                                       
+                                       if (decoded_buffer_recycle != null && decoded_buffer_size <= decoded_buffer_recycle.Length) {
+                                               decoded_buffer = decoded_buffer_recycle;
+                                               decoded_buffer_recycle = null;
+                                       }
+                               }
+                       }
+                       
+                       if (input_buffer == null)
+                               input_buffer = new byte [bufferSize];
+                       else
+                               Array.Clear (input_buffer, 0, bufferSize);
+                       
+                       if (decoded_buffer == null)
+                               decoded_buffer = new char [decoded_buffer_size];
+                       else
+                               Array.Clear (decoded_buffer, 0, decoded_buffer_size);
 
-                       base_stream = stream;
-                       input_buffer = new byte [buffer_size];
-                       this.buffer_size = buffer_size;
+                       base_stream = stream;           
+                       this.buffer_size = bufferSize;
                        this.encoding = encoding;
                        decoder = encoding.GetDecoder ();
 
                        byte [] preamble = encoding.GetPreamble ();
-                       do_checks = detect_encoding_from_bytemarks ? 1 : 0;
+                       do_checks = detectEncodingFromByteOrderMarks ? 1 : 0;
                        do_checks += (preamble.Length == 0) ? 0 : 2;
                        
-                       decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
                        decoded_count = 0;
                        pos = 0;
                }
@@ -215,6 +248,10 @@ namespace System.IO {
                        }
                }
 
+               public bool EndOfStream {
+                       get { return Peek () < 0; }
+               }
+
                public override void Close ()
                {
                        Dispose (true);
@@ -225,6 +262,18 @@ namespace System.IO {
                        if (disposing && base_stream != null)
                                base_stream.Close ();
                        
+                       if (input_buffer != null && input_buffer.Length == DefaultBufferSize && input_buffer_recycle == null) {
+                               lock (input_buffer_recycle_lock) {
+                                       if (input_buffer_recycle == null) {
+                                               input_buffer_recycle = input_buffer;
+                                       }
+                                       
+                                       if (decoded_buffer_recycle == null) {
+                                               decoded_buffer_recycle = decoded_buffer;
+                                       }
+                               }
+                       }
+                       
                        input_buffer = null;
                        decoded_buffer = null;
                        encoding = null;
@@ -262,8 +311,8 @@ namespace System.IO {
                                        this.encoding = Encoding.BigEndianUnicode;
                                        return 2;
                                }
-
-                               if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
+                               if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe && count < 4) {
+                                       // If we don't have enough bytes we can't check for UTF32, so use Unicode
                                        this.encoding = Encoding.Unicode;
                                        return 2;
                                }
@@ -275,6 +324,31 @@ namespace System.IO {
                                        this.encoding = Encoding.UTF8Unmarked;
                                        return 3;
                                }
+
+                               if (count < 4) {
+                                       if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe && input_buffer [2] != 0) {
+                                               this.encoding = Encoding.Unicode;
+                                               return 2;
+                                       }
+                                       return 0;
+                               }
+
+                               if (input_buffer [0] == 0 && input_buffer [1] == 0
+                                       && input_buffer [2] == 0xfe && input_buffer [3] == 0xff)
+                               {
+                                       this.encoding = Encoding.BigEndianUTF32;
+                                       return 4;
+                               }
+
+                               if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe) {
+                                       if (input_buffer [2] == 0 && input_buffer[3] == 0) {
+                                               this.encoding = Encoding.UTF32;
+                                               return 4;
+                                       }
+
+                                       this.encoding = Encoding.Unicode;
+                                       return 2;
+                               }
                        }
 
                        return 0;
@@ -301,7 +375,7 @@ namespace System.IO {
                        {
                                cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
                                
-                               if (cbEncoded == 0)
+                               if (cbEncoded <= 0)
                                        return 0;
 
                                mayBlock = (cbEncoded < buffer_size);
@@ -309,6 +383,10 @@ namespace System.IO {
                                        Encoding old = encoding;
                                        parse_start = DoChecks (cbEncoded);
                                        if (old != encoding){
+                                               int old_decoded_size = old.GetMaxCharCount (buffer_size) + 1;
+                                               int new_decoded_size = encoding.GetMaxCharCount (buffer_size) + 1;
+                                               if (old_decoded_size != new_decoded_size)
+                                                       decoded_buffer = new char [new_decoded_size];
                                                decoder = encoding.GetDecoder ();
                                        }
                                        do_checks = 0;
@@ -322,16 +400,29 @@ namespace System.IO {
                        return decoded_count;
                }
 
+               //
+               // Peek can block:
+               // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=96484
+               //
                public override int Peek ()
                {
                        if (base_stream == null)
                                throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
-                       if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
+                       if (pos >= decoded_count && ReadBuffer () == 0)
                                return -1;
 
                        return decoded_buffer [pos];
                }
 
+               //
+               // Used internally by our console, as it previously depended on Peek() being a
+               // routine that would not block.
+               //
+               internal bool DataAvailable ()
+               {
+                       return pos < decoded_count;
+               }
+               
                public override int Read ()
                {
                        if (base_stream == null)
@@ -342,19 +433,19 @@ namespace System.IO {
                        return decoded_buffer [pos++];
                }
 
-               public override int Read ([In, Out] char[] dest_buffer, int index, int count)
+               public override int Read ([In, Out] char[] buffer, int index, int count)
                {
                        if (base_stream == null)
                                throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
-                       if (dest_buffer == null)
-                               throw new ArgumentNullException ("dest_buffer");
+                       if (buffer == null)
+                               throw new ArgumentNullException ("buffer");
                        if (index < 0)
                                throw new ArgumentOutOfRangeException ("index", "< 0");
                        if (count < 0)
                                throw new ArgumentOutOfRangeException ("count", "< 0");
                        // re-ordered to avoid possible integer overflow
-                       if (index > dest_buffer.Length - count)
-                               throw new ArgumentException ("index + count > dest_buffer.Length");
+                       if (index > buffer.Length - count)
+                               throw new ArgumentException ("index + count > buffer.Length");
 
                        int chars_read = 0;
                        while (count > 0)
@@ -363,56 +454,93 @@ namespace System.IO {
                                        return chars_read > 0 ? chars_read : 0;
 
                                int cch = Math.Min (decoded_count - pos, count);
-                               Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
+                               Array.Copy (decoded_buffer, pos, buffer, index, cch);
                                pos += cch;
                                index += cch;
                                count -= cch;
                                chars_read += cch;
+                               if (mayBlock)
+                                       break;
                        }
                        return chars_read;
                }
 
+               bool foundCR;
+               int FindNextEOL ()
+               {
+                       char c = '\0';
+                       for (; pos < decoded_count; pos++) {
+                               c = decoded_buffer [pos];
+                               if (c == '\n') {
+                                       pos++;
+                                       int res = (foundCR) ? (pos - 2) : (pos - 1);
+                                       if (res < 0)
+                                               res = 0; // if a new buffer starts with a \n and there was a \r at
+                                                       // the end of the previous one, we get here.
+                                       foundCR = false;
+                                       return res;
+                               } else if (foundCR) {
+                                       foundCR = false;
+                                       if (pos == 0)
+                                               return -2; // Need to flush the current buffered line.
+                                                          // This is a \r at the end of the previous decoded buffer that
+                                                          // is not followed by a \n in the current decoded buffer.
+                                       return pos - 1;
+                               }
+
+                               foundCR = (c == '\r');
+                       }
+
+                       return -1;
+               }
+
                public override string ReadLine()
                {
                        if (base_stream == null)
                                throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
-                       
-                       bool foundCR = false;
-                       StringBuilder text = new StringBuilder ();
 
-                       while (true) {
-                               int c = Read ();
-
-                               if (c == -1) {                          // end of stream
-                                       if (text.Length == 0)
-                                               return null;
+                       if (pos >= decoded_count && ReadBuffer () == 0)
+                               return null;
 
-                                       if (foundCR)
-                                               text.Length--;
+                       int begin = pos;
+                       int end = FindNextEOL ();
+                       if (end < decoded_count && end >= begin)
+                               return new string (decoded_buffer, begin, end - begin);
+                       else if (end == -2)
+                               return line_builder.ToString (0, line_builder.Length);
 
-                                       break;
-                               }
+                       if (line_builder == null)
+                               line_builder = new StringBuilder ();
+                       else
+                               line_builder.Length = 0;
 
-                               if (c == '\n') {                        // newline
-                                       if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
-                                               text.Length--;
-
-                                       foundCR = false;
-                                       break;
-                               } else if (foundCR) {
-                                       pos--;
-                                       text.Length--;
-                                       break;
+                       while (true) {
+                               if (foundCR) // don't include the trailing CR if present
+                                       decoded_count--;
+
+                               line_builder.Append (decoded_buffer, begin, decoded_count - begin);
+                               if (ReadBuffer () == 0) {
+                                       if (line_builder.Capacity > 32768) {
+                                               StringBuilder sb = line_builder;
+                                               line_builder = null;
+                                               return sb.ToString (0, sb.Length);
+                                       }
+                                       return line_builder.ToString (0, line_builder.Length);
                                }
 
-                               if (c == '\r')
-                                       foundCR = true;
-                                       
-
-                               text.Append ((char) c);
+                               begin = pos;
+                               end = FindNextEOL ();
+                               if (end < decoded_count && end >= begin) {
+                                       line_builder.Append (decoded_buffer, begin, end - begin);
+                                       if (line_builder.Capacity > 32768) {
+                                               StringBuilder sb = line_builder;
+                                               line_builder = null;
+                                               return sb.ToString (0, sb.Length);
+                                       }
+                                       return line_builder.ToString (0, line_builder.Length);
+                               } else if (end == -2)
+                                       return line_builder.ToString (0, line_builder.Length);
                        }
-
-                       return text.ToString ();
                }
 
                public override string ReadToEnd()
@@ -426,7 +554,7 @@ namespace System.IO {
                        char [] buffer = new char [size];
                        int len;
                        
-                       while ((len = Read (buffer, 0, size)) != 0)
+                       while ((len = Read (buffer, 0, size)) > 0)
                                text.Append (buffer, 0, len);
 
                        return text.ToString ();