Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StreamReader.cs
Go to the documentation of this file.
2using System.Text;
5
6namespace System.IO;
7
8public class StreamReader : TextReader
9{
10 private sealed class NullStreamReader : StreamReader
11 {
13
14 protected override void Dispose(bool disposing)
15 {
16 }
17
18 public override int Peek()
19 {
20 return -1;
21 }
22
23 public override int Read()
24 {
25 return -1;
26 }
27
28 public override int Read(char[] buffer, int index, int count)
29 {
30 return 0;
31 }
32
33 public override string ReadLine()
34 {
35 return null;
36 }
37
38 public override string ReadToEnd()
39 {
40 return string.Empty;
41 }
42
43 internal override int ReadBuffer()
44 {
45 return 0;
46 }
47 }
48
49 public new static readonly StreamReader Null = new NullStreamReader();
50
51 private readonly Stream _stream;
52
54
56
57 private readonly byte[] _byteBuffer;
58
59 private char[] _charBuffer;
60
61 private int _charPos;
62
63 private int _charLen;
64
65 private int _byteLen;
66
67 private int _bytePos;
68
69 private int _maxCharsPerBuffer;
70
71 private bool _disposed;
72
73 private bool _detectEncoding;
74
75 private bool _checkPreamble;
76
77 private bool _isBlocked;
78
79 private readonly bool _closable;
80
82
84
85 public virtual Stream BaseStream => _stream;
86
87 public bool EndOfStream
88 {
89 get
90 {
93 if (_charPos < _charLen)
94 {
95 return false;
96 }
97 int num = ReadBuffer();
98 return num == 0;
99 }
100 }
101
103 {
105 {
107 }
108 }
109
110 [DoesNotReturn]
115
116 private StreamReader()
117 {
119 _closable = true;
120 }
121
123 : this(stream, detectEncodingFromByteOrderMarks: true)
124 {
125 }
126
127 public StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
128 : this(stream, Encoding.UTF8, detectEncodingFromByteOrderMarks, 1024, leaveOpen: false)
129 {
130 }
131
133 : this(stream, encoding, detectEncodingFromByteOrderMarks: true, 1024, leaveOpen: false)
134 {
135 }
136
137 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
138 : this(stream, encoding, detectEncodingFromByteOrderMarks, 1024, leaveOpen: false)
139 {
140 }
141
142 public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
143 : this(stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen: false)
144 {
145 }
146
147 public StreamReader(Stream stream, Encoding? encoding = null, bool detectEncodingFromByteOrderMarks = true, int bufferSize = -1, bool leaveOpen = false)
148 {
149 if (stream == null)
150 {
152 }
153 if (encoding == null)
154 {
155 encoding = Encoding.UTF8;
156 }
157 if (!stream.CanRead)
158 {
160 }
161 if (bufferSize == -1)
162 {
163 bufferSize = 1024;
164 }
165 else if (bufferSize <= 0)
166 {
168 }
169 _stream = stream;
170 _encoding = encoding;
171 _decoder = encoding.GetDecoder();
172 if (bufferSize < 128)
173 {
174 bufferSize = 128;
175 }
176 _byteBuffer = new byte[bufferSize];
177 _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
179 _detectEncoding = detectEncodingFromByteOrderMarks;
180 _checkPreamble = encoding.Preamble.Length > 0;
181 _closable = !leaveOpen;
182 }
183
184 public StreamReader(string path)
185 : this(path, detectEncodingFromByteOrderMarks: true)
186 {
187 }
188
189 public StreamReader(string path, bool detectEncodingFromByteOrderMarks)
190 : this(path, Encoding.UTF8, detectEncodingFromByteOrderMarks, 1024)
191 {
192 }
193
194 public StreamReader(string path, Encoding encoding)
195 : this(path, encoding, detectEncodingFromByteOrderMarks: true, 1024)
196 {
197 }
198
199 public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
200 : this(path, encoding, detectEncodingFromByteOrderMarks, 1024)
201 {
202 }
203
204 public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
205 : this(ValidateArgsAndOpenPath(path, encoding, bufferSize), encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen: false)
206 {
207 }
208
210 : this(path, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, options)
211 {
212 }
213
214 public StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, FileStreamOptions options)
215 : this(ValidateArgsAndOpenPath(path, encoding, options), encoding, detectEncodingFromByteOrderMarks, 1024)
216 {
217 }
218
219 private static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options)
220 {
221 ValidateArgs(path, encoding);
222 if (options == null)
223 {
224 throw new ArgumentNullException("options");
225 }
226 if ((options.Access & FileAccess.Read) == 0)
227 {
228 throw new ArgumentException(SR.Argument_StreamNotReadable, "options");
229 }
230 return new FileStream(path, options);
231 }
232
233 private static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, int bufferSize)
234 {
235 ValidateArgs(path, encoding);
236 if (bufferSize <= 0)
237 {
239 }
240 return new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096);
241 }
242
243 private static void ValidateArgs(string path, Encoding encoding)
244 {
245 if (path == null)
246 {
247 throw new ArgumentNullException("path");
248 }
249 if (encoding == null)
250 {
251 throw new ArgumentNullException("encoding");
252 }
253 if (path.Length == 0)
254 {
256 }
257 }
258
259 public override void Close()
260 {
261 Dispose(disposing: true);
262 }
263
264 protected override void Dispose(bool disposing)
265 {
266 if (_disposed)
267 {
268 return;
269 }
270 _disposed = true;
271 if (!_closable)
272 {
273 return;
274 }
275 try
276 {
277 if (disposing)
278 {
279 _stream.Close();
280 }
281 }
282 finally
283 {
284 _charPos = 0;
285 _charLen = 0;
286 base.Dispose(disposing);
287 }
288 }
289
291 {
293 _byteLen = 0;
294 _charLen = 0;
295 _charPos = 0;
296 if (_encoding != null)
297 {
299 }
300 _isBlocked = false;
301 }
302
303 public override int Peek()
304 {
307 if (_charPos == _charLen && (_isBlocked || ReadBuffer() == 0))
308 {
309 return -1;
310 }
311 return _charBuffer[_charPos];
312 }
313
314 public override int Read()
315 {
318 if (_charPos == _charLen && ReadBuffer() == 0)
319 {
320 return -1;
321 }
322 int result = _charBuffer[_charPos];
323 _charPos++;
324 return result;
325 }
326
327 public override int Read(char[] buffer, int index, int count)
328 {
329 if (buffer == null)
330 {
331 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
332 }
333 if (index < 0 || count < 0)
334 {
335 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
336 }
337 if (buffer.Length - index < count)
338 {
340 }
341 return ReadSpan(new Span<char>(buffer, index, count));
342 }
343
344 public override int Read(Span<char> buffer)
345 {
346 if (!(GetType() == typeof(StreamReader)))
347 {
348 return base.Read(buffer);
349 }
350 return ReadSpan(buffer);
351 }
352
354 {
357 int num = 0;
358 bool readToUserBuffer = false;
359 int num2 = buffer.Length;
360 while (num2 > 0)
361 {
362 int num3 = _charLen - _charPos;
363 if (num3 == 0)
364 {
365 num3 = ReadBuffer(buffer.Slice(num), out readToUserBuffer);
366 }
367 if (num3 == 0)
368 {
369 break;
370 }
371 if (num3 > num2)
372 {
373 num3 = num2;
374 }
375 if (!readToUserBuffer)
376 {
377 new Span<char>(_charBuffer, _charPos, num3).CopyTo(buffer.Slice(num));
378 _charPos += num3;
379 }
380 num += num3;
381 num2 -= num3;
382 if (_isBlocked)
383 {
384 break;
385 }
386 }
387 return num;
388 }
389
390 public override string ReadToEnd()
391 {
394 StringBuilder stringBuilder = new StringBuilder(_charLen - _charPos);
395 do
396 {
397 stringBuilder.Append(_charBuffer, _charPos, _charLen - _charPos);
399 ReadBuffer();
400 }
401 while (_charLen > 0);
402 return stringBuilder.ToString();
403 }
404
405 public override int ReadBlock(char[] buffer, int index, int count)
406 {
407 if (buffer == null)
408 {
409 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
410 }
411 if (index < 0 || count < 0)
412 {
413 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
414 }
415 if (buffer.Length - index < count)
416 {
418 }
421 return base.ReadBlock(buffer, index, count);
422 }
423
424 public override int ReadBlock(Span<char> buffer)
425 {
426 if (GetType() != typeof(StreamReader))
427 {
428 return base.ReadBlock(buffer);
429 }
430 int num = 0;
431 int num2;
432 do
433 {
434 num2 = ReadSpan(buffer.Slice(num));
435 num += num2;
436 }
437 while (num2 > 0 && num < buffer.Length);
438 return num;
439 }
440
441 private void CompressBuffer(int n)
442 {
444 _byteLen -= n;
445 }
446
447 private void DetectEncoding()
448 {
449 if (_byteLen < 2)
450 {
451 return;
452 }
453 _detectEncoding = false;
454 bool flag = false;
455 if (_byteBuffer[0] == 254 && _byteBuffer[1] == byte.MaxValue)
456 {
459 flag = true;
460 }
461 else if (_byteBuffer[0] == byte.MaxValue && _byteBuffer[1] == 254)
462 {
463 if (_byteLen < 4 || _byteBuffer[2] != 0 || _byteBuffer[3] != 0)
464 {
467 flag = true;
468 }
469 else
470 {
473 flag = true;
474 }
475 }
476 else if (_byteLen >= 3 && _byteBuffer[0] == 239 && _byteBuffer[1] == 187 && _byteBuffer[2] == 191)
477 {
480 flag = true;
481 }
482 else if (_byteLen >= 4 && _byteBuffer[0] == 0 && _byteBuffer[1] == 0 && _byteBuffer[2] == 254 && _byteBuffer[3] == byte.MaxValue)
483 {
484 _encoding = new UTF32Encoding(bigEndian: true, byteOrderMark: true);
486 flag = true;
487 }
488 else if (_byteLen == 2)
489 {
490 _detectEncoding = true;
491 }
492 if (flag)
493 {
495 int maxCharCount = _encoding.GetMaxCharCount(_byteBuffer.Length);
496 if (maxCharCount > _maxCharsPerBuffer)
497 {
498 _charBuffer = new char[maxCharCount];
499 }
500 _maxCharsPerBuffer = maxCharCount;
501 }
502 }
503
504 private bool IsPreamble()
505 {
506 if (!_checkPreamble)
507 {
508 return _checkPreamble;
509 }
511 int num = ((_byteLen >= preamble.Length) ? (preamble.Length - _bytePos) : (_byteLen - _bytePos));
512 int num2 = 0;
513 while (num2 < num)
514 {
515 if (_byteBuffer[_bytePos] != preamble[_bytePos])
516 {
517 _bytePos = 0;
518 _checkPreamble = false;
519 break;
520 }
521 num2++;
522 _bytePos++;
523 }
524 if (_checkPreamble && _bytePos == preamble.Length)
525 {
526 CompressBuffer(preamble.Length);
527 _bytePos = 0;
528 _checkPreamble = false;
529 _detectEncoding = false;
530 }
531 return _checkPreamble;
532 }
533
534 internal virtual int ReadBuffer()
535 {
536 _charLen = 0;
537 _charPos = 0;
538 if (!_checkPreamble)
539 {
540 _byteLen = 0;
541 }
542 do
543 {
544 if (_checkPreamble)
545 {
546 int num = _stream.Read(_byteBuffer, _bytePos, _byteBuffer.Length - _bytePos);
547 if (num == 0)
548 {
549 if (_byteLen > 0)
550 {
552 _bytePos = (_byteLen = 0);
553 }
554 return _charLen;
555 }
556 _byteLen += num;
557 }
558 else
559 {
561 if (_byteLen == 0)
562 {
563 return _charLen;
564 }
565 }
567 if (!IsPreamble())
568 {
569 if (_detectEncoding && _byteLen >= 2)
570 {
572 }
574 }
575 }
576 while (_charLen == 0);
577 return _charLen;
578 }
579
580 private int ReadBuffer(Span<char> userBuffer, out bool readToUserBuffer)
581 {
582 _charLen = 0;
583 _charPos = 0;
584 if (!_checkPreamble)
585 {
586 _byteLen = 0;
587 }
588 int num = 0;
589 readToUserBuffer = userBuffer.Length >= _maxCharsPerBuffer;
590 do
591 {
592 if (_checkPreamble)
593 {
594 int num2 = _stream.Read(_byteBuffer, _bytePos, _byteBuffer.Length - _bytePos);
595 if (num2 == 0)
596 {
597 if (_byteLen > 0)
598 {
599 if (readToUserBuffer)
600 {
601 num = _decoder.GetChars(new ReadOnlySpan<byte>(_byteBuffer, 0, _byteLen), userBuffer.Slice(num), flush: false);
602 _charLen = 0;
603 }
604 else
605 {
607 _charLen += num;
608 }
609 }
610 return num;
611 }
612 _byteLen += num2;
613 }
614 else
615 {
617 if (_byteLen == 0)
618 {
619 break;
620 }
621 }
623 if (!IsPreamble())
624 {
625 if (_detectEncoding && _byteLen >= 2)
626 {
628 readToUserBuffer = userBuffer.Length >= _maxCharsPerBuffer;
629 }
630 _charPos = 0;
631 if (readToUserBuffer)
632 {
633 num += _decoder.GetChars(new ReadOnlySpan<byte>(_byteBuffer, 0, _byteLen), userBuffer.Slice(num), flush: false);
634 _charLen = 0;
635 }
636 else
637 {
639 _charLen += num;
640 }
641 }
642 }
643 while (num == 0);
644 _isBlocked &= num < userBuffer.Length;
645 return num;
646 }
647
648 public override string? ReadLine()
649 {
652 if (_charPos == _charLen && ReadBuffer() == 0)
653 {
654 return null;
655 }
656 StringBuilder stringBuilder = null;
657 do
658 {
659 int num = _charPos;
660 do
661 {
662 char c = _charBuffer[num];
663 if (c == '\r' || c == '\n')
664 {
665 string result;
666 if (stringBuilder != null)
667 {
668 stringBuilder.Append(_charBuffer, _charPos, num - _charPos);
669 result = stringBuilder.ToString();
670 }
671 else
672 {
673 result = new string(_charBuffer, _charPos, num - _charPos);
674 }
675 _charPos = num + 1;
676 if (c == '\r' && (_charPos < _charLen || ReadBuffer() > 0) && _charBuffer[_charPos] == '\n')
677 {
678 _charPos++;
679 }
680 return result;
681 }
682 num++;
683 }
684 while (num < _charLen);
685 num = _charLen - _charPos;
686 if (stringBuilder == null)
687 {
688 stringBuilder = new StringBuilder(num + 80);
689 }
690 stringBuilder.Append(_charBuffer, _charPos, num);
691 }
692 while (ReadBuffer() > 0);
693 return stringBuilder.ToString();
694 }
695
696 public override Task<string?> ReadLineAsync()
697 {
698 if (GetType() != typeof(StreamReader))
699 {
700 return base.ReadLineAsync();
701 }
705 }
706
708 {
709 bool flag = _charPos == _charLen;
710 bool flag2 = flag;
711 if (flag2)
712 {
713 flag2 = await ReadBufferAsync(CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false) == 0;
714 }
715 if (flag2)
716 {
717 return null;
718 }
719 StringBuilder sb = null;
720 do
721 {
722 char[] charBuffer = _charBuffer;
723 int charLen = _charLen;
724 int charPos = _charPos;
725 int num = charPos;
726 do
727 {
728 char c = charBuffer[num];
729 if (c == '\r' || c == '\n')
730 {
731 string s;
732 if (sb != null)
733 {
734 sb.Append(charBuffer, charPos, num - charPos);
735 s = sb.ToString();
736 }
737 else
738 {
739 s = new string(charBuffer, charPos, num - charPos);
740 }
741 charPos = (_charPos = num + 1);
742 bool flag3 = c == '\r';
743 bool flag4 = flag3;
744 if (flag4)
745 {
746 bool flag5 = charPos < charLen;
747 bool flag6 = flag5;
748 if (!flag6)
749 {
750 flag6 = await ReadBufferAsync(CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false) > 0;
751 }
752 flag4 = flag6;
753 }
754 if (flag4)
755 {
756 charPos = _charPos;
757 if (_charBuffer[charPos] == '\n')
758 {
759 _charPos = charPos + 1;
760 }
761 }
762 return s;
763 }
764 num++;
765 }
766 while (num < charLen);
767 num = charLen - charPos;
768 if (sb == null)
769 {
770 sb = new StringBuilder(num + 80);
771 }
772 sb.Append(charBuffer, charPos, num);
773 }
774 while (await ReadBufferAsync(CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false) > 0);
775 return sb.ToString();
776 }
777
778 public override Task<string> ReadToEndAsync()
779 {
780 if (GetType() != typeof(StreamReader))
781 {
782 return base.ReadToEndAsync();
783 }
787 }
788
790 {
792 do
793 {
794 int charPos = _charPos;
795 sb.Append(_charBuffer, charPos, _charLen - charPos);
797 await ReadBufferAsync(CancellationToken.None).ConfigureAwait(continueOnCapturedContext: false);
798 }
799 while (_charLen > 0);
800 return sb.ToString();
801 }
802
803 public override Task<int> ReadAsync(char[] buffer, int index, int count)
804 {
805 if (buffer == null)
806 {
807 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
808 }
809 if (index < 0 || count < 0)
810 {
811 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
812 }
813 if (buffer.Length - index < count)
814 {
816 }
817 if (GetType() != typeof(StreamReader))
818 {
819 return base.ReadAsync(buffer, index, count);
820 }
824 }
825
827 {
828 if (GetType() != typeof(StreamReader))
829 {
830 return base.ReadAsync(buffer, cancellationToken);
831 }
834 if (cancellationToken.IsCancellationRequested)
835 {
837 }
839 }
840
842 {
843 bool flag = _charPos == _charLen;
844 bool flag2 = flag;
845 if (flag2)
846 {
847 flag2 = await ReadBufferAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false) == 0;
848 }
849 if (flag2)
850 {
851 return 0;
852 }
853 int charsRead = 0;
854 bool readToUserBuffer = false;
855 byte[] tmpByteBuffer = _byteBuffer;
856 Stream tmpStream = _stream;
857 int count = buffer.Length;
858 while (count > 0)
859 {
860 int i = _charLen - _charPos;
861 if (i == 0)
862 {
863 _charLen = 0;
864 _charPos = 0;
865 if (!_checkPreamble)
866 {
867 _byteLen = 0;
868 }
869 readToUserBuffer = count >= _maxCharsPerBuffer;
870 do
871 {
872 if (_checkPreamble)
873 {
874 int bytePos = _bytePos;
875 int num = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer, bytePos, tmpByteBuffer.Length - bytePos), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
876 if (num == 0)
877 {
878 if (_byteLen > 0)
879 {
880 if (readToUserBuffer)
881 {
882 i = _decoder.GetChars(new ReadOnlySpan<byte>(tmpByteBuffer, 0, _byteLen), buffer.Span.Slice(charsRead), flush: false);
883 _charLen = 0;
884 }
885 else
886 {
887 i = _decoder.GetChars(tmpByteBuffer, 0, _byteLen, _charBuffer, 0);
888 _charLen += i;
889 }
890 }
891 _isBlocked = true;
892 break;
893 }
894 _byteLen += num;
895 }
896 else
897 {
898 _byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
899 if (_byteLen == 0)
900 {
901 _isBlocked = true;
902 break;
903 }
904 }
905 _isBlocked = _byteLen < tmpByteBuffer.Length;
906 if (!IsPreamble())
907 {
908 if (_detectEncoding && _byteLen >= 2)
909 {
911 readToUserBuffer = count >= _maxCharsPerBuffer;
912 }
913 _charPos = 0;
914 if (readToUserBuffer)
915 {
916 i += _decoder.GetChars(new ReadOnlySpan<byte>(tmpByteBuffer, 0, _byteLen), buffer.Span.Slice(charsRead), flush: false);
917 _charLen = 0;
918 }
919 else
920 {
921 i = _decoder.GetChars(tmpByteBuffer, 0, _byteLen, _charBuffer, 0);
922 _charLen += i;
923 }
924 }
925 }
926 while (i == 0);
927 if (i == 0)
928 {
929 break;
930 }
931 }
932 if (i > count)
933 {
934 i = count;
935 }
936 if (!readToUserBuffer)
937 {
938 new Span<char>(_charBuffer, _charPos, i).CopyTo(buffer.Span.Slice(charsRead));
939 _charPos += i;
940 }
941 charsRead += i;
942 count -= i;
943 if (_isBlocked)
944 {
945 break;
946 }
947 }
948 return charsRead;
949 }
950
951 public override Task<int> ReadBlockAsync(char[] buffer, int index, int count)
952 {
953 if (buffer == null)
954 {
955 throw new ArgumentNullException("buffer", SR.ArgumentNull_Buffer);
956 }
957 if (index < 0 || count < 0)
958 {
959 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
960 }
961 if (buffer.Length - index < count)
962 {
964 }
965 if (GetType() != typeof(StreamReader))
966 {
967 return base.ReadBlockAsync(buffer, index, count);
968 }
971 return (Task<int>)(_asyncReadTask = base.ReadBlockAsync(buffer, index, count));
972 }
973
975 {
976 if (GetType() != typeof(StreamReader))
977 {
978 return base.ReadBlockAsync(buffer, cancellationToken);
979 }
982 if (cancellationToken.IsCancellationRequested)
983 {
985 }
987 if (result.IsCompletedSuccessfully)
988 {
989 return result;
990 }
991 return new ValueTask<int>((Task<int>)(_asyncReadTask = result.AsTask()));
992 }
993
995 {
996 _charLen = 0;
997 _charPos = 0;
998 byte[] tmpByteBuffer = _byteBuffer;
999 Stream tmpStream = _stream;
1000 if (!_checkPreamble)
1001 {
1002 _byteLen = 0;
1003 }
1004 do
1005 {
1006 if (_checkPreamble)
1007 {
1008 int bytePos = _bytePos;
1009 int num = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer, bytePos, tmpByteBuffer.Length - bytePos), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
1010 if (num == 0)
1011 {
1012 if (_byteLen > 0)
1013 {
1014 _charLen += _decoder.GetChars(tmpByteBuffer, 0, _byteLen, _charBuffer, _charLen);
1015 _bytePos = 0;
1016 _byteLen = 0;
1017 }
1018 return _charLen;
1019 }
1020 _byteLen += num;
1021 }
1022 else
1023 {
1024 _byteLen = await tmpStream.ReadAsync(new Memory<byte>(tmpByteBuffer), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
1025 if (_byteLen == 0)
1026 {
1027 return _charLen;
1028 }
1029 }
1030 _isBlocked = _byteLen < tmpByteBuffer.Length;
1031 if (!IsPreamble())
1032 {
1033 if (_detectEncoding && _byteLen >= 2)
1034 {
1036 }
1037 _charLen += _decoder.GetChars(tmpByteBuffer, 0, _byteLen, _charBuffer, _charLen);
1038 }
1039 }
1040 while (_charLen == 0);
1041 return _charLen;
1042 }
1043
1044 private void ThrowIfDisposed()
1045 {
1046 if (_disposed)
1047 {
1048 ThrowObjectDisposedException();
1049 }
1050 void ThrowObjectDisposedException()
1051 {
1053 }
1054 }
1055}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
override int Read(char[] buffer, int index, int count)
override void Dispose(bool disposing)
readonly bool _closable
StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, FileStreamOptions options)
static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, FileStreamOptions options)
async ValueTask< int > ReadBufferAsync(CancellationToken cancellationToken)
override ValueTask< int > ReadBlockAsync(Memory< char > buffer, CancellationToken cancellationToken=default(CancellationToken))
StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
override int ReadBlock(char[] buffer, int index, int count)
static void ValidateArgs(string path, Encoding encoding)
override int ReadBlock(Span< char > buffer)
virtual int ReadBuffer()
override void Dispose(bool disposing)
virtual Encoding CurrentEncoding
virtual Stream BaseStream
override Task< int > ReadAsync(char[] buffer, int index, int count)
override string ReadToEnd()
StreamReader(string path)
override async ValueTask< int > ReadAsyncInternal(Memory< char > buffer, CancellationToken cancellationToken)
int ReadSpan(Span< char > buffer)
async Task< string > ReadToEndAsyncInternal()
async Task< string > ReadLineAsyncInternal()
StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
override int Read(char[] buffer, int index, int count)
override int Read(Span< char > buffer)
StreamReader(string path, FileStreamOptions options)
int ReadBuffer(Span< char > userBuffer, out bool readToUserBuffer)
StreamReader(Stream stream, Encoding encoding)
override Task< string?> ReadLineAsync()
static new readonly StreamReader Null
StreamReader(Stream stream)
static void ThrowAsyncIOInProgress()
StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
override void Close()
readonly Stream _stream
override? string ReadLine()
override Task< int > ReadBlockAsync(char[] buffer, int index, int count)
readonly byte[] _byteBuffer
override ValueTask< int > ReadAsync(Memory< char > buffer, CancellationToken cancellationToken=default(CancellationToken))
override Task< string > ReadToEndAsync()
static Stream ValidateArgsAndOpenPath(string path, Encoding encoding, int bufferSize)
StreamReader(string path, bool detectEncodingFromByteOrderMarks)
StreamReader(string path, Encoding encoding)
StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
StreamReader(Stream stream, Encoding? encoding=null, bool detectEncodingFromByteOrderMarks=true, int bufferSize=-1, bool leaveOpen=false)
virtual void Close()
Definition Stream.cs:644
int Read(byte[] buffer, int offset, int count)
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
static readonly Stream Null
Definition Stream.cs:488
async ValueTask< int > ReadBlockAsyncInternal(Memory< char > buffer, CancellationToken cancellationToken)
static string ArgumentOutOfRange_NeedPosNum
Definition SR.cs:20
static string ObjectDisposed_ReaderClosed
Definition SR.cs:1748
static string Argument_StreamNotReadable
Definition SR.cs:876
static string Argument_EmptyPath
Definition SR.cs:38
static string ArgumentNull_Buffer
Definition SR.cs:22
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string InvalidOperation_AsyncIOInProgress
Definition SR.cs:1398
Definition SR.cs:7
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
static Encoding BigEndianUnicode
Definition Encoding.cs:521
static Encoding Unicode
Definition Encoding.cs:519
virtual ReadOnlySpan< byte > Preamble
Definition Encoding.cs:347
static Encoding UTF8
Definition Encoding.cs:526
int GetMaxCharCount(int byteCount)
static Encoding UTF32
Definition Encoding.cs:528
virtual Decoder GetDecoder()
Definition Encoding.cs:1004
override string ToString()
StringBuilder Append(char value, int repeatCount)
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static Task CompletedTask
Definition Task.cs:1120
static void ThrowArgumentNullException(string name)
ReadOnlySpan< T > Slice(int start)
void CopyTo(Span< T > destination)
Definition Span.cs:224
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312