mscorlib(4.0.0.0) API with additions
StringReader.cs
3 
4 namespace System.IO
5 {
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public class StringReader : TextReader
11  {
12  private string _s;
13 
14  private int _pos;
15 
16  private int _length;
17 
21  [__DynamicallyInvokable]
22  public StringReader(string s)
23  {
24  if (s == null)
25  {
26  throw new ArgumentNullException("s");
27  }
28  _s = s;
29  _length = (s?.Length ?? 0);
30  }
31 
33  public override void Close()
34  {
35  Dispose(disposing: true);
36  }
37 
41  [__DynamicallyInvokable]
42  protected override void Dispose(bool disposing)
43  {
44  _s = null;
45  _pos = 0;
46  _length = 0;
47  base.Dispose(disposing);
48  }
49 
53  [__DynamicallyInvokable]
54  public override int Peek()
55  {
56  if (_s == null)
57  {
58  __Error.ReaderClosed();
59  }
60  if (_pos == _length)
61  {
62  return -1;
63  }
64  return _s[_pos];
65  }
66 
70  [__DynamicallyInvokable]
71  public override int Read()
72  {
73  if (_s == null)
74  {
75  __Error.ReaderClosed();
76  }
77  if (_pos == _length)
78  {
79  return -1;
80  }
81  return _s[_pos++];
82  }
83 
95  [__DynamicallyInvokable]
96  public override int Read([In] [Out] char[] buffer, int index, int count)
97  {
98  if (buffer == null)
99  {
100  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
101  }
102  if (index < 0)
103  {
104  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
105  }
106  if (count < 0)
107  {
108  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
109  }
110  if (buffer.Length - index < count)
111  {
112  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
113  }
114  if (_s == null)
115  {
116  __Error.ReaderClosed();
117  }
118  int num = _length - _pos;
119  if (num > 0)
120  {
121  if (num > count)
122  {
123  num = count;
124  }
125  _s.CopyTo(_pos, buffer, index, num);
126  _pos += num;
127  }
128  return num;
129  }
130 
135  [__DynamicallyInvokable]
136  public override string ReadToEnd()
137  {
138  if (_s == null)
139  {
140  __Error.ReaderClosed();
141  }
142  string result = (_pos != 0) ? _s.Substring(_pos, _length - _pos) : _s;
143  _pos = _length;
144  return result;
145  }
146 
151  [__DynamicallyInvokable]
152  public override string ReadLine()
153  {
154  if (_s == null)
155  {
156  __Error.ReaderClosed();
157  }
158  int i;
159  for (i = _pos; i < _length; i++)
160  {
161  char c = _s[i];
162  if (c == '\r' || c == '\n')
163  {
164  string result = _s.Substring(_pos, i - _pos);
165  _pos = i + 1;
166  if (c == '\r' && _pos < _length && _s[_pos] == '\n')
167  {
168  _pos++;
169  }
170  return result;
171  }
172  }
173  if (i > _pos)
174  {
175  string result2 = _s.Substring(_pos, i - _pos);
176  _pos = i;
177  return result2;
178  }
179  return null;
180  }
181 
187  [ComVisible(false)]
188  [__DynamicallyInvokable]
189  public override Task<string> ReadLineAsync()
190  {
191  return Task.FromResult(ReadLine());
192  }
193 
199  [ComVisible(false)]
200  [__DynamicallyInvokable]
201  public override Task<string> ReadToEndAsync()
202  {
203  return Task.FromResult(ReadToEnd());
204  }
205 
218  [ComVisible(false)]
219  [__DynamicallyInvokable]
220  public override Task<int> ReadBlockAsync(char[] buffer, int index, int count)
221  {
222  if (buffer == null)
223  {
224  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
225  }
226  if (index < 0 || count < 0)
227  {
228  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
229  }
230  if (buffer.Length - index < count)
231  {
232  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
233  }
234  return Task.FromResult(ReadBlock(buffer, index, count));
235  }
236 
249  [ComVisible(false)]
250  [__DynamicallyInvokable]
251  public override Task<int> ReadAsync(char[] buffer, int index, int count)
252  {
253  if (buffer == null)
254  {
255  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
256  }
257  if (index < 0 || count < 0)
258  {
259  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
260  }
261  if (buffer.Length - index < count)
262  {
263  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
264  }
265  return Task.FromResult(Read(buffer, index, count));
266  }
267  }
268 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override int Read([In] [Out] char[] buffer, int index, int count)
Reads a block of characters from the input string and advances the character position by count .
Definition: StringReader.cs:96
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
override Task< int > ReadBlockAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current string asynchronously and writes the ...
StringReader(string s)
Initializes a new instance of the T:System.IO.StringReader class that reads from the specified string...
Definition: StringReader.cs:22
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
override Task< int > ReadAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current string asynchronously and writes the ...
virtual int ReadBlock([In] [Out] char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader and writes the data to a ...
Definition: TextReader.cs:284
override Task< string > ReadToEndAsync()
Reads all characters from the current position to the end of the string asynchronously and returns th...
void Dispose()
Releases all resources used by the T:System.IO.TextReader object.
Definition: TextReader.cs:173
The exception that is thrown when one of the arguments provided to a method is not valid.
override string ReadLine()
Reads a line of characters from the current string and returns the data as a string.
override int Peek()
Returns the next available character but does not consume it.
Definition: StringReader.cs:54
override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.StringReader and optionally releases the man...
Definition: StringReader.cs:42
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
override void Close()
Closes the T:System.IO.StringReader.
Definition: StringReader.cs:33
override int Read()
Reads the next character from the input string and advances the character position by one character.
Definition: StringReader.cs:71
Specifies that the class can be serialized.
override Task< string > ReadLineAsync()
Reads a line of characters asynchronously from the current string and returns the data as a string.
override string ReadToEnd()
Reads all characters from the current position to the end of the string and returns them as a single ...
Implements a T:System.IO.TextReader that reads from a string.
Definition: StringReader.cs:10
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18