TextReader.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 (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 read_count = 0;
  48. do {
  49. read_count = Read (buffer, index, count);
  50. index += read_count;
  51. count -= read_count;
  52. } while (read_count != 0 && count > 0);
  53. return read_count;
  54. }
  55. public virtual string ReadLine()
  56. {
  57. return String.Empty;
  58. }
  59. public virtual string ReadToEnd()
  60. {
  61. return String.Empty;
  62. }
  63. public static TextReader Synchronized (TextReader reader)
  64. {
  65. if (reader == null)
  66. throw new ArgumentNullException ("reader is null");
  67. if (reader is SynchronizedReader)
  68. return reader;
  69. return new SynchronizedReader (reader);
  70. }
  71. }
  72. //
  73. // Synchronized Reader implementation, used internally.
  74. //
  75. [Serializable]
  76. internal class SynchronizedReader : TextReader {
  77. TextReader reader;
  78. public SynchronizedReader (TextReader reader)
  79. {
  80. this.reader = reader;
  81. }
  82. public override void Close ()
  83. {
  84. lock (this){
  85. reader.Close ();
  86. }
  87. }
  88. public override int Peek ()
  89. {
  90. lock (this){
  91. return reader.Peek ();
  92. }
  93. }
  94. public override int ReadBlock (char [] buffer, int index, int count)
  95. {
  96. lock (this){
  97. return reader.ReadBlock (buffer, index, count);
  98. }
  99. }
  100. public override string ReadLine ()
  101. {
  102. lock (this){
  103. return reader.ReadLine ();
  104. }
  105. }
  106. public override string ReadToEnd ()
  107. {
  108. lock (this){
  109. return reader.ReadToEnd ();
  110. }
  111. }
  112. #region Read Methods
  113. public override int Read ()
  114. {
  115. lock (this){
  116. return reader.Read ();
  117. }
  118. }
  119. public override int Read (char [] buffer, int index, int count)
  120. {
  121. lock (this){
  122. return reader.Read (buffer, index, count);
  123. }
  124. }
  125. #endregion
  126. }
  127. }