e270bc00b86b1cc4621587f93ade23751adda0df
[mono.git] / mcs / class / referencesource / System.Data / System / Data / SQLTypes / SqlCharStream.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SqlStreamChars.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 //  </copyright>                                                                
5 // <owner current="true" primary="true">junfang</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="true" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
9
10 //**************************************************************************
11 // @File: SqlStreamChars.cs
12 //
13 // Create by:   JunFang
14 //
15 // Description: 
16 //
17 // Notes: 
18 //      
19 // History:
20 //
21 //   04/17/01  JunFang  Created.
22 //
23 // @EndHeader@
24 //**************************************************************************
25
26 namespace System.Data.SqlTypes 
27         {
28         using System;
29         using System.IO;
30         using System.Runtime.InteropServices;
31         using System.Data.SqlTypes;
32
33         internal abstract class SqlStreamChars: System.Data.SqlTypes.INullable, IDisposable
34                 {
35                 public abstract bool IsNull { get; }
36
37                 public abstract bool CanRead { get; }
38
39                 public abstract bool CanSeek { get; }
40
41                 public abstract bool CanWrite { get; }
42
43                 public abstract long Length { get; }
44
45                 public abstract long Position { get; set; }
46
47                 // --------------------------------------------------------------
48                 //        Public methods
49                 // --------------------------------------------------------------
50                 public abstract int Read (char[] buffer, int offset, int count);
51
52                 public abstract void Write (char[] buffer, int offset, int count);
53
54                 public abstract long Seek (long offset, SeekOrigin origin);
55
56                 public abstract void SetLength (long value);
57
58                 public abstract void Flush ();
59
60                 public virtual void Close(){
61                         Dispose(true);
62                 }
63
64                 void IDisposable.Dispose() {
65             Dispose(true);           
66         }
67
68                 protected virtual void Dispose(bool disposing) {           
69                 }
70
71                 public virtual int ReadChar()
72                         {
73                         // Reads one char from the stream by calling Read(char[], int, int). 
74                         // Will return an char cast to an int or -1 on end of stream.
75                         // The performance of the default implementation on Stream is bad,
76                         // and any subclass with an internal buffer should override this method.
77                         char[] oneCharArray = new char[1];
78                         int r = Read(oneCharArray, 0, 1);
79                         if (r==0)
80                                 return -1;
81                         return oneCharArray[0];
82                         }
83
84                 public virtual void WriteChar(char value)
85                         {
86                         // Writes one char from the stream by calling Write(char[], int, int).  
87                         // The performance of the default implementation on Stream is bad,
88                         // and any subclass with an internal buffer should override this method.
89                         char[] oneCharArray = new char[1];
90                         oneCharArray[0] = value;
91                         Write(oneCharArray, 0, 1);
92                         }
93
94
95                 // Private class: the Null SqlStreamChars
96                 private class NullSqlStreamChars : SqlStreamChars
97                         {
98                         // --------------------------------------------------------------
99                         //        Constructor(s)
100                         // --------------------------------------------------------------
101
102                         internal NullSqlStreamChars()
103                                 {
104                                 }
105
106
107                         // --------------------------------------------------------------
108                         //        Public properties
109                         // --------------------------------------------------------------
110
111                         public override bool IsNull 
112                                 {
113                                 get 
114                                         {
115                                         return true;
116                                         }
117                                 }
118
119                         public override bool CanRead
120                                 {
121                                 get
122                                         {
123                                         return false;
124                                         }
125                                 }
126
127                         public override bool CanSeek
128                                 {
129                                 get
130                                         {
131                                         return false;
132                                         }
133                                 }
134
135                         public override bool CanWrite
136                                 {
137                                 get
138                                         {
139                                         return false;
140                                         }
141                                 }
142
143                         public override long Length
144                                 {
145                                 get
146                                         {
147                                         throw new SqlNullValueException();
148                                         }
149                                 }
150
151                         public override long Position
152                                 {
153                                 get
154                                         {
155                                         throw new SqlNullValueException();
156                                         }
157                                 set
158                                         {
159                                         throw new SqlNullValueException();
160                                         }
161                                 }
162
163                         // --------------------------------------------------------------
164                         //        Public methods
165                         // --------------------------------------------------------------
166                         public override int Read (char[] buffer, int offset, int count)
167                                 {
168                                 throw new SqlNullValueException();
169                                 }
170
171                         public override void Write (char[] buffer, int offset, int count)
172                                 {
173                                 throw new SqlNullValueException();
174                                 }
175
176                         public override long Seek (long offset, SeekOrigin origin)
177                                 {
178                                 throw new SqlNullValueException();
179                                 }
180
181                         public override void SetLength (long value)
182                                 {
183                                 throw new SqlNullValueException();
184                                 }
185
186                         public override void Flush ()
187                                 {
188                                 throw new SqlNullValueException();
189                                 }
190
191                         public override void Close ()
192                                 {
193                                 }
194
195                         } // class NullSqlStreamChars
196
197
198                 // The Null instance
199                 public static SqlStreamChars Null {
200                     get {
201                         return new NullSqlStreamChars();
202                     }
203                 }
204
205                 } // class SqlStreamChars
206
207         } // namespace System.Data.SqlTypes