2 // System.IO.StreamReader.cs
5 // Dietmar Maurer (dietmar@ximian.com)
6 // Miguel de Icaza (miguel@ximian.com)
7 // Marek Safar (marek.safar@gmail.com)
9 // (C) Ximian, Inc. http://www.ximian.com
10 // Copyright (C) 2004 Novell (http://www.novell.com)
11 // Copyright 2011 Xamarin Inc.
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.InteropServices;
37 using System.Threading.Tasks;
43 public class StreamReader : TextReader
45 sealed class NullStreamReader : StreamReader
47 public override int Peek ()
52 public override int Read ()
57 public override int Read ([In, Out] char[] buffer, int index, int count)
62 public override string ReadLine ()
67 public override string ReadToEnd ()
72 public override Stream BaseStream {
73 get { return Stream.Null; }
76 public override Encoding CurrentEncoding {
77 get { return Encoding.Unicode; }
81 const int DefaultBufferSize = 1024;
82 const int DefaultFileBufferSize = 4096;
83 const int MinimumBufferSize = 128;
90 // Input buffer ready for recycling
91 static byte [] input_buffer_recycle;
92 static object input_buffer_recycle_lock = new object ();
95 // The decoded buffer from the above input buffer
97 char [] decoded_buffer;
98 static char[] decoded_buffer_recycle;
102 StringBuilder line_builder;
106 // Decoded bytes in decoded_buffer.
111 // Current position in the decoded_buffer
116 // The buffer size that we are using
126 readonly bool leave_open;
129 public new static readonly StreamReader Null = new NullStreamReader ();
131 private StreamReader() {}
133 public StreamReader(Stream stream)
134 : this (stream, Encoding.UTF8Unmarked, true, DefaultBufferSize) { }
136 public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
137 : this (stream, Encoding.UTF8Unmarked, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }
139 public StreamReader(Stream stream, Encoding encoding)
140 : this (stream, encoding, true, DefaultBufferSize) { }
142 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
143 : this (stream, encoding, detectEncodingFromByteOrderMarks, DefaultBufferSize) { }
146 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
147 : this (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, false)
151 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
153 const bool leave_open = false;
155 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
159 leave_open = leaveOpen;
161 Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);
164 public StreamReader(string path)
165 : this (path, Encoding.UTF8Unmarked, true, DefaultFileBufferSize) { }
167 public StreamReader(string path, bool detectEncodingFromByteOrderMarks)
168 : this (path, Encoding.UTF8Unmarked, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }
170 public StreamReader(string path, Encoding encoding)
171 : this (path, encoding, true, DefaultFileBufferSize) { }
173 public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
174 : this (path, encoding, detectEncodingFromByteOrderMarks, DefaultFileBufferSize) { }
176 public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
179 throw new ArgumentNullException("path");
180 if (String.Empty == path)
181 throw new ArgumentException("Empty path not allowed");
182 if (path.IndexOfAny (Path.InvalidPathChars) != -1)
183 throw new ArgumentException("path contains invalid characters");
184 if (null == encoding)
185 throw new ArgumentNullException ("encoding");
187 throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
189 Stream stream = (Stream) File.OpenRead (path);
190 Initialize (stream, encoding, detectEncodingFromByteOrderMarks, bufferSize);
193 internal void Initialize (Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
196 throw new ArgumentNullException ("stream");
197 if (null == encoding)
198 throw new ArgumentNullException ("encoding");
200 throw new ArgumentException ("Cannot read stream");
202 throw new ArgumentOutOfRangeException ("bufferSize", "The minimum size of the buffer must be positive");
204 if (bufferSize < MinimumBufferSize)
205 bufferSize = MinimumBufferSize;
207 // since GetChars() might add flushed character, it
208 // should have additional char buffer for extra 1
209 // (probably 1 is ok, but might be insufficient. I'm not sure)
210 var decoded_buffer_size = encoding.GetMaxCharCount (bufferSize) + 1;
213 // Instead of allocating a new default buffer use the
214 // last one if there is any available
216 if (bufferSize <= DefaultBufferSize && input_buffer_recycle != null) {
217 lock (input_buffer_recycle_lock) {
218 if (input_buffer_recycle != null) {
219 input_buffer = input_buffer_recycle;
220 input_buffer_recycle = null;
223 if (decoded_buffer_recycle != null && decoded_buffer_size <= decoded_buffer_recycle.Length) {
224 decoded_buffer = decoded_buffer_recycle;
225 decoded_buffer_recycle = null;
230 if (input_buffer == null)
231 input_buffer = new byte [bufferSize];
233 Array.Clear (input_buffer, 0, bufferSize);
235 if (decoded_buffer == null)
236 decoded_buffer = new char [decoded_buffer_size];
238 Array.Clear (decoded_buffer, 0, decoded_buffer_size);
240 base_stream = stream;
241 this.buffer_size = bufferSize;
242 this.encoding = encoding;
243 decoder = encoding.GetDecoder ();
245 byte [] preamble = encoding.GetPreamble ();
246 do_checks = detectEncodingFromByteOrderMarks ? 1 : 0;
247 do_checks += (preamble.Length == 0) ? 0 : 2;
253 public virtual Stream BaseStream {
259 public virtual Encoding CurrentEncoding {
261 if (encoding == null)
262 throw new Exception ();
267 public bool EndOfStream {
268 get { return Peek () < 0; }
271 public override void Close ()
276 protected override void Dispose (bool disposing)
278 if (disposing && base_stream != null && !leave_open)
279 base_stream.Close ();
281 if (input_buffer != null && input_buffer.Length == DefaultBufferSize && input_buffer_recycle == null) {
282 lock (input_buffer_recycle_lock) {
283 if (input_buffer_recycle == null) {
284 input_buffer_recycle = input_buffer;
287 if (decoded_buffer_recycle == null) {
288 decoded_buffer_recycle = decoded_buffer;
294 decoded_buffer = null;
298 base.Dispose (disposing);
302 // Provides auto-detection of the encoding, as well as skipping over
303 // byte marks at the beginning of a stream.
305 int DoChecks (int count)
307 if ((do_checks & 2) == 2){
308 byte [] preamble = encoding.GetPreamble ();
309 int c = preamble.Length;
313 for (i = 0; i < c; i++)
314 if (input_buffer [i] != preamble [i])
322 if ((do_checks & 1) == 1){
326 if (input_buffer [0] == 0xfe && input_buffer [1] == 0xff){
327 this.encoding = Encoding.BigEndianUnicode;
330 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe && count < 4) {
331 // If we don't have enough bytes we can't check for UTF32, so use Unicode
332 this.encoding = Encoding.Unicode;
339 if (input_buffer [0] == 0xef && input_buffer [1] == 0xbb && input_buffer [2] == 0xbf){
340 this.encoding = Encoding.UTF8Unmarked;
345 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe && input_buffer [2] != 0) {
346 this.encoding = Encoding.Unicode;
352 if (input_buffer [0] == 0 && input_buffer [1] == 0
353 && input_buffer [2] == 0xfe && input_buffer [3] == 0xff)
355 this.encoding = Encoding.BigEndianUTF32;
359 if (input_buffer [0] == 0xff && input_buffer [1] == 0xfe) {
360 if (input_buffer [2] == 0 && input_buffer[3] == 0) {
361 this.encoding = Encoding.UTF32;
365 this.encoding = Encoding.Unicode;
373 public void DiscardBufferedData ()
377 pos = decoded_count = 0;
379 // Discard internal state of the decoder too.
380 decoder = encoding.GetDecoder ();
383 // the buffer is empty, fill it again
384 private int ReadBuffer ()
389 // keep looping until the decoder gives us some chars
394 cbEncoded = base_stream.Read (input_buffer, 0, buffer_size);
399 mayBlock = (cbEncoded < buffer_size);
401 Encoding old = encoding;
402 parse_start = DoChecks (cbEncoded);
403 if (old != encoding){
404 int old_decoded_size = old.GetMaxCharCount (buffer_size) + 1;
405 int new_decoded_size = encoding.GetMaxCharCount (buffer_size) + 1;
406 if (old_decoded_size != new_decoded_size)
407 decoded_buffer = new char [new_decoded_size];
408 decoder = encoding.GetDecoder ();
411 cbEncoded -= parse_start;
414 decoded_count += decoder.GetChars (input_buffer, parse_start, cbEncoded, decoded_buffer, 0);
416 } while (decoded_count == 0);
418 return decoded_count;
423 // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=96484
425 public override int Peek ()
429 if (pos >= decoded_count && ReadBuffer () == 0)
432 return decoded_buffer [pos];
436 // Used internally by our console, as it previously depended on Peek() being a
437 // routine that would not block.
439 internal bool DataAvailable ()
441 return pos < decoded_count;
444 public override int Read ()
448 if (pos >= decoded_count && ReadBuffer () == 0)
451 return decoded_buffer [pos++];
454 public override int Read ([In, Out] char[] buffer, int index, int count)
457 throw new ArgumentNullException ("buffer");
459 throw new ArgumentOutOfRangeException ("index", "< 0");
461 throw new ArgumentOutOfRangeException ("count", "< 0");
462 // re-ordered to avoid possible integer overflow
463 if (index > buffer.Length - count)
464 throw new ArgumentException ("index + count > buffer.Length");
471 if (pos >= decoded_count && ReadBuffer () == 0)
472 return chars_read > 0 ? chars_read : 0;
474 int cch = Math.Min (decoded_count - pos, count);
475 Array.Copy (decoded_buffer, pos, buffer, index, cch);
490 for (; pos < decoded_count; pos++) {
491 c = decoded_buffer [pos];
494 int res = (foundCR) ? (pos - 2) : (pos - 1);
496 res = 0; // if a new buffer starts with a \n and there was a \r at
497 // the end of the previous one, we get here.
500 } else if (foundCR) {
503 return -2; // Need to flush the current buffered line.
504 // This is a \r at the end of the previous decoded buffer that
505 // is not followed by a \n in the current decoded buffer.
509 foundCR = (c == '\r');
515 public override string ReadLine()
519 if (pos >= decoded_count && ReadBuffer () == 0)
523 int end = FindNextEOL ();
524 if (end < decoded_count && end >= begin)
525 return new string (decoded_buffer, begin, end - begin);
527 return line_builder.ToString (0, line_builder.Length);
529 if (line_builder == null)
530 line_builder = new StringBuilder ();
532 line_builder.Length = 0;
535 if (foundCR) // don't include the trailing CR if present
538 line_builder.Append (decoded_buffer, begin, decoded_count - begin);
539 if (ReadBuffer () == 0) {
540 if (line_builder.Capacity > 32768) {
541 StringBuilder sb = line_builder;
543 return sb.ToString (0, sb.Length);
545 return line_builder.ToString (0, line_builder.Length);
549 end = FindNextEOL ();
550 if (end < decoded_count && end >= begin) {
551 line_builder.Append (decoded_buffer, begin, end - begin);
552 if (line_builder.Capacity > 32768) {
553 StringBuilder sb = line_builder;
555 return sb.ToString (0, sb.Length);
557 return line_builder.ToString (0, line_builder.Length);
558 } else if (end == -2)
559 return line_builder.ToString (0, line_builder.Length);
563 public override string ReadToEnd()
567 StringBuilder text = new StringBuilder ();
569 int size = decoded_buffer.Length;
570 char [] buffer = new char [size];
573 while ((len = Read (buffer, 0, size)) > 0)
574 text.Append (buffer, 0, len);
576 return text.ToString ();
581 if (base_stream == null)
582 throw new ObjectDisposedException ("StreamReader", "Cannot read from a closed StreamReader");
585 if (async_task != null && async_task.IsCompleted)
586 throw new InvalidOperationException ();
591 public override int ReadBlock ([In, Out] char[] buffer, int index, int count)
594 throw new ArgumentNullException ("buffer");
596 throw new ArgumentOutOfRangeException ("index", "< 0");
598 throw new ArgumentOutOfRangeException ("count", "< 0");
599 // re-ordered to avoid possible integer overflow
600 if (index > buffer.Length - count)
601 throw new ArgumentException ("index + count > buffer.Length");
605 return base.ReadBlock (buffer, index, count);
608 public override Task<int> ReadAsync (char[] buffer, int index, int count)
613 async_task = res = base.ReadAsync (buffer, index, count);
617 public override Task<int> ReadBlockAsync (char[] buffer, int index, int count)
622 async_task = res = base.ReadBlockAsync (buffer, index, count);
626 public override Task<string> ReadLineAsync ()
631 async_task = res = base.ReadLineAsync ();
635 public override Task<string> ReadToEndAsync ()
640 async_task = res = base.ReadToEndAsync ();