**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / System.Data.SqlClient / SqlXmlTextReader.cs
blob8035991039d1f4e0cbffaca42149324cfff0dfe4
1 //
2 // System.Data.SqlClient.SqlXmlTextReader.cs
3 //
4 // Author:
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
7 // Tim Coleman (tim@timcoleman.com)
8 //
9 // (C) Ximian, Inc 2002
10 // (C) Daniel Morgan 2002
11 // Copyright (C) Tim Coleman, 2002
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 //
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 //
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using Mono.Data.Tds.Protocol;
38 using System;
39 using System.IO;
40 using System.Text;
42 namespace System.Data.SqlClient {
43 internal sealed class SqlXmlTextReader : TextReader, IDisposable
45 #region Fields
47 bool disposed = false;
48 bool eof = false;
49 SqlDataReader reader;
50 string localBuffer = "<results>";
51 int position;
53 #endregion // Fields
55 #region Constructors
57 internal SqlXmlTextReader (SqlDataReader reader)
58 : base ()
60 this.reader = reader;
63 #endregion
65 #region Methods
67 public override void Close()
69 reader.Close ();
72 protected override void Dispose (bool disposing)
74 if (!disposed) {
75 if (disposing) {
76 Close ();
77 ((IDisposable) reader).Dispose ();
79 disposed = true;
83 void IDisposable.Dispose ()
85 Dispose (true);
86 GC.SuppressFinalize (this);
89 private bool GetNextBuffer ()
91 if (eof) {
92 localBuffer = null;
93 return false;
96 position = 0;
97 if (reader.Read ())
98 localBuffer = reader.GetString (0);
99 else if (reader.NextResult () && reader.Read ())
100 localBuffer = reader.GetString (0);
101 else {
102 eof = true;
103 localBuffer = "</results>";
105 return true;
108 public override int Peek ()
110 bool moreResults;
111 if (localBuffer == null || localBuffer.Length == 0) {
112 moreResults = GetNextBuffer ();
113 if (!moreResults)
114 return -1;
116 if (eof && position >= localBuffer.Length)
117 return -1;
118 return (int) localBuffer[position];
121 public override int Read ()
123 int result = Peek ();
124 position += 1;
125 if (!eof && position >= localBuffer.Length)
126 GetNextBuffer ();
127 return result;
130 public override int Read (char[] buffer, int index, int count)
132 bool moreResults = true;
133 int countRead = 0;
135 if (localBuffer == null)
136 moreResults = GetNextBuffer ();
138 while (moreResults && count - countRead > localBuffer.Length - position) {
139 localBuffer.CopyTo (position, buffer, index + countRead, localBuffer.Length);
140 countRead += localBuffer.Length;
141 moreResults = GetNextBuffer ();
143 if (moreResults && countRead < count) {
144 localBuffer.CopyTo (position, buffer, index + countRead, count - countRead);
145 position += count - countRead;
148 return countRead;
151 public override int ReadBlock (char[] buffer, int index, int count)
153 return Read (buffer, index, count);
156 public override string ReadLine ()
158 bool moreResults = true;
159 string outBuffer;
160 if (localBuffer == null)
161 moreResults = GetNextBuffer ();
162 if (!moreResults)
163 return null;
164 outBuffer = localBuffer;
165 GetNextBuffer ();
166 return outBuffer;
169 public override string ReadToEnd ()
171 string outBuffer = String.Empty;
173 bool moreResults = true;
174 if (localBuffer == null)
175 moreResults = GetNextBuffer ();
176 while (moreResults) {
177 outBuffer += localBuffer;
178 moreResults = GetNextBuffer ();
180 return outBuffer;
183 #endregion // Methods