TextReader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // System.IO.TextReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Marek Safar ([email protected])
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. // Copyright 2011 Xamarin Inc.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Runtime.InteropServices;
  33. using System.Threading.Tasks;
  34. namespace System.IO {
  35. [Serializable]
  36. [ComVisible (true)]
  37. #if NET_2_1
  38. public abstract class TextReader : IDisposable {
  39. #else
  40. public abstract class TextReader : MarshalByRefObject, IDisposable {
  41. #endif
  42. sealed class NullTextReader : TextReader
  43. {
  44. public override string ReadLine ()
  45. {
  46. return null;
  47. }
  48. public override string ReadToEnd ()
  49. {
  50. return String.Empty;
  51. }
  52. }
  53. public static readonly TextReader Null = new NullTextReader ();
  54. protected TextReader()
  55. {
  56. }
  57. public virtual void Close()
  58. {
  59. Dispose(true);
  60. }
  61. public void Dispose ()
  62. {
  63. Dispose(true);
  64. }
  65. protected virtual void Dispose (bool disposing)
  66. {
  67. if (disposing){
  68. // If we are explicitly disposed, we can avoid finalization.
  69. GC.SuppressFinalize (this);
  70. }
  71. return;
  72. }
  73. public virtual int Peek()
  74. {
  75. return -1;
  76. }
  77. public virtual int Read()
  78. {
  79. return -1;
  80. }
  81. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  82. {
  83. int c, i;
  84. for (i = 0; i < count; i++) {
  85. if ((c = Read ()) == -1)
  86. return i;
  87. buffer [index + i] = (char)c;
  88. }
  89. return i;
  90. }
  91. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  92. {
  93. int total_read_count = 0;
  94. int current_read_count;
  95. do {
  96. current_read_count = Read (buffer, index, count);
  97. index += current_read_count;
  98. total_read_count += current_read_count;
  99. count -= current_read_count;
  100. } while (current_read_count != 0 && count > 0);
  101. return total_read_count;
  102. }
  103. public virtual string ReadLine ()
  104. {
  105. var result = new System.Text.StringBuilder ();
  106. int c;
  107. while ((c = Read ()) != -1){
  108. // check simple character line ending
  109. if (c == '\n')
  110. break;
  111. if (c == '\r') {
  112. if (Peek () == '\n')
  113. Read ();
  114. break;
  115. }
  116. result.Append ((char) c);
  117. }
  118. if (c == -1 && result.Length == 0)
  119. return null;
  120. return result.ToString ();
  121. }
  122. public virtual string ReadToEnd ()
  123. {
  124. var result = new System.Text.StringBuilder ();
  125. int c;
  126. while ((c = Read ()) != -1)
  127. result.Append ((char) c);
  128. return result.ToString ();
  129. }
  130. public static TextReader Synchronized (TextReader reader)
  131. {
  132. if (reader == null)
  133. throw new ArgumentNullException ("reader is null");
  134. if (reader is SynchronizedReader)
  135. return reader;
  136. return new SynchronizedReader (reader);
  137. }
  138. //
  139. // Use tuple to pack the arguments because it's faster than
  140. // setting up anonymous method container with an instance delegate
  141. //
  142. public virtual Task<int> ReadAsync (char[] buffer, int index, int count)
  143. {
  144. return Task.Factory.StartNew (l => {
  145. var t = (Tuple<TextReader, char[], int, int>) l;
  146. return t.Item1.Read (t.Item2, t.Item3, t.Item4);
  147. }, Tuple.Create (this, buffer, index, count));
  148. }
  149. public virtual Task<int> ReadBlockAsync (char[] buffer, int index, int count)
  150. {
  151. return Task.Factory.StartNew (l => {
  152. var t = (Tuple<TextReader, char[], int, int>) l;
  153. return t.Item1.ReadBlock (t.Item2, t.Item3, t.Item4);
  154. }, Tuple.Create (this, buffer, index, count));
  155. }
  156. public virtual Task<string> ReadLineAsync ()
  157. {
  158. return Task.Factory.StartNew (l => ((TextReader) l).ReadLine (), this);
  159. }
  160. public virtual Task<string> ReadToEndAsync ()
  161. {
  162. return Task.Factory.StartNew (l => ((TextReader) l).ReadToEnd (), this);
  163. }
  164. }
  165. //
  166. // Synchronized Reader implementation, used internally.
  167. //
  168. [Serializable]
  169. sealed class SynchronizedReader : TextReader
  170. {
  171. readonly TextReader reader;
  172. public SynchronizedReader (TextReader reader)
  173. {
  174. this.reader = reader;
  175. }
  176. public override void Close ()
  177. {
  178. lock (this){
  179. reader.Close ();
  180. }
  181. }
  182. public override int Peek ()
  183. {
  184. lock (this){
  185. return reader.Peek ();
  186. }
  187. }
  188. public override int ReadBlock (char [] buffer, int index, int count)
  189. {
  190. lock (this){
  191. return reader.ReadBlock (buffer, index, count);
  192. }
  193. }
  194. public override string ReadLine ()
  195. {
  196. lock (this){
  197. return reader.ReadLine ();
  198. }
  199. }
  200. public override string ReadToEnd ()
  201. {
  202. lock (this){
  203. return reader.ReadToEnd ();
  204. }
  205. }
  206. public override int Read ()
  207. {
  208. lock (this){
  209. return reader.Read ();
  210. }
  211. }
  212. public override int Read (char [] buffer, int index, int count)
  213. {
  214. lock (this){
  215. return reader.Read (buffer, index, count);
  216. }
  217. }
  218. public override Task<int> ReadAsync (char[] buffer, int index, int count)
  219. {
  220. lock (this) {
  221. return reader.ReadAsync (buffer, index, count);
  222. }
  223. }
  224. public override Task<int> ReadBlockAsync (char[] buffer, int index, int count)
  225. {
  226. lock (this) {
  227. return reader.ReadBlockAsync (buffer, index, count);
  228. }
  229. }
  230. public override Task<string> ReadLineAsync ()
  231. {
  232. lock (this) {
  233. return reader.ReadLineAsync ();
  234. }
  235. }
  236. public override Task<string> ReadToEndAsync ()
  237. {
  238. lock (this) {
  239. return reader.ReadToEndAsync ();
  240. }
  241. }
  242. }
  243. }