New test.
[mono.git] / mcs / class / corlib / System.IO / StreamReader.cs
index 3038379c67a9cc3c3c462533b1ecfe2e899f08fd..9102031f2f3ef4bfd276129172d6180c0f990282 100644 (file)
@@ -76,6 +76,7 @@ namespace System.IO {
 
                Stream base_stream;
                bool mayBlock;
+               StringBuilder line_builder;
 
                private class NullStreamReader : StreamReader {
                        public override int Peek ()
@@ -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);
@@ -283,6 +290,9 @@ namespace System.IO {
                public void DiscardBufferedData ()
                {
                        pos = decoded_count = 0;
+                       mayBlock = false;
+                       // Discard internal state of the decoder too.
+                       decoder = encoding.GetDecoder ();
                }
                
                // the buffer is empty, fill it again
@@ -298,7 +308,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);
@@ -369,47 +379,75 @@ namespace System.IO {
                        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()
@@ -423,7 +461,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 ();