TextReader.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // System.IO.TextReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Runtime.InteropServices;
  32. namespace System.IO {
  33. [Serializable]
  34. [ComVisible (true)]
  35. #if NET_2_1
  36. public abstract class TextReader : IDisposable {
  37. #else
  38. public abstract class TextReader : MarshalByRefObject, IDisposable {
  39. #endif
  40. static TextReader ()
  41. {
  42. Null = new NullTextReader ();
  43. }
  44. protected TextReader() { }
  45. public static readonly TextReader Null;
  46. public virtual void Close()
  47. {
  48. Dispose(true);
  49. }
  50. public void Dispose ()
  51. {
  52. Dispose(true);
  53. }
  54. protected virtual void Dispose (bool disposing)
  55. {
  56. if (disposing){
  57. // If we are explicitly disposed, we can avoid finalization.
  58. GC.SuppressFinalize (this);
  59. }
  60. return;
  61. }
  62. public virtual int Peek()
  63. {
  64. return -1;
  65. }
  66. public virtual int Read()
  67. {
  68. return -1;
  69. }
  70. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  71. {
  72. int c, i;
  73. for (i = 0; i < count; i++) {
  74. if ((c = Read ()) == -1)
  75. return i;
  76. buffer [index + i] = (char)c;
  77. }
  78. return i;
  79. }
  80. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  81. {
  82. int total_read_count = 0;
  83. int current_read_count = 0;
  84. do {
  85. current_read_count = Read (buffer, index, count);
  86. index += current_read_count;
  87. total_read_count += current_read_count;
  88. count -= current_read_count;
  89. } while (current_read_count != 0 && count > 0);
  90. return total_read_count;
  91. }
  92. public virtual string ReadLine()
  93. {
  94. return String.Empty;
  95. }
  96. public virtual string ReadToEnd()
  97. {
  98. return String.Empty;
  99. }
  100. public static TextReader Synchronized (TextReader reader)
  101. {
  102. if (reader == null)
  103. throw new ArgumentNullException ("reader is null");
  104. if (reader is SynchronizedReader)
  105. return reader;
  106. return new SynchronizedReader (reader);
  107. }
  108. private class NullTextReader : System.IO.TextReader {
  109. public override string ReadLine ()
  110. {
  111. return null;
  112. }
  113. }
  114. }
  115. //
  116. // Synchronized Reader implementation, used internally.
  117. //
  118. [Serializable]
  119. internal class SynchronizedReader : TextReader {
  120. TextReader reader;
  121. public SynchronizedReader (TextReader reader)
  122. {
  123. this.reader = reader;
  124. }
  125. public override void Close ()
  126. {
  127. lock (this){
  128. reader.Close ();
  129. }
  130. }
  131. public override int Peek ()
  132. {
  133. lock (this){
  134. return reader.Peek ();
  135. }
  136. }
  137. public override int ReadBlock (char [] buffer, int index, int count)
  138. {
  139. lock (this){
  140. return reader.ReadBlock (buffer, index, count);
  141. }
  142. }
  143. public override string ReadLine ()
  144. {
  145. lock (this){
  146. return reader.ReadLine ();
  147. }
  148. }
  149. public override string ReadToEnd ()
  150. {
  151. lock (this){
  152. return reader.ReadToEnd ();
  153. }
  154. }
  155. #region Read Methods
  156. public override int Read ()
  157. {
  158. lock (this){
  159. return reader.Read ();
  160. }
  161. }
  162. public override int Read (char [] buffer, int index, int count)
  163. {
  164. lock (this){
  165. return reader.Read (buffer, index, count);
  166. }
  167. }
  168. #endregion
  169. }
  170. }