2008-01-15 Stephane Delcroix <sdelcroix@novell.com>
[mono.git] / mcs / class / corlib / System.IO / StreamReader.cs
index 77a3d15a1b427c2e3cb9bbeb6014d9b5902a025d..70e08f91967a70e2ed9f1fb55ce8af40e325f97d 100644 (file)
@@ -38,6 +38,9 @@ using System.Runtime.InteropServices;
 
 namespace System.IO {
        [Serializable]
+#if NET_2_0
+       [ComVisible (true)]
+#endif
        public class StreamReader : TextReader {
 
                const int DefaultBufferSize = 1024;
@@ -76,6 +79,7 @@ namespace System.IO {
 
                Stream base_stream;
                bool mayBlock;
+               StringBuilder line_builder;
 
                private class NullStreamReader : StreamReader {
                        public override int Peek ()
@@ -160,12 +164,6 @@ namespace System.IO {
                        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);
-
                        Stream stream = (Stream) File.OpenRead (path);
                        Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
                }
@@ -194,7 +192,10 @@ namespace System.IO {
                        do_checks = detect_encoding_from_bytemarks ? 1 : 0;
                        do_checks += (preamble.Length == 0) ? 0 : 2;
                        
-                       decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
+                       // 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)
+                       decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size) + 1];
                        decoded_count = 0;
                        pos = 0;
                }
@@ -215,6 +216,12 @@ namespace System.IO {
                        }
                }
 
+#if NET_2_0
+               public bool EndOfStream {
+                       get { return Peek () < 0; }
+               }
+#endif
+
                public override void Close ()
                {
                        Dispose (true);
@@ -258,13 +265,15 @@ namespace System.IO {
                                if (count < 2)
                                        return 0;
 
-                               if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
-                                       this.encoding = Encoding.BigEndianUnicode;
+#if !NET_2_0
+                               if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
+                                       this.encoding = Encoding.Unicode;
                                        return 2;
                                }
+#endif
 
-                               if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
-                                       this.encoding = Encoding.Unicode;
+                               if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
+                                       this.encoding = Encoding.BigEndianUnicode;
                                        return 2;
                                }
 
@@ -275,6 +284,33 @@ namespace System.IO {
                                        this.encoding = Encoding.UTF8Unmarked;
                                        return 3;
                                }
+
+#if NET_2_0
+                               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;
+                               }
+#endif
                        }
 
                        return 0;
@@ -284,6 +320,8 @@ namespace System.IO {
                {
                        pos = decoded_count = 0;
                        mayBlock = false;
+                       // Discard internal state of the decoder too.
+                       decoder = encoding.GetDecoder ();
                }
                
                // the buffer is empty, fill it again
@@ -299,7 +337,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);
@@ -366,51 +404,81 @@ namespace System.IO {
                                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;
+                                       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 (pos >= decoded_count && ReadBuffer () == 0)
+                               return null;
 
-                               if (c == -1) {                          // end of stream
-                                       if (text.Length == 0)
-                                               return null;
+                       int begin = pos;
+                       int end = FindNextEOL ();
+                       if (end < decoded_count && end >= begin)
+                               return new string (decoded_buffer, begin, end - begin);
 
-                                       if (foundCR)
-                                               text.Length--;
+                       if (line_builder == null)
+                               line_builder = new StringBuilder ();
+                       else
+                               line_builder.Length = 0;
 
-                                       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 == '\n') {                        // newline
-                                       if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
-                                               text.Length--;
-
-                                       foundCR = false;
-                                       break;
-                               } else if (foundCR) {
-                                       pos--;
-                                       text.Length--;
-                                       break;
+                               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);
                                }
-
-                               if (c == '\r')
-                                       foundCR = true;
-                                       
-
-                               text.Append ((char) c);
                        }
-
-                       return text.ToString ();
                }
 
                public override string ReadToEnd()
@@ -424,7 +492,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 ();