TextReader.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // System.IO.TextReader
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. using System;
  9. using System.Runtime.InteropServices;
  10. namespace System.IO {
  11. [Serializable]
  12. public abstract class TextReader : MarshalByRefObject, IDisposable {
  13. protected TextReader() { }
  14. public static readonly TextReader Null;
  15. public virtual void Close()
  16. {
  17. Dispose(true);
  18. }
  19. void System.IDisposable.Dispose()
  20. {
  21. Dispose(true);
  22. }
  23. protected virtual void Dispose( bool disposing )
  24. {
  25. return;
  26. }
  27. public virtual int Peek()
  28. {
  29. return -1;
  30. }
  31. public virtual int Read()
  32. {
  33. return -1;
  34. }
  35. public virtual int Read ([In, Out] char[] buffer, int index, int count)
  36. {
  37. int c, i;
  38. for (i = 0; i < count; i++) {
  39. if ((c = Read ()) == -1)
  40. return i;
  41. buffer [index + i] = (char)c;
  42. }
  43. return i;
  44. }
  45. public virtual int ReadBlock ([In, Out] char [] buffer, int index, int count)
  46. {
  47. int total_read_count = 0;
  48. int current_read_count = 0;
  49. do {
  50. current_read_count = Read (buffer, index, count);
  51. index += current_read_count;
  52. total_read_count += current_read_count;
  53. count -= current_read_count;
  54. } while (current_read_count != 0 && count > 0);
  55. return total_read_count;
  56. }
  57. public virtual string ReadLine()
  58. {
  59. return String.Empty;
  60. }
  61. public virtual string ReadToEnd()
  62. {
  63. return String.Empty;
  64. }
  65. public static TextReader Synchronized (TextReader reader)
  66. {
  67. if (reader == null)
  68. throw new ArgumentNullException ("reader is null");
  69. if (reader is SynchronizedReader)
  70. return reader;
  71. return new SynchronizedReader (reader);
  72. }
  73. }
  74. //
  75. // Synchronized Reader implementation, used internally.
  76. //
  77. [Serializable]
  78. internal class SynchronizedReader : TextReader {
  79. TextReader reader;
  80. public SynchronizedReader (TextReader reader)
  81. {
  82. this.reader = reader;
  83. }
  84. public override void Close ()
  85. {
  86. lock (this){
  87. reader.Close ();
  88. }
  89. }
  90. public override int Peek ()
  91. {
  92. lock (this){
  93. return reader.Peek ();
  94. }
  95. }
  96. public override int ReadBlock (char [] buffer, int index, int count)
  97. {
  98. lock (this){
  99. return reader.ReadBlock (buffer, index, count);
  100. }
  101. }
  102. public override string ReadLine ()
  103. {
  104. lock (this){
  105. return reader.ReadLine ();
  106. }
  107. }
  108. public override string ReadToEnd ()
  109. {
  110. lock (this){
  111. return reader.ReadToEnd ();
  112. }
  113. }
  114. #region Read Methods
  115. public override int Read ()
  116. {
  117. lock (this){
  118. return reader.Read ();
  119. }
  120. }
  121. public override int Read (char [] buffer, int index, int count)
  122. {
  123. lock (this){
  124. return reader.Read (buffer, index, count);
  125. }
  126. }
  127. #endregion
  128. }
  129. }