New test.
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlXmlTextReader.cs
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
12 //
13
14 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
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.
35 //
36
37 using Mono.Data.Tds.Protocol;
38 using System;
39 using System.IO;
40 using System.Text;
41
42 namespace System.Data.SqlClient {
43         internal sealed class SqlXmlTextReader : TextReader, IDisposable
44         {
45                 #region Fields
46
47                 bool disposed = false;
48                 bool eof = false;
49                 SqlDataReader reader;
50                 string localBuffer = "<results>";
51                 int position;
52
53                 #endregion // Fields
54
55                 #region Constructors
56
57                 internal SqlXmlTextReader (SqlDataReader reader)
58                         : base ()
59                 {
60                         this.reader = reader;
61                 }
62
63                 #endregion
64
65                 #region Methods
66
67                 public override void Close()
68                 {
69                         reader.Close ();        
70                 }
71
72                 protected override void Dispose (bool disposing)
73                 {
74                         if (!disposed) {
75                                 if (disposing) {
76                                         Close ();
77                                         ((IDisposable) reader).Dispose ();
78                                 }
79                                 disposed = true;
80                         }
81                 }
82
83                 void IDisposable.Dispose ()
84                 {
85                         Dispose (true);
86                         GC.SuppressFinalize (this);
87                 }
88
89                 private bool GetNextBuffer ()
90                 {
91                         if (eof) {
92                                 localBuffer = null;
93                                 return false;
94                         }
95
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>";
104                         }
105                         return true;
106                 }
107
108                 public override int Peek ()
109                 {
110                         bool moreResults;
111                         if (localBuffer == null || localBuffer.Length == 0) {
112                                 moreResults = GetNextBuffer ();
113                                 if (!moreResults)
114                                         return -1;
115                         }
116                         if (eof && position >= localBuffer.Length)
117                                 return -1;
118                         return (int) localBuffer[position];
119                 }
120                         
121                 public override int Read ()
122                 {
123                         int result = Peek ();
124                         position += 1;
125                         if (!eof && position >= localBuffer.Length)
126                                 GetNextBuffer ();
127                         return result;
128                 }       
129
130                 public override int Read (char[] buffer, int index, int count)
131                 {
132                         bool moreResults = true;
133                         int countRead = 0;
134
135                         if (localBuffer == null)
136                                 moreResults = GetNextBuffer ();
137
138                         while (moreResults && count - countRead > localBuffer.Length - position) {
139                                 localBuffer.CopyTo (position, buffer, index + countRead, localBuffer.Length);
140                                 countRead += localBuffer.Length;
141                                 moreResults = GetNextBuffer ();
142                         }
143                         if (moreResults && countRead < count) {
144                                 localBuffer.CopyTo (position, buffer, index + countRead, count - countRead);
145                                 position += count - countRead;
146                         }
147
148                         return countRead;
149                 }
150
151                 public override int ReadBlock (char[] buffer, int index, int count)
152                 {
153                         return Read (buffer, index, count);
154                 }
155
156                 public override string ReadLine ()
157                 {
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;
167                 }
168
169                 public override string ReadToEnd ()
170                 {
171                         string outBuffer = String.Empty;
172
173                         bool moreResults = true;
174                         if (localBuffer == null)
175                                 moreResults = GetNextBuffer ();
176                         while (moreResults) {
177                                 outBuffer += localBuffer;
178                                 moreResults = GetNextBuffer ();
179                         }
180                         return outBuffer;
181                 }
182
183                 #endregion // Methods
184         }
185 }