2
0

SqlXmlTextReader.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. return false;
  69. }
  70. return true;
  71. }
  72. public override int Peek ()
  73. {
  74. bool moreResults;
  75. if (localBuffer == null || localBuffer.Length == 0) {
  76. moreResults = GetNextBuffer ();
  77. if (!moreResults)
  78. return -1;
  79. }
  80. return (int) localBuffer[position];
  81. }
  82. public override int Read ()
  83. {
  84. int result = Peek ();
  85. position += 1;
  86. return result;
  87. }
  88. public override int Read (char[] buffer, int index, int count)
  89. {
  90. bool moreResults = true;
  91. int countRead = 0;
  92. if (localBuffer == null)
  93. moreResults = GetNextBuffer ();
  94. while (moreResults && count - countRead > localBuffer.Length - position) {
  95. localBuffer.CopyTo (position, buffer, index + countRead, localBuffer.Length);
  96. countRead += localBuffer.Length;
  97. moreResults = GetNextBuffer ();
  98. }
  99. if (moreResults && countRead < count) {
  100. localBuffer.CopyTo (position, buffer, index + countRead, count - countRead);
  101. position += count - countRead;
  102. }
  103. return countRead;
  104. }
  105. public override int ReadBlock (char[] buffer, int index, int count)
  106. {
  107. return Read (buffer, index, count);
  108. }
  109. public override string ReadLine ()
  110. {
  111. bool moreResults = true;
  112. string outBuffer;
  113. if (localBuffer == null)
  114. moreResults = GetNextBuffer ();
  115. if (!moreResults)
  116. return null;
  117. outBuffer = localBuffer;
  118. GetNextBuffer ();
  119. return outBuffer;
  120. }
  121. public override string ReadToEnd ()
  122. {
  123. string outBuffer = String.Empty;
  124. bool moreResults = true;
  125. if (localBuffer == null)
  126. moreResults = GetNextBuffer ();
  127. while (moreResults) {
  128. outBuffer += localBuffer;
  129. moreResults = GetNextBuffer ();
  130. }
  131. return outBuffer;
  132. }
  133. #endregion // Methods
  134. }
  135. }