2 // System.IO.StreamReader.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
37 using System.Runtime.InteropServices;
44 public class StreamReader : TextReader {
46 const int DefaultBufferSize = 1024;
47 const int DefaultFileBufferSize = 4096;
48 const int MinimumBufferSize = 128;
56 // The decoded buffer from the above input buffer
58 char [] decoded_buffer;
61 // Decoded bytes in decoded_buffer.
66 // Current position in the decoded_buffer
71 // The buffer size that we are using
82 StringBuilder line_builder;
84 private class NullStreamReader : StreamReader {
85 public override int Peek ()
90 public override int Read ()
95 public override int Read ([In, Out] char[] buffer, int index, int count)
100 public override string ReadLine ()
105 public override string ReadToEnd ()
110 public override Stream BaseStream
112 get { return Stream.Null; }
115 public override Encoding CurrentEncoding
117 get { return Encoding.Unicode; }
121 public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
123 internal StreamReader() {}
125 public StreamReader(Stream stream)
126 : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
128 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
129 : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
131 public StreamReader(Stream stream, Encoding encoding)
132 : this (stream, encoding, true, DefaultBufferSize) { }
134 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
135 : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
137 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
139 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
142 public StreamReader(string path)
143 : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
145 public StreamReader(string path, bool detect_encoding_from_bytemarks)
146 : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
148 public StreamReader(string path, Encoding encoding)
149 : this (path, encoding, true, DefaultFileBufferSize) { }
151 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
152 : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
154 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
157 throw new ArgumentNullException("path");
158 if (String.Empty == path)
159 throw new ArgumentException("Empty path not allowed");
160 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
161 throw new ArgumentException("path contains invalid characters");
162 if (null == encoding)
163 throw new ArgumentNullException ("encoding");
164 if (buffer_size <= 0)
165 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
167 Stream stream = (Stream) File.OpenRead (path);
168 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
171 internal void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
174 throw new ArgumentNullException ("stream");
175 if (null == encoding)
176 throw new ArgumentNullException ("encoding");
178 throw new ArgumentException ("Cannot read stream");
179 if (buffer_size <= 0)
180 throw new ArgumentOutOfRangeException ("buffer_size", "The minimum size of the buffer must be positive");
182 if (buffer_size < MinimumBufferSize)
183 buffer_size = MinimumBufferSize;
185 base_stream = stream;
186 input_buffer = new byte [buffer_size];
187 this.buffer_size = buffer_size;
188 this.encoding = encoding;
189 decoder = encoding.GetDecoder ();
191 byte [] preamble = encoding.GetPreamble ();
192 do_checks = detect_encoding_from_bytemarks ? 1 : 0;
193 do_checks += (preamble.Length == 0) ? 0 : 2;
195 // since GetChars() might add flushed character, it
196 // should have additional char buffer for extra 1
197 // (probably 1 is ok, but might be insufficient. I'm not sure)
198 decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size) + 1];
203 public virtual Stream BaseStream
210 public virtual Encoding CurrentEncoding
213 if (encoding == null)
214 throw new Exception ();
220 public bool EndOfStream {
221 get { return Peek () < 0; }
225 public override void Close ()
230 protected override void Dispose (bool disposing)
232 if (disposing && base_stream != null)
233 base_stream.Close ();
236 decoded_buffer = null;
240 base.Dispose (disposing);
244 // Provides auto-detection of the encoding, as well as skipping over
245 // byte marks at the beginning of a stream.
247 int DoChecks (int count)
249 if ((do_checks & 2) == 2){
250 byte [] preamble = encoding.GetPreamble ();
251 int c = preamble.Length;
255 for (i = 0; i < c; i++)
256 if (input_buffer [i] != preamble [i])
264 if ((do_checks & 1) == 1){
269 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
270 this.encoding = Encoding.Unicode;
275 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
276 this.encoding = Encoding.BigEndianUnicode;
283 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
284 this.encoding = Encoding.UTF8Unmarked;
290 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe && input_buffer [2] != 0) {
291 this.encoding = Encoding.Unicode;
297 if (input_buffer [0] == 0 && input_buffer [1] == 0
298 && input_buffer [2] == 0xfe && input_buffer [3] == 0xff)
300 this.encoding = Encoding.BigEndianUTF32;
304 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe) {
305 if (input_buffer [2] == 0 && input_buffer[3] == 0) {
306 this.encoding = Encoding.UTF32;
310 this.encoding = Encoding.Unicode;
319 public void DiscardBufferedData ()
321 pos = decoded_count = 0;
323 // Discard internal state of the decoder too.
324 decoder = encoding.GetDecoder ();
327 // the buffer is empty, fill it again
328 private int ReadBuffer ()
333 // keep looping until the decoder gives us some chars
338 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
343 mayBlock = (cbEncoded < buffer_size);
345 Encoding old = encoding;
346 parse_start = DoChecks (cbEncoded);
347 if (old != encoding){
348 decoder = encoding.GetDecoder ();
351 cbEncoded -= parse_start;
354 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
356 } while (decoded_count == 0);
358 return decoded_count;
361 public override int Peek ()
363 if (base_stream == null)
364 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
365 if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
368 return decoded_buffer [pos];
371 public override int Read ()
373 if (base_stream == null)
374 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
375 if (pos >= decoded_count && ReadBuffer () == 0)
378 return decoded_buffer [pos++];
381 public override int Read ([In, Out] char[] dest_buffer, int index, int count)
383 if (base_stream == null)
384 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
385 if (dest_buffer == null)
386 throw new ArgumentNullException ("dest_buffer");
388 throw new ArgumentOutOfRangeException ("index", "< 0");
390 throw new ArgumentOutOfRangeException ("count", "< 0");
391 // re-ordered to avoid possible integer overflow
392 if (index > dest_buffer.Length - count)
393 throw new ArgumentException ("index + count > dest_buffer.Length");
398 if (pos >= decoded_count && ReadBuffer () == 0)
399 return chars_read > 0 ? chars_read : 0;
401 int cch = Math.Min (decoded_count - pos, count);
402 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
417 for (; pos < decoded_count; pos++) {
418 c = decoded_buffer [pos];
421 int res = (foundCR) ? (pos - 2) : (pos - 1);
423 res = 0; // if a new buffer starts with a \n and there was a \r at
424 // the end of the previous one, we get here.
427 } else if (foundCR) {
432 foundCR = (c == '\r');
438 public override string ReadLine()
440 if (base_stream == null)
441 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
443 if (pos >= decoded_count && ReadBuffer () == 0)
447 int end = FindNextEOL ();
448 if (end < decoded_count && end >= begin)
449 return new string (decoded_buffer, begin, end - begin);
451 if (line_builder == null)
452 line_builder = new StringBuilder ();
454 line_builder.Length = 0;
457 if (foundCR) // don't include the trailing CR if present
460 line_builder.Append (decoded_buffer, begin, decoded_count - begin);
461 if (ReadBuffer () == 0) {
462 if (line_builder.Capacity > 32768) {
463 StringBuilder sb = line_builder;
465 return sb.ToString (0, sb.Length);
467 return line_builder.ToString (0, line_builder.Length);
471 end = FindNextEOL ();
472 if (end < decoded_count && end >= begin) {
473 line_builder.Append (decoded_buffer, begin, end - begin);
474 if (line_builder.Capacity > 32768) {
475 StringBuilder sb = line_builder;
477 return sb.ToString (0, sb.Length);
479 return line_builder.ToString (0, line_builder.Length);
484 public override string ReadToEnd()
486 if (base_stream == null)
487 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
489 StringBuilder text = new StringBuilder ();
491 int size = decoded_buffer.Length;
492 char [] buffer = new char [size];
495 while ((len = Read (buffer, 0, size)) > 0)
496 text.Append (buffer, 0, len);
498 return text.ToString ();