2005-10-20 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.IO / StreamReader.cs
1 //
2 // System.IO.StreamReader.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Miguel de Icaza (miguel@ximian.com) 
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Text;
37 using System.Runtime.InteropServices;
38
39 namespace System.IO {
40         [Serializable]
41         public class StreamReader : TextReader {
42
43                 const int DefaultBufferSize = 1024;
44                 const int DefaultFileBufferSize = 4096;
45                 const int MinimumBufferSize = 128;
46
47                 //
48                 // The input buffer
49                 //
50                 byte [] input_buffer;
51
52                 //
53                 // The decoded buffer from the above input buffer
54                 //
55                 char [] decoded_buffer;
56
57                 //
58                 // Decoded bytes in decoded_buffer.
59                 //
60                 int decoded_count;
61
62                 //
63                 // Current position in the decoded_buffer
64                 //
65                 int pos;
66
67                 //
68                 // The buffer size that we are using
69                 //
70                 int buffer_size;
71
72                 int do_checks;
73                 
74                 Encoding encoding;
75                 Decoder decoder;
76
77                 Stream base_stream;
78                 bool mayBlock;
79                 StringBuilder line_builder;
80
81                 private class NullStreamReader : StreamReader {
82                         public override int Peek ()
83                         {
84                                 return -1;
85                         }
86
87                         public override int Read ()
88                         {
89                                 return -1;
90                         }
91
92                         public override int Read ([In, Out] char[] buffer, int index, int count)
93                         {
94                                 return 0;
95                         }
96
97                         public override string ReadLine ()
98                         {
99                                 return null;
100                         }
101
102                         public override string ReadToEnd ()
103                         {
104                                 return String.Empty;
105                         }
106
107                         public override Stream BaseStream
108                         {
109                                 get { return Stream.Null; }
110                         }
111
112                         public override Encoding CurrentEncoding
113                         {
114                                 get { return Encoding.Unicode; }
115                         }
116                 }
117
118                 public new static readonly StreamReader Null =  (StreamReader)(new NullStreamReader());
119                 
120                 internal StreamReader() {}
121
122                 public StreamReader(Stream stream)
123                         : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
124
125                 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
126                         : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
127
128                 public StreamReader(Stream stream, Encoding encoding)
129                         : this (stream, encoding, true, DefaultBufferSize) { }
130
131                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
132                         : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
133                 
134                 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
135                 {
136                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
137                 }
138
139                 public StreamReader(string path)
140                         : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
141
142                 public StreamReader(string path, bool detect_encoding_from_bytemarks)
143                         : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
144
145                 public StreamReader(string path, Encoding encoding)
146                         : this (path, encoding, true, DefaultFileBufferSize) { }
147
148                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
149                         : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
150                 
151                 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
152                 {
153                         if (null == path)
154                                 throw new ArgumentNullException("path");
155                         if (String.Empty == path)
156                                 throw new ArgumentException("Empty path not allowed");
157                         if (path.IndexOfAny (Path.InvalidPathChars) != -1)
158                                 throw new ArgumentException("path contains invalid characters");
159                         if (null == encoding)
160                                 throw new ArgumentNullException ("encoding");
161                         if (buffer_size <= 0)
162                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
163
164                         string DirName = Path.GetDirectoryName(path);
165                         if (DirName != String.Empty && !Directory.Exists(DirName))
166                                 throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
167                         if (!File.Exists(path))
168                                 throw new FileNotFoundException("File not found.", path);
169
170                         Stream stream = (Stream) File.OpenRead (path);
171                         Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
172                 }
173
174                 internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
175                 {
176                         if (null == stream)
177                                 throw new ArgumentNullException ("stream");
178                         if (null == encoding)
179                                 throw new ArgumentNullException ("encoding");
180                         if (!stream.CanRead)
181                                 throw new ArgumentException ("Cannot read stream");
182                         if (buffer_size <= 0)
183                                 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
184
185                         if (buffer_size < MinimumBufferSize)
186                                 buffer_size = MinimumBufferSize;
187
188                         base_stream = stream;
189                         input_buffer = new byte [buffer_size];
190                         this.buffer_size = buffer_size;
191                         this.encoding = encoding;
192                         decoder = encoding.GetDecoder ();
193
194                         byte [] preamble = encoding.GetPreamble ();
195                         do_checks = detect_encoding_from_bytemarks ? 1 : 0;
196                         do_checks += (preamble.Length == 0) ? 0 : 2;
197                         
198                         decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
199                         decoded_count = 0;
200                         pos = 0;
201                 }
202
203                 public virtual Stream BaseStream
204                 {
205                         get {
206                                 return base_stream;
207                         }
208                 }
209
210                 public virtual Encoding CurrentEncoding
211                 {
212                         get {
213                                 if (encoding == null)
214                                         throw new Exception ();
215                                 return encoding;
216                         }
217                 }
218
219                 public override void Close ()
220                 {
221                         Dispose (true);
222                 }
223
224                 protected override void Dispose (bool disposing)
225                 {
226                         if (disposing && base_stream != null)
227                                 base_stream.Close ();
228                         
229                         input_buffer = null;
230                         decoded_buffer = null;
231                         encoding = null;
232                         decoder = null;
233                         base_stream = null;
234                         base.Dispose (disposing);
235                 }
236
237                 //
238                 // Provides auto-detection of the encoding, as well as skipping over
239                 // byte marks at the beginning of a stream.
240                 //
241                 int DoChecks (int count)
242                 {
243                         if ((do_checks & 2) == 2){
244                                 byte [] preamble = encoding.GetPreamble ();
245                                 int c = preamble.Length;
246                                 if (count >= c){
247                                         int i;
248                                         
249                                         for (i = 0; i < c; i++)
250                                                 if (input_buffer [i] != preamble [i])
251                                                         break;
252
253                                         if (i == c)
254                                                 return i;
255                                 }
256                         }
257
258                         if ((do_checks & 1) == 1){
259                                 if (count < 2)
260                                         return 0;
261
262                                 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
263                                         this.encoding = Encoding.BigEndianUnicode;
264                                         return 2;
265                                 }
266
267                                 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
268                                         this.encoding = Encoding.Unicode;
269                                         return 2;
270                                 }
271
272                                 if (count < 3)
273                                         return 0;
274
275                                 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
276                                         this.encoding = Encoding.UTF8Unmarked;
277                                         return 3;
278                                 }
279                         }
280
281                         return 0;
282                 }
283
284                 public void DiscardBufferedData ()
285                 {
286                         pos = decoded_count = 0;
287                         mayBlock = false;
288                         // Discard internal state of the decoder too.
289                         decoder = encoding.GetDecoder ();
290                 }
291                 
292                 // the buffer is empty, fill it again
293                 private int ReadBuffer ()
294                 {
295                         pos = 0;
296                         int cbEncoded = 0;
297
298                         // keep looping until the decoder gives us some chars
299                         decoded_count = 0;
300                         int parse_start = 0;
301                         do      
302                         {
303                                 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
304                                 
305                                 if (cbEncoded == 0)
306                                         return 0;
307
308                                 mayBlock = (cbEncoded < buffer_size);
309                                 if (do_checks > 0){
310                                         Encoding old = encoding;
311                                         parse_start = DoChecks (cbEncoded);
312                                         if (old != encoding){
313                                                 decoder = encoding.GetDecoder ();
314                                         }
315                                         do_checks = 0;
316                                         cbEncoded -= parse_start;
317                                 }
318                                 
319                                 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
320                                 parse_start = 0;
321                         } while (decoded_count == 0);
322
323                         return decoded_count;
324                 }
325
326                 public override int Peek ()
327                 {
328                         if (base_stream == null)
329                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
330                         if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
331                                 return -1;
332
333                         return decoded_buffer [pos];
334                 }
335
336                 public override int Read ()
337                 {
338                         if (base_stream == null)
339                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
340                         if (pos >= decoded_count && ReadBuffer () == 0)
341                                 return -1;
342
343                         return decoded_buffer [pos++];
344                 }
345
346                 public override int Read ([In, Out] char[] dest_buffer, int index, int count)
347                 {
348                         if (base_stream == null)
349                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
350                         if (dest_buffer == null)
351                                 throw new ArgumentNullException ("dest_buffer");
352                         if (index < 0)
353                                 throw new ArgumentOutOfRangeException ("index", "< 0");
354                         if (count < 0)
355                                 throw new ArgumentOutOfRangeException ("count", "< 0");
356                         // re-ordered to avoid possible integer overflow
357                         if (index > dest_buffer.Length - count)
358                                 throw new ArgumentException ("index + count > dest_buffer.Length");
359
360                         int chars_read = 0;
361                         while (count > 0)
362                         {
363                                 if (pos >= decoded_count && ReadBuffer () == 0)
364                                         return chars_read > 0 ? chars_read : 0;
365
366                                 int cch = Math.Min (decoded_count - pos, count);
367                                 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
368                                 pos += cch;
369                                 index += cch;
370                                 count -= cch;
371                                 chars_read += cch;
372                         }
373                         return chars_read;
374                 }
375
376                 bool foundCR;
377                 int FindNextEOL ()
378                 {
379                         char c = '\0';
380                         for (; pos < decoded_count; pos++) {
381                                 c = decoded_buffer [pos];
382                                 if (c == '\n') {
383                                         pos++;
384                                         int res = (foundCR) ? (pos - 2) : (pos - 1);
385                                         if (res < 0)
386                                                 res = 0; // if a new buffer starts with a \n and there was a \r at
387                                                         // the end of the previous one, we get here.
388                                         foundCR = false;
389                                         return res;
390                                 } else if (foundCR) {
391                                         foundCR = false;
392                                         return pos - 1;
393                                 }
394
395                                 foundCR = (c == '\r');
396                         }
397
398                         return -1;
399                 }
400
401                 public override string ReadLine()
402                 {
403                         if (base_stream == null)
404                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
405
406                         if (pos >= decoded_count && ReadBuffer () == 0)
407                                 return null;
408
409                         int begin = pos;
410                         int end = FindNextEOL ();
411                         if (end < decoded_count && end >= begin)
412                                 return new string (decoded_buffer, begin, end - begin);
413
414                         if (line_builder == null)
415                                 line_builder = new StringBuilder ();
416                         else
417                                 line_builder.Length = 0;
418
419                         while (true) {
420                                 if (foundCR) // don't include the trailing CR if present
421                                         decoded_count--;
422
423                                 line_builder.Append (new string (decoded_buffer, begin, decoded_count - begin));
424                                 if (ReadBuffer () == 0) {
425                                         if (line_builder.Capacity > 32768) {
426                                                 StringBuilder sb = line_builder;
427                                                 line_builder = null;
428                                                 return sb.ToString (0, sb.Length);
429                                         }
430                                         return line_builder.ToString (0, line_builder.Length);
431                                 }
432
433                                 begin = pos;
434                                 end = FindNextEOL ();
435                                 if (end < decoded_count && end >= begin) {
436                                         line_builder.Append (new string (decoded_buffer, begin, end - begin));
437                                         if (line_builder.Capacity > 32768) {
438                                                 StringBuilder sb = line_builder;
439                                                 line_builder = null;
440                                                 return sb.ToString (0, sb.Length);
441                                         }
442                                         return line_builder.ToString (0, line_builder.Length);
443                                 }
444                         }
445                 }
446
447                 public override string ReadToEnd()
448                 {
449                         if (base_stream == null)
450                                 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
451
452                         StringBuilder text = new StringBuilder ();
453
454                         int size = decoded_buffer.Length;
455                         char [] buffer = new char [size];
456                         int len;
457                         
458                         while ((len = Read (buffer, 0, size)) != 0)
459                                 text.Append (buffer, 0, len);
460
461                         return text.ToString ();
462                 }
463         }
464 }