SqlXmlTextReader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // System.Data.SqlClient.SqlXmlTextReader.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002
  10. // (C) Daniel Morgan 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. using Mono.Data.Tds.Protocol;
  14. using System;
  15. using System.IO;
  16. using System.Text;
  17. namespace System.Data.SqlClient {
  18. internal sealed class SqlXmlTextReader : TextReader, IDisposable
  19. {
  20. #region Fields
  21. bool disposed = false;
  22. bool eof = false;
  23. SqlDataReader reader;
  24. string localBuffer = "<results>";
  25. int position;
  26. #endregion // Fields
  27. #region Constructors
  28. internal SqlXmlTextReader (SqlDataReader reader)
  29. : base ()
  30. {
  31. this.reader = reader;
  32. }
  33. #endregion
  34. #region Methods
  35. public override void Close()
  36. {
  37. reader.Close ();
  38. }
  39. protected override void Dispose (bool disposing)
  40. {
  41. if (!disposed) {
  42. if (disposing) {
  43. Close ();
  44. ((IDisposable) reader).Dispose ();
  45. }
  46. disposed = true;
  47. }
  48. }
  49. void IDisposable.Dispose ()
  50. {
  51. Dispose (true);
  52. GC.SuppressFinalize (this);
  53. }
  54. private bool GetNextBuffer ()
  55. {
  56. if (eof) {
  57. localBuffer = null;
  58. return false;
  59. }
  60. position = 0;
  61. if (reader.Read ())
  62. localBuffer = reader.GetString (0);
  63. else if (reader.NextResult () && reader.Read ())
  64. localBuffer = reader.GetString (0);
  65. else {
  66. eof = true;
  67. localBuffer = "</results>";
  68. }
  69. return true;
  70. }
  71. public override int Peek ()
  72. {
  73. bool moreResults;
  74. if (localBuffer == null || localBuffer.Length == 0) {
  75. moreResults = GetNextBuffer ();
  76. if (!moreResults)
  77. return -1;
  78. }
  79. if (eof && position >= localBuffer.Length)
  80. return -1;
  81. return (int) localBuffer[position];
  82. }
  83. public override int Read ()
  84. {
  85. int result = Peek ();
  86. position += 1;
  87. if (!eof && position >= localBuffer.Length)
  88. GetNextBuffer ();
  89. return result;
  90. }
  91. public override int Read (char[] buffer, int index, int count)
  92. {
  93. bool moreResults = true;
  94. int countRead = 0;
  95. if (localBuffer == null)
  96. moreResults = GetNextBuffer ();
  97. while (moreResults && count - countRead > localBuffer.Length - position) {
  98. localBuffer.CopyTo (position, buffer, index + countRead, localBuffer.Length);
  99. countRead += localBuffer.Length;
  100. moreResults = GetNextBuffer ();
  101. }
  102. if (moreResults && countRead < count) {
  103. localBuffer.CopyTo (position, buffer, index + countRead, count - countRead);
  104. position += count - countRead;
  105. }
  106. return countRead;
  107. }
  108. public override int ReadBlock (char[] buffer, int index, int count)
  109. {
  110. return Read (buffer, index, count);
  111. }
  112. public override string ReadLine ()
  113. {
  114. bool moreResults = true;
  115. string outBuffer;
  116. if (localBuffer == null)
  117. moreResults = GetNextBuffer ();
  118. if (!moreResults)
  119. return null;
  120. outBuffer = localBuffer;
  121. GetNextBuffer ();
  122. return outBuffer;
  123. }
  124. public override string ReadToEnd ()
  125. {
  126. string outBuffer = String.Empty;
  127. bool moreResults = true;
  128. if (localBuffer == null)
  129. moreResults = GetNextBuffer ();
  130. while (moreResults) {
  131. outBuffer += localBuffer;
  132. moreResults = GetNextBuffer ();
  133. }
  134. return outBuffer;
  135. }
  136. #endregion // Methods
  137. }
  138. }