12 [__DynamicallyInvokable]
21 internal NullStreamReader()
26 protected override void Dispose(
bool disposing)
30 public override int Peek()
35 public override int Read()
40 public override int Read(
char[] buffer,
int index,
int count)
55 internal override int ReadBuffer()
62 [__DynamicallyInvokable]
65 private const int DefaultFileStreamBufferSize = 4096;
67 private const int MinBufferSize = 128;
75 private byte[] byteBuffer;
77 private char[] charBuffer;
79 private byte[] _preamble;
89 private int _maxCharsPerBuffer;
91 private bool _detectEncoding;
93 private bool _checkPreamble;
95 private bool _isBlocked;
97 private bool _closable;
100 private volatile Task _asyncReadTask;
102 internal static int DefaultBufferSize => 1024;
106 [__DynamicallyInvokable]
109 [__DynamicallyInvokable]
118 [__DynamicallyInvokable]
121 [__DynamicallyInvokable]
128 internal bool LeaveOpen => !_closable;
134 [__DynamicallyInvokable]
137 [__DynamicallyInvokable]
142 __Error.ReaderClosed();
144 CheckAsyncTaskInProgress();
145 if (charPos < charLen)
149 int num = ReadBuffer();
154 private int CharLen_Prop
166 private int CharPos_Prop
178 private int ByteLen_Prop
190 private int BytePos_Prop
202 private byte[] Preamble_Prop => _preamble;
204 private bool CheckPreamble_Prop => _checkPreamble;
206 private Decoder Decoder_Prop => decoder;
208 private bool DetectEncoding_Prop => _detectEncoding;
210 private char[] CharBuffer_Prop => charBuffer;
212 private byte[] ByteBuffer_Prop => byteBuffer;
214 private bool IsBlocked_Prop
226 private Stream Stream_Prop => stream;
228 private int MaxCharsPerBuffer_Prop => _maxCharsPerBuffer;
230 private void CheckAsyncTaskInProgress()
232 Task asyncReadTask = _asyncReadTask;
233 if (asyncReadTask !=
null && !asyncReadTask.
IsCompleted)
235 throw new InvalidOperationException(Environment.GetResourceString(
"InvalidOperation_AsyncIOInProgress"));
239 internal StreamReader()
249 [__DynamicallyInvokable]
251 : this(stream, detectEncodingFromByteOrderMarks: true)
262 [__DynamicallyInvokable]
264 : this(stream,
Encoding.UTF8, detectEncodingFromByteOrderMarks, DefaultBufferSize, leaveOpen: false)
275 [__DynamicallyInvokable]
277 : this(stream, encoding, detectEncodingFromByteOrderMarks: true, DefaultBufferSize, leaveOpen: false)
289 [__DynamicallyInvokable]
291 : this(stream, encoding, detectEncodingFromByteOrderMarks, DefaultBufferSize, leaveOpen: false)
305 [__DynamicallyInvokable]
307 : this(stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen: false)
319 [__DynamicallyInvokable]
322 if (stream ==
null || encoding ==
null)
334 Init(stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen);
348 : this(path, detectEncodingFromByteOrderMarks: true)
363 public StreamReader(
string path,
bool detectEncodingFromByteOrderMarks)
364 : this(path,
Encoding.UTF8, detectEncodingFromByteOrderMarks, DefaultBufferSize)
380 : this(path, encoding, detectEncodingFromByteOrderMarks: true, DefaultBufferSize)
397 : this(path, encoding, detectEncodingFromByteOrderMarks, DefaultBufferSize)
416 [SecuritySafeCritical]
418 : this(path, encoding, detectEncodingFromByteOrderMarks, bufferSize, checkHost: true)
423 internal StreamReader(
string path,
Encoding encoding,
bool detectEncodingFromByteOrderMarks,
int bufferSize,
bool checkHost)
425 if (path ==
null || encoding ==
null)
429 if (path.Length == 0)
435 throw new ArgumentOutOfRangeException(
"bufferSize", Environment.GetResourceString(
"ArgumentOutOfRange_NeedPosNum"));
438 Init(stream, encoding, detectEncodingFromByteOrderMarks, bufferSize, leaveOpen:
false);
441 private void Init(Stream stream,
Encoding encoding,
bool detectEncodingFromByteOrderMarks,
int bufferSize,
bool leaveOpen)
443 this.stream = stream;
444 this.encoding = encoding;
446 if (bufferSize < 128)
450 byteBuffer =
new byte[bufferSize];
452 charBuffer =
new char[_maxCharsPerBuffer];
455 _detectEncoding = detectEncodingFromByteOrderMarks;
457 _checkPreamble = (_preamble.Length != 0);
459 _closable = !leaveOpen;
462 internal void Init(Stream stream)
464 this.stream = stream;
477 [__DynamicallyInvokable]
478 protected override void Dispose(
bool disposing)
482 if (!LeaveOpen && disposing && stream !=
null)
489 if (!LeaveOpen && stream !=
null)
498 base.Dispose(disposing);
504 [__DynamicallyInvokable]
507 CheckAsyncTaskInProgress();
511 if (encoding !=
null)
521 [__DynamicallyInvokable]
526 __Error.ReaderClosed();
528 CheckAsyncTaskInProgress();
529 if (charPos == charLen && (_isBlocked || ReadBuffer() == 0))
533 return charBuffer[charPos];
539 [__DynamicallyInvokable]
544 __Error.ReaderClosed();
546 CheckAsyncTaskInProgress();
547 if (charPos == charLen && ReadBuffer() == 0)
551 int result = charBuffer[charPos];
567 [__DynamicallyInvokable]
568 public override int Read([In] [Out]
char[] buffer,
int index,
int count)
574 if (index < 0 || count < 0)
578 if (buffer.Length - index < count)
584 __Error.ReaderClosed();
586 CheckAsyncTaskInProgress();
588 bool readToUserBuffer =
false;
591 int num2 = charLen - charPos;
594 num2 = ReadBuffer(buffer, index + num, count, out readToUserBuffer);
604 if (!readToUserBuffer)
606 Buffer.InternalBlockCopy(charBuffer, charPos * 2, buffer, (index + num) * 2, num2 * 2);
623 [__DynamicallyInvokable]
628 __Error.ReaderClosed();
630 CheckAsyncTaskInProgress();
634 stringBuilder.
Append(charBuffer, charPos, charLen - charPos);
654 [__DynamicallyInvokable]
655 public override int ReadBlock([In] [Out]
char[] buffer,
int index,
int count)
661 if (index < 0 || count < 0)
665 if (buffer.Length - index < count)
671 __Error.ReaderClosed();
673 CheckAsyncTaskInProgress();
674 return base.ReadBlock(buffer, index, count);
677 private void CompressBuffer(
int n)
679 Buffer.InternalBlockCopy(byteBuffer, n, byteBuffer, 0, byteLen - n);
683 private void DetectEncoding()
689 _detectEncoding =
false;
691 if (byteBuffer[0] == 254 && byteBuffer[1] ==
byte.MaxValue)
697 else if (byteBuffer[0] ==
byte.MaxValue && byteBuffer[1] == 254)
699 if (byteLen < 4 || byteBuffer[2] != 0 || byteBuffer[3] != 0)
707 encoding =
new UTF32Encoding(bigEndian:
false, byteOrderMark:
true);
712 else if (byteLen >= 3 && byteBuffer[0] == 239 && byteBuffer[1] == 187 && byteBuffer[2] == 191)
718 else if (byteLen >= 4 && byteBuffer[0] == 0 && byteBuffer[1] == 0 && byteBuffer[2] == 254 && byteBuffer[3] ==
byte.MaxValue)
720 encoding =
new UTF32Encoding(bigEndian:
true, byteOrderMark:
true);
724 else if (byteLen == 2)
726 _detectEncoding =
true;
732 charBuffer =
new char[_maxCharsPerBuffer];
736 private bool IsPreamble()
740 return _checkPreamble;
742 int num = (byteLen >= _preamble.Length) ? (_preamble.Length - bytePos) : (byteLen - bytePos);
746 if (byteBuffer[bytePos] != _preamble[bytePos])
749 _checkPreamble =
false;
755 if (_checkPreamble && bytePos == _preamble.Length)
757 CompressBuffer(_preamble.Length);
759 _checkPreamble =
false;
760 _detectEncoding =
false;
762 return _checkPreamble;
765 internal virtual int ReadBuffer()
777 int num = stream.Read(byteBuffer, bytePos, byteBuffer.Length - bytePos);
782 charLen += decoder.
GetChars(byteBuffer, 0, byteLen, charBuffer, charLen);
783 bytePos = (byteLen = 0);
791 byteLen = stream.Read(byteBuffer, 0, byteBuffer.Length);
797 _isBlocked = (byteLen < byteBuffer.Length);
800 if (_detectEncoding && byteLen >= 2)
804 charLen += decoder.
GetChars(byteBuffer, 0, byteLen, charBuffer, charLen);
807 while (charLen == 0);
811 private int ReadBuffer(
char[] userBuffer,
int userOffset,
int desiredChars, out
bool readToUserBuffer)
820 readToUserBuffer = (desiredChars >= _maxCharsPerBuffer);
825 int num2 = stream.Read(byteBuffer, bytePos, byteBuffer.Length - bytePos);
830 if (readToUserBuffer)
832 num = decoder.
GetChars(byteBuffer, 0, byteLen, userBuffer, userOffset + num);
837 num = decoder.
GetChars(byteBuffer, 0, byteLen, charBuffer, num);
847 byteLen = stream.Read(byteBuffer, 0, byteBuffer.Length);
853 _isBlocked = (byteLen < byteBuffer.Length);
856 if (_detectEncoding && byteLen >= 2)
859 readToUserBuffer = (desiredChars >= _maxCharsPerBuffer);
862 if (readToUserBuffer)
864 num += decoder.
GetChars(byteBuffer, 0, byteLen, userBuffer, userOffset + num);
869 num = decoder.
GetChars(byteBuffer, 0, byteLen, charBuffer, num);
875 _isBlocked = (_isBlocked && num < desiredChars);
883 [__DynamicallyInvokable]
888 __Error.ReaderClosed();
890 CheckAsyncTaskInProgress();
891 if (charPos == charLen && ReadBuffer() == 0)
901 char c = charBuffer[num];
902 if (c ==
'\r' || c ==
'\n')
905 if (stringBuilder !=
null)
907 stringBuilder.
Append(charBuffer, charPos, num - charPos);
912 result =
new string(charBuffer, charPos, num - charPos);
915 if (c ==
'\r' && (charPos < charLen || ReadBuffer() > 0) && charBuffer[charPos] ==
'\n')
923 while (num < charLen);
924 num = charLen - charPos;
925 if (stringBuilder ==
null)
929 stringBuilder.
Append(charBuffer, charPos, num);
931 while (ReadBuffer() > 0);
941 [__DynamicallyInvokable]
942 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
947 return base.ReadLineAsync();
951 __Error.ReaderClosed();
953 CheckAsyncTaskInProgress();
954 return (
Task<string>)(_asyncReadTask = ReadLineAsyncInternal());
959 bool flag = CharPos_Prop == CharLen_Prop;
963 flag2 = (await ReadBufferAsync().
ConfigureAwait(continueOnCapturedContext:
false) == 0);
972 char[] tmpCharBuffer = CharBuffer_Prop;
973 int tmpCharLen = CharLen_Prop;
974 int tmpCharPos3 = CharPos_Prop;
978 char c = tmpCharBuffer[j];
979 if (c ==
'\r' || c ==
'\n')
984 sb.
Append(tmpCharBuffer, tmpCharPos3, j - tmpCharPos3);
989 s =
new string(tmpCharBuffer, tmpCharPos3, j - tmpCharPos3);
991 tmpCharPos3 = (CharPos_Prop = j + 1);
992 bool flag3 = c ==
'\r';
995 bool flag4 = tmpCharPos3 < tmpCharLen;
998 flag4 = (await ReadBufferAsync().
ConfigureAwait(continueOnCapturedContext:
false) > 0);
1004 tmpCharPos3 = CharPos_Prop;
1005 if (CharBuffer_Prop[tmpCharPos3] ==
'\n')
1007 CharPos_Prop = tmpCharPos3 + 1;
1014 while (j < tmpCharLen);
1015 j = tmpCharLen - tmpCharPos3;
1020 sb.
Append(tmpCharBuffer, tmpCharPos3, j);
1022 while (await ReadBufferAsync().ConfigureAwait(continueOnCapturedContext:
false) > 0);
1032 [__DynamicallyInvokable]
1033 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
1038 return base.ReadToEndAsync();
1042 __Error.ReaderClosed();
1044 CheckAsyncTaskInProgress();
1045 return (
Task<string>)(_asyncReadTask = ReadToEndAsyncInternal());
1053 int charPos_Prop = CharPos_Prop;
1054 sb.
Append(CharBuffer_Prop, charPos_Prop, CharLen_Prop - charPos_Prop);
1055 CharPos_Prop = CharLen_Prop;
1056 await ReadBufferAsync().
ConfigureAwait(continueOnCapturedContext:
false);
1058 while (CharLen_Prop > 0);
1075 [__DynamicallyInvokable]
1076 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
1083 if (index < 0 || count < 0)
1087 if (buffer.Length - index < count)
1093 return base.ReadAsync(buffer, index, count);
1097 __Error.ReaderClosed();
1099 CheckAsyncTaskInProgress();
1100 return (
Task<int>)(_asyncReadTask = ReadAsyncInternal(buffer, index, count));
1103 internal override async
Task<int> ReadAsyncInternal(
char[] buffer,
int index,
int count)
1105 bool flag = CharPos_Prop == CharLen_Prop;
1109 flag2 = (await ReadBufferAsync().
ConfigureAwait(continueOnCapturedContext:
false) == 0);
1116 bool readToUserBuffer =
false;
1117 byte[] tmpByteBuffer = ByteBuffer_Prop;
1118 Stream tmpStream = Stream_Prop;
1121 int i = CharLen_Prop - CharPos_Prop;
1126 if (!CheckPreamble_Prop)
1130 readToUserBuffer = (count >= MaxCharsPerBuffer_Prop);
1133 if (CheckPreamble_Prop)
1135 int bytePos_Prop = BytePos_Prop;
1136 int num = await tmpStream.ReadAsync(tmpByteBuffer, bytePos_Prop, tmpByteBuffer.Length - bytePos_Prop).ConfigureAwait(continueOnCapturedContext:
false);
1139 if (ByteLen_Prop > 0)
1141 if (readToUserBuffer)
1143 i = Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, buffer, index + charsRead);
1148 i = Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, 0);
1152 IsBlocked_Prop =
true;
1155 ByteLen_Prop += num;
1159 ByteLen_Prop = await tmpStream.ReadAsync(tmpByteBuffer, 0, tmpByteBuffer.Length).ConfigureAwait(continueOnCapturedContext:
false);
1160 if (ByteLen_Prop == 0)
1162 IsBlocked_Prop =
true;
1166 IsBlocked_Prop = (ByteLen_Prop < tmpByteBuffer.Length);
1169 if (DetectEncoding_Prop && ByteLen_Prop >= 2)
1172 readToUserBuffer = (count >= MaxCharsPerBuffer_Prop);
1175 if (readToUserBuffer)
1177 i += Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, buffer, index + charsRead);
1182 i = Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, 0);
1197 if (!readToUserBuffer)
1199 Buffer.InternalBlockCopy(CharBuffer_Prop, CharPos_Prop * 2, buffer, (index + charsRead) * 2, i * 2);
1225 [__DynamicallyInvokable]
1226 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
1233 if (index < 0 || count < 0)
1237 if (buffer.Length - index < count)
1243 return base.ReadBlockAsync(buffer, index, count);
1247 __Error.ReaderClosed();
1249 CheckAsyncTaskInProgress();
1250 return (
Task<int>)(_asyncReadTask = base.ReadBlockAsync(buffer, index, count));
1253 private async
Task<int> ReadBufferAsync()
1257 byte[] tmpByteBuffer = ByteBuffer_Prop;
1258 Stream tmpStream = Stream_Prop;
1259 if (!CheckPreamble_Prop)
1265 if (CheckPreamble_Prop)
1267 int bytePos_Prop = BytePos_Prop;
1268 int num = await tmpStream.
ReadAsync(tmpByteBuffer, bytePos_Prop, tmpByteBuffer.Length - bytePos_Prop).
ConfigureAwait(continueOnCapturedContext:
false);
1271 if (ByteLen_Prop > 0)
1273 CharLen_Prop += Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, CharLen_Prop);
1277 return CharLen_Prop;
1279 ByteLen_Prop += num;
1283 ByteLen_Prop = await tmpStream.
ReadAsync(tmpByteBuffer, 0, tmpByteBuffer.Length).
ConfigureAwait(continueOnCapturedContext:
false);
1284 if (ByteLen_Prop == 0)
1286 return CharLen_Prop;
1289 IsBlocked_Prop = (ByteLen_Prop < tmpByteBuffer.Length);
1292 if (DetectEncoding_Prop && ByteLen_Prop >= 2)
1296 CharLen_Prop += Decoder_Prop.
GetChars(tmpByteBuffer, 0, ByteLen_Prop, CharBuffer_Prop, CharLen_Prop);
1299 while (CharLen_Prop == 0);
1300 return CharLen_Prop;
Represents a character encoding.To browse the .NET Framework source code for this type,...
override Task< int > ReadAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current stream asynchronously and writes the ...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream,...
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Configures an awaiter used to await this T:System.Threading.Tasks.Task`1.
StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream based on th...
FileOptions
Represents advanced options for creating a T:System.IO.FileStream object.
bool EndOfStream
Gets a value that indicates whether the current stream position is at the end of the stream.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
StreamReader(string path)
Initializes a new instance of the T:System.IO.StreamReader class for the specified file name.
FileMode
Specifies how the operating system should open a file.
static Encoding Unicode
Gets an encoding for the UTF-16 format using the little endian byte order.
abstract int GetMaxCharCount(int byteCount)
When overridden in a derived class, calculates the maximum number of characters produced by decoding ...
static new readonly StreamReader Null
A T:System.IO.StreamReader object around an empty stream.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override string ReadToEnd()
Reads all characters from the current position to the end of the stream.
Implements a T:System.IO.TextReader that reads characters from a byte stream in a particular encoding...
StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize)
Initializes a new instance of the T:System.IO.StreamReader class for the specified file name,...
override string ReadLine()
Reads a line of characters from the current stream and returns the data as a string.
virtual Decoder GetDecoder()
When overridden in a derived class, obtains a decoder that converts an encoded sequence of bytes into...
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Asynchronously reads a sequence of bytes from the current stream and advances the position within the...
StreamReader(Stream stream, Encoding encoding)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream,...
abstract bool CanRead
When overridden in a derived class, gets a value indicating whether the current stream supports readi...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
override Task< int > ReadBlockAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current stream asynchronously and writes the ...
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
static readonly Stream Null
A Stream with no backing store.
Represents a UTF-16 encoding of Unicode characters.
override Task< string > ReadLineAsync()
Reads a line of characters asynchronously from the current stream and returns the data as a string.
override Task< string > ReadToEndAsync()
Reads all characters from the current position to the end of the stream asynchronously and returns th...
bool IsCompleted
Gets whether this T:System.Threading.Tasks.Task has completed.
virtual Stream BaseStream
Returns the underlying stream.
Represents a UTF-32 encoding of Unicode characters.
Converts a sequence of encoded bytes into a set of characters.
void Dispose()
Releases all resources used by the T:System.IO.TextReader object.
StreamReader(string path, Encoding encoding)
Initializes a new instance of the T:System.IO.StreamReader class for the specified file name,...
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
override int ReadBlock([In] [Out] char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current stream and writes the data to a buffe...
StreamReader(Stream stream)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream.
The exception that is thrown when one of the arguments provided to a method is not valid.
virtual byte [] GetPreamble()
When overridden in a derived class, returns a sequence of bytes that specifies the encoding used.
FileAccess
Defines constants for read, write, or read/write access to a file.
override int Peek()
Returns the next available character but does not consume it.
StreamReader(Stream stream, bool detectEncodingFromByteOrderMarks)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream,...
Represents a reader that can read a sequential series of characters.
override int Read([In] [Out] char[] buffer, int index, int count)
Reads a specified maximum of characters from the current stream into a buffer, beginning at the speci...
void DiscardBufferedData()
Clears the internal buffer.
virtual Encoding CurrentEncoding
Gets the current character encoding that the current T:System.IO.StreamReader object is using.
abstract int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any...
Specifies that the class can be serialized.
static Encoding UTF8
Gets an encoding for the UTF-8 format.
Manipulates arrays of primitive types.
override int Read()
Reads the next character from the input stream and advances the character position by one character.
StreamReader(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks)
Initializes a new instance of the T:System.IO.StreamReader class for the specified file name,...
The P:System.Uri.LocalPath data.
override void Close()
Closes the T:System.IO.StreamReader object and the underlying stream, and releases any system resourc...
override void Dispose(bool disposing)
Closes the underlying stream, releases the unmanaged resources used by the T:System....
StreamReader(string path, bool detectEncodingFromByteOrderMarks)
Initializes a new instance of the T:System.IO.StreamReader class for the specified file name,...
StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks)
Initializes a new instance of the T:System.IO.StreamReader class for the specified stream,...
FileShare
Contains constants for controlling the kind of access other T:System.IO.FileStream objects can have t...
Represents an asynchronous operation that can return a value.
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....