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
17 public class StreamReader : TextReader {
19 private const int DefaultBufferSize = 1024;
20 private const int DefaultFileBufferSize = 4096;
21 private const int MinimumBufferSize = 128;
26 private byte [] input_buffer;
29 // The decoded buffer from the above input buffer
31 private char [] decoded_buffer;
34 // Decoded bytes in decoded_buffer.
36 private int decoded_count;
39 // Current position in the decoded_buffer
44 // The buffer size that we are using
46 private int buffer_size;
49 // Index into `input_buffer' where we start decoding
51 private int parse_start;
55 private Encoding encoding;
56 private Decoder decoder;
58 private Stream base_stream;
59 private bool mayBlock;
61 private class NullStreamReader : StreamReader {
62 public override int Peek ()
67 public override int Read ()
72 public override int Read (char[] buffer, int index, int count)
77 public override string ReadLine ()
82 public override string ReadToEnd ()
87 public override Stream BaseStream
89 get { return Stream.Null; }
92 public override Encoding CurrentEncoding
94 get { return Encoding.Unicode; }
98 public new static readonly StreamReader Null = (StreamReader)(new NullStreamReader());
100 internal StreamReader() {}
102 public StreamReader(Stream stream)
103 : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
105 public StreamReader(Stream stream, bool detect_encoding_from_bytemarks)
106 : this (stream, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultBufferSize) { }
108 public StreamReader(Stream stream, Encoding encoding)
109 : this (stream, encoding, true, DefaultBufferSize) { }
111 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks)
112 : this (stream, encoding, detect_encoding_from_bytemarks, DefaultBufferSize) { }
114 public StreamReader(Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
116 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
119 public StreamReader(string path)
120 : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
122 public StreamReader(string path, bool detect_encoding_from_bytemarks)
123 : this (path, Encoding.UTF8Unmarked, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
125 public StreamReader(string path, Encoding encoding)
126 : this (path, encoding, true, DefaultFileBufferSize) { }
128 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks)
129 : this (path, encoding, detect_encoding_from_bytemarks, DefaultFileBufferSize) { }
131 public StreamReader(string path, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
134 throw new ArgumentNullException("path");
135 if (String.Empty == path)
136 throw new ArgumentException("Empty path not allowed");
137 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
138 throw new ArgumentException("path contains invalid characters");
140 string DirName = Path.GetDirectoryName(path);
141 if (DirName != String.Empty && !Directory.Exists(DirName))
142 throw new DirectoryNotFoundException ("Directory '" + DirName + "' not found.");
143 if (!File.Exists(path))
144 throw new FileNotFoundException(path);
146 Stream stream = (Stream) File.OpenRead (path);
147 Initialize (stream, encoding, detect_encoding_from_bytemarks, buffer_size);
150 protected void Initialize (Stream stream, Encoding encoding, bool detect_encoding_from_bytemarks, int buffer_size)
153 throw new ArgumentNullException("stream");
155 throw new ArgumentException("Cannot read stream");
157 if (buffer_size < MinimumBufferSize)
158 buffer_size = MinimumBufferSize;
160 base_stream = stream;
161 input_buffer = new byte [buffer_size];
162 this.buffer_size = buffer_size;
163 this.encoding = encoding;
164 decoder = encoding.GetDecoder ();
166 byte [] preamble = encoding.GetPreamble ();
167 do_checks = detect_encoding_from_bytemarks ? 1 : 0;
168 do_checks += (preamble.Length == 0) ? 0 : 2;
170 decoded_buffer = new char [encoding.GetMaxCharCount (buffer_size)];
175 public virtual Stream BaseStream
182 public virtual Encoding CurrentEncoding
185 if (encoding == null)
186 throw new Exception ();
191 public override void Close ()
196 protected override void Dispose (bool disposing)
198 if (disposing && base_stream != null)
199 base_stream.Close ();
202 decoded_buffer = null;
206 base.Dispose (disposing);
210 // Provides auto-detection of the encoding, as well as skipping over
211 // byte marks at the beginning of a stream.
213 int DoChecks (int count)
215 if ((do_checks & 2) == 2){
216 byte [] preamble = encoding.GetPreamble ();
217 int c = preamble.Length;
221 for (i = 0; i < c; i++)
222 if (input_buffer [i] != preamble [i])
230 if ((do_checks & 1) == 1){
234 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
235 this.encoding = Encoding.BigEndianUnicode;
239 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe){
240 this.encoding = Encoding.Unicode;
247 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
248 this.encoding = Encoding.UTF8Unmarked;
256 // the buffer is empty, fill it again
257 private int ReadBuffer ()
262 // keep looping until the decoder gives us some chars
267 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
272 mayBlock = (cbEncoded < buffer_size);
274 Encoding old = encoding;
275 parse_start = DoChecks (cbEncoded);
276 if (old != encoding){
277 decoder = encoding.GetDecoder ();
280 cbEncoded -= parse_start;
283 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
285 } while (decoded_count == 0);
287 return decoded_count;
290 public override int Peek ()
292 if (pos >= decoded_count && (mayBlock || ReadBuffer () == 0))
295 return decoded_buffer [pos];
298 public override int Read ()
300 if (pos >= decoded_count && ReadBuffer () == 0)
303 return decoded_buffer [pos++];
306 public override int Read (char[] dest_buffer, int index, int count)
308 if (dest_buffer == null)
309 throw new ArgumentException ();
311 if ((index < 0) || (count < 0))
312 throw new ArgumentOutOfRangeException ();
314 if (index + count > dest_buffer.Length)
315 throw new ArgumentException ();
320 if (pos >= decoded_count && ReadBuffer () == 0)
321 return chars_read > 0 ? chars_read : 0;
323 int cch = Math.Min (decoded_count - pos, count);
324 Array.Copy (decoded_buffer, pos, dest_buffer, index, cch);
333 public override string ReadLine()
335 bool foundCR = false;
336 StringBuilder text = new StringBuilder ();
341 if (c == -1) { // end of stream
342 if (text.Length == 0)
351 if (c == '\n') { // newline
352 if ((text.Length > 0) && (text [text.Length - 1] == '\r'))
357 } else if (foundCR) {
367 text.Append ((char) c);
370 return text.ToString ();
373 public override string ReadToEnd()
375 StringBuilder text = new StringBuilder ();
377 int size = decoded_buffer.Length;
378 char [] buffer = new char [size];
381 while ((len = Read (buffer, 0, size)) != 0)
382 text.Append (buffer, 0, len);
384 return text.ToString ();