mscorlib(4.0.0.0) API with additions
TextReader.cs
4 using System.Text;
5 using System.Threading;
7 
8 namespace System.IO
9 {
11  [Serializable]
12  [ComVisible(true)]
13  [__DynamicallyInvokable]
14  public abstract class TextReader : MarshalByRefObject, IDisposable
15  {
16  [Serializable]
17  private sealed class NullTextReader : TextReader
18  {
19  public override int Read(char[] buffer, int index, int count)
20  {
21  return 0;
22  }
23 
24  public override string ReadLine()
25  {
26  return null;
27  }
28  }
29 
30  [Serializable]
31  internal sealed class SyncTextReader : TextReader
32  {
33  internal TextReader _in;
34 
35  internal SyncTextReader(TextReader t)
36  {
37  _in = t;
38  }
39 
40  [MethodImpl(MethodImplOptions.Synchronized)]
41  public override void Close()
42  {
43  _in.Close();
44  }
45 
46  [MethodImpl(MethodImplOptions.Synchronized)]
47  protected override void Dispose(bool disposing)
48  {
49  if (disposing)
50  {
51  ((IDisposable)_in).Dispose();
52  }
53  }
54 
55  [MethodImpl(MethodImplOptions.Synchronized)]
56  public override int Peek()
57  {
58  return _in.Peek();
59  }
60 
61  [MethodImpl(MethodImplOptions.Synchronized)]
62  public override int Read()
63  {
64  return _in.Read();
65  }
66 
67  [MethodImpl(MethodImplOptions.Synchronized)]
68  public override int Read([In] [Out] char[] buffer, int index, int count)
69  {
70  return _in.Read(buffer, index, count);
71  }
72 
73  [MethodImpl(MethodImplOptions.Synchronized)]
74  public override int ReadBlock([In] [Out] char[] buffer, int index, int count)
75  {
76  return _in.ReadBlock(buffer, index, count);
77  }
78 
79  [MethodImpl(MethodImplOptions.Synchronized)]
80  public override string ReadLine()
81  {
82  return _in.ReadLine();
83  }
84 
85  [MethodImpl(MethodImplOptions.Synchronized)]
86  public override string ReadToEnd()
87  {
88  return _in.ReadToEnd();
89  }
90 
91  [MethodImpl(MethodImplOptions.Synchronized)]
92  [ComVisible(false)]
93  public override Task<string> ReadLineAsync()
94  {
95  return Task.FromResult(ReadLine());
96  }
97 
98  [MethodImpl(MethodImplOptions.Synchronized)]
99  [ComVisible(false)]
100  public override Task<string> ReadToEndAsync()
101  {
102  return Task.FromResult(ReadToEnd());
103  }
104 
105  [MethodImpl(MethodImplOptions.Synchronized)]
106  [ComVisible(false)]
107  public override Task<int> ReadBlockAsync(char[] buffer, int index, int count)
108  {
109  if (buffer == null)
110  {
111  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
112  }
113  if (index < 0 || count < 0)
114  {
115  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
116  }
117  if (buffer.Length - index < count)
118  {
119  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
120  }
121  return Task.FromResult(ReadBlock(buffer, index, count));
122  }
123 
124  [MethodImpl(MethodImplOptions.Synchronized)]
125  [ComVisible(false)]
126  public override Task<int> ReadAsync(char[] buffer, int index, int count)
127  {
128  if (buffer == null)
129  {
130  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
131  }
132  if (index < 0 || count < 0)
133  {
134  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
135  }
136  if (buffer.Length - index < count)
137  {
138  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
139  }
140  return Task.FromResult(Read(buffer, index, count));
141  }
142  }
143 
144  [NonSerialized]
145  private static Func<object, string> _ReadLineDelegate = (object state) => ((TextReader)state).ReadLine();
146 
147  [NonSerialized]
148  private static Func<object, int> _ReadDelegate = delegate(object state)
149  {
151  return tuple.Item1.Read(tuple.Item2, tuple.Item3, tuple.Item4);
152  };
153 
155  [__DynamicallyInvokable]
156  public static readonly TextReader Null = new NullTextReader();
157 
159  [__DynamicallyInvokable]
160  protected TextReader()
161  {
162  }
163 
165  public virtual void Close()
166  {
167  Dispose(disposing: true);
168  GC.SuppressFinalize(this);
169  }
170 
172  [__DynamicallyInvokable]
173  public void Dispose()
174  {
175  Dispose(disposing: true);
176  GC.SuppressFinalize(this);
177  }
178 
182  [__DynamicallyInvokable]
183  protected virtual void Dispose(bool disposing)
184  {
185  }
186 
191  [__DynamicallyInvokable]
192  public virtual int Peek()
193  {
194  return -1;
195  }
196 
201  [__DynamicallyInvokable]
202  public virtual int Read()
203  {
204  return -1;
205  }
206 
219  [__DynamicallyInvokable]
220  public virtual int Read([In] [Out] 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)
227  {
228  throw new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
229  }
230  if (count < 0)
231  {
232  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
233  }
234  if (buffer.Length - index < count)
235  {
236  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
237  }
238  int num = 0;
239  do
240  {
241  int num2 = Read();
242  if (num2 == -1)
243  {
244  break;
245  }
246  buffer[index + num++] = (char)num2;
247  }
248  while (num < count);
249  return num;
250  }
251 
258  [__DynamicallyInvokable]
259  public virtual string ReadToEnd()
260  {
261  char[] array = new char[4096];
262  StringBuilder stringBuilder = new StringBuilder(4096);
263  int charCount;
264  while ((charCount = Read(array, 0, array.Length)) != 0)
265  {
266  stringBuilder.Append(array, 0, charCount);
267  }
268  return stringBuilder.ToString();
269  }
270 
283  [__DynamicallyInvokable]
284  public virtual int ReadBlock([In] [Out] char[] buffer, int index, int count)
285  {
286  int num = 0;
287  int num2;
288  do
289  {
290  num += (num2 = Read(buffer, index + num, count - num));
291  }
292  while (num2 > 0 && num < count);
293  return num;
294  }
295 
302  [__DynamicallyInvokable]
303  public virtual string ReadLine()
304  {
305  StringBuilder stringBuilder = new StringBuilder();
306  while (true)
307  {
308  int num = Read();
309  switch (num)
310  {
311  case 10:
312  case 13:
313  if (num == 13 && Peek() == 10)
314  {
315  Read();
316  }
317  return stringBuilder.ToString();
318  case -1:
319  if (stringBuilder.Length > 0)
320  {
321  return stringBuilder.ToString();
322  }
323  return null;
324  }
325  stringBuilder.Append((char)num);
326  }
327  }
328 
334  [ComVisible(false)]
335  [__DynamicallyInvokable]
336  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
337  public virtual Task<string> ReadLineAsync()
338  {
339  return Task<string>.Factory.StartNew(_ReadLineDelegate, this, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
340  }
341 
347  [ComVisible(false)]
348  [__DynamicallyInvokable]
349  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
350  public virtual async Task<string> ReadToEndAsync()
351  {
352  char[] chars = new char[4096];
353  StringBuilder sb = new StringBuilder(4096);
354  int len;
355  while ((len = await ReadAsyncInternal(chars, 0, chars.Length).ConfigureAwait(continueOnCapturedContext: false)) != 0)
356  {
357  sb.Append(chars, 0, len);
358  }
359  return sb.ToString();
360  }
361 
374  [ComVisible(false)]
375  [__DynamicallyInvokable]
376  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
377  public virtual Task<int> ReadAsync(char[] buffer, int index, int count)
378  {
379  if (buffer == null)
380  {
381  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
382  }
383  if (index < 0 || count < 0)
384  {
385  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
386  }
387  if (buffer.Length - index < count)
388  {
389  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
390  }
391  return ReadAsyncInternal(buffer, index, count);
392  }
393 
394  internal virtual Task<int> ReadAsyncInternal(char[] buffer, int index, int count)
395  {
396  Tuple<TextReader, char[], int, int> state = new Tuple<TextReader, char[], int, int>(this, buffer, index, count);
397  return Task<int>.Factory.StartNew(_ReadDelegate, state, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
398  }
399 
412  [ComVisible(false)]
413  [__DynamicallyInvokable]
414  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
415  public virtual Task<int> ReadBlockAsync(char[] buffer, int index, int count)
416  {
417  if (buffer == null)
418  {
419  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
420  }
421  if (index < 0 || count < 0)
422  {
423  throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
424  }
425  if (buffer.Length - index < count)
426  {
427  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
428  }
429  return ReadBlockAsyncInternal(buffer, index, count);
430  }
431 
432  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
433  private async Task<int> ReadBlockAsyncInternal(char[] buffer, int index, int count)
434  {
435  int i = 0;
436  int num;
437  do
438  {
439  num = await ReadAsyncInternal(buffer, index + i, count - i).ConfigureAwait(continueOnCapturedContext: false);
440  i += num;
441  }
442  while (num > 0 && i < count);
443  return i;
444  }
445 
451  [HostProtection(SecurityAction.LinkDemand, Synchronization = true)]
452  public static TextReader Synchronized(TextReader reader)
453  {
454  if (reader == null)
455  {
456  throw new ArgumentNullException("reader");
457  }
458  if (reader is SyncTextReader)
459  {
460  return reader;
461  }
462  return new SyncTextReader(reader);
463  }
464  }
465 }
static new TaskFactory< TResult > Factory
Provides access to factory methods for creating and configuring T:System.Threading....
Definition: Task.cs:75
virtual void Close()
Closes the T:System.IO.TextReader and releases any system resources associated with the TextReader.
Definition: TextReader.cs:165
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Propagates notification that operations should be canceled.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.TextReader and optionally releases the manag...
Definition: TextReader.cs:183
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Configures an awaiter used to await this T:System.Threading.Tasks.Task`1.
Definition: Task.cs:393
virtual Task< string > ReadLineAsync()
Reads a line of characters asynchronously and returns the data as a string.
Definition: TextReader.cs:337
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Provides static methods for creating tuple objects. To browse the .NET Framework source code for this...
Definition: Tuple.cs:10
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
virtual string ReadToEnd()
Reads all characters from the current position to the end of the text reader and returns them as one ...
Definition: TextReader.cs:259
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
virtual async Task< string > ReadToEndAsync()
Reads all characters from the current position to the end of the text reader asynchronously and retur...
Definition: TextReader.cs:350
Represents an object that handles the low-level work of queuing tasks onto threads.
virtual Task< int > ReadBlockAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader asynchronously and writes...
Definition: TextReader.cs:415
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual int Read([In] [Out] char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current reader and writes the data to a buffe...
Definition: TextReader.cs:220
TaskCreationOptions
Specifies flags that control optional behavior for the creation and execution of tasks.
static TaskScheduler Default
Gets the default T:System.Threading.Tasks.TaskScheduler instance that is provided by the ....
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static readonly TextReader Null
Provides a TextReader with no data to read from.
Definition: TextReader.cs:156
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
virtual int Read()
Reads the next character from the text reader and advances the character position by one character.
Definition: TextReader.cs:202
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
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
void Dispose()
Releases all resources used by the T:System.IO.TextReader object.
Definition: TextReader.cs:173
MethodImplOptions
Defines the details of how a method is implemented.
virtual Task< int > ReadAsync(char[] buffer, int index, int count)
Reads a specified maximum number of characters from the current text reader asynchronously and writes...
Definition: TextReader.cs:377
static CancellationToken None
Returns an empty T:System.Threading.CancellationToken value.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
The exception that is thrown when one of the arguments provided to a method is not valid.
TextReader()
Initializes a new instance of the T:System.IO.TextReader class.
Definition: TextReader.cs:160
Represents a reader that can read a sequential series of characters.
Definition: TextReader.cs:14
static TextReader Synchronized(TextReader reader)
Creates a thread-safe wrapper around the specified TextReader.
Definition: TextReader.cs:452
virtual int Peek()
Reads the next character without changing the state of the reader or the character source....
Definition: TextReader.cs:192
Specifies that the class can be serialized.
virtual string ReadLine()
Reads a line of characters from the text reader and returns the data as a string.
Definition: TextReader.cs:303
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18
Enables access to objects across application domain boundaries in applications that support remoting.