SqlXmlTextReader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using Mono.Data.Tds.Protocol;
  36. using System;
  37. using System.IO;
  38. using System.Text;
  39. namespace System.Data.SqlClient {
  40. internal sealed class SqlXmlTextReader : TextReader, IDisposable
  41. {
  42. #region Fields
  43. bool disposed = false;
  44. bool eof = false;
  45. SqlDataReader reader;
  46. string localBuffer = "<results>";
  47. int position;
  48. #endregion // Fields
  49. #region Constructors
  50. internal SqlXmlTextReader (SqlDataReader reader)
  51. : base ()
  52. {
  53. this.reader = reader;
  54. }
  55. #endregion
  56. #region Methods
  57. public override void Close()
  58. {
  59. reader.Close ();
  60. }
  61. protected override void Dispose (bool disposing)
  62. {
  63. if (!disposed) {
  64. if (disposing) {
  65. Close ();
  66. ((IDisposable) reader).Dispose ();
  67. }
  68. disposed = true;
  69. }
  70. }
  71. void IDisposable.Dispose ()
  72. {
  73. Dispose (true);
  74. GC.SuppressFinalize (this);
  75. }
  76. private bool GetNextBuffer ()
  77. {
  78. if (eof) {
  79. localBuffer = null;
  80. return false;
  81. }
  82. position = 0;
  83. if (reader.Read ())
  84. localBuffer = reader.GetString (0);
  85. else if (reader.NextResult () && reader.Read ())
  86. localBuffer = reader.GetString (0);
  87. else {
  88. eof = true;
  89. localBuffer = "</results>";
  90. }
  91. return true;
  92. }
  93. public override int Peek ()
  94. {
  95. bool moreResults;
  96. if (localBuffer == null || localBuffer.Length == 0) {
  97. moreResults = GetNextBuffer ();
  98. if (!moreResults)
  99. return -1;
  100. }
  101. if (eof && position >= localBuffer.Length)
  102. return -1;
  103. return (int) localBuffer[position];
  104. }
  105. public override int Read ()
  106. {
  107. int result = Peek ();
  108. position += 1;
  109. if (!eof && position >= localBuffer.Length)
  110. GetNextBuffer ();
  111. return result;
  112. }
  113. public override int Read (char[] buffer, int index, int count)
  114. {
  115. bool moreResults = true;
  116. int countRead = 0;
  117. if (localBuffer == null)
  118. moreResults = GetNextBuffer ();
  119. while (moreResults && count - countRead > localBuffer.Length - position) {
  120. localBuffer.CopyTo (position, buffer, index + countRead, localBuffer.Length);
  121. countRead += localBuffer.Length;
  122. moreResults = GetNextBuffer ();
  123. }
  124. if (moreResults && countRead < count) {
  125. localBuffer.CopyTo (position, buffer, index + countRead, count - countRead);
  126. position += count - countRead;
  127. }
  128. return countRead;
  129. }
  130. public override int ReadBlock (char[] buffer, int index, int count)
  131. {
  132. return Read (buffer, index, count);
  133. }
  134. public override string ReadLine ()
  135. {
  136. bool moreResults = true;
  137. string outBuffer;
  138. if (localBuffer == null)
  139. moreResults = GetNextBuffer ();
  140. if (!moreResults)
  141. return null;
  142. outBuffer = localBuffer;
  143. GetNextBuffer ();
  144. return outBuffer;
  145. }
  146. public override string ReadToEnd ()
  147. {
  148. string outBuffer = String.Empty;
  149. bool moreResults = true;
  150. if (localBuffer == null)
  151. moreResults = GetNextBuffer ();
  152. while (moreResults) {
  153. outBuffer += localBuffer;
  154. moreResults = GetNextBuffer ();
  155. }
  156. return outBuffer;
  157. }
  158. #endregion // Methods
  159. }
  160. }