File: SqlBytes.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (314 lines) | stat: -rw-r--r-- 7,428 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//
// System.Data.SqlTypes.SqlBytes
//
// Author:
//   Tim Coleman <tim@timcoleman.com>
//
// Copyright (C) Tim Coleman, 2003
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#if NET_2_0

using System;
using System.Globalization;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace System.Data.SqlTypes
{
	[SerializableAttribute]
	[XmlSchemaProvider ("GetXsdType")]
	public sealed class SqlBytes : INullable, IXmlSerializable, ISerializable
	{
		#region Fields

		bool notNull;
		byte [] buffer;
		StorageState storage = StorageState.UnmanagedBuffer;
		Stream stream = null;

		#endregion

		#region Constructors

		public SqlBytes ()
		{
			buffer = null;
			notNull = false;
		}

		public SqlBytes (byte[] buffer)
		{
			if (buffer == null) {
				notNull = false;
				buffer = null;
			}
			else {
				notNull = true;
				this.buffer = buffer;
				storage = StorageState.Buffer;
			}
		}

		public SqlBytes (SqlBinary value)
		{
			if (value.IsNull) {
				notNull = false;
				buffer = null;
			}
			else {
				notNull = true;
				buffer = value.Value;
				storage = StorageState.Buffer;
			}
		}

		public SqlBytes (Stream s)
		{
			if (s == null) {
				notNull = false;
				buffer = null;
			} else {
				notNull = true;
				int len = (int) s.Length;
				buffer = new byte [len];
				s.Read (buffer, 0, len);
				storage = StorageState.Stream;
				stream = s;
			}
		}

		#endregion

		#region Properties

		public byte [] Buffer {
			get { return buffer; }
		}

		public bool IsNull {
			get { return !notNull; }
		}

		public byte this [long offset] {
			set {
				if (notNull && offset >= 0 && offset < buffer.Length)
					buffer [offset] = value;
			}
			get {
				if (buffer == null)
					throw new SqlNullValueException ("Data is Null");
				if (offset < 0 || offset >= buffer.Length)
					throw new ArgumentOutOfRangeException ("Parameter name: offset");
				return buffer [offset];
			}
		}

		public long Length {
			get {
				if (!notNull || buffer == null)
					throw new SqlNullValueException ("Data is Null");
				if (buffer.Length < 0)
					return -1;
				return buffer.Length;
			}
		}

		public long MaxLength {
			get {
				if (!notNull || buffer == null || storage == StorageState.Stream)
					return -1;
				return buffer.Length;
			}
		}

		public static SqlBytes Null {
			get {
				return new SqlBytes ();
			}
		}

		public StorageState Storage {
			get {
				if (storage == StorageState.UnmanagedBuffer)
					throw new SqlNullValueException ("Data is Null");
				return storage;
			}
		}

		public Stream Stream {
			set {
				stream = value;
			}
			get {
				return stream;
			}
		}

		public byte [] Value {
			get {
				if (buffer == null)
					return buffer;
				return (byte []) buffer.Clone ();
			}
		}

		#endregion

		#region Methods

		public void SetLength (long value)
		{
			if (buffer == null)
				throw new SqlTypeException ("There is no buffer. Read or write operation failed.");
			if (value < 0 || value > buffer.Length)
				throw new ArgumentOutOfRangeException ("Specified argument was out of the range of valid values.");
			Array.Resize (ref buffer, (int) value);
		}

		public void SetNull ()
		{
			buffer = null;
			notNull = false;
		}

		public static explicit operator SqlBytes (SqlBinary value)
		{
			if (value.IsNull)
				return Null;
			else
				return new SqlBytes (value.Value);
		}

		public static explicit operator SqlBinary (SqlBytes value)
		{
			if (value.IsNull)
				return SqlBinary.Null;
			else
				return new SqlBinary (value.Value);
		}

		public SqlBinary ToSqlBinary ()
		{
			return new SqlBinary (buffer);
		}

		public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
		{
			XmlQualifiedName qualifiedName = new XmlQualifiedName ("base64Binary", "http://www.w3.org/2001/XMLSchema");
			return qualifiedName;
		}
		
		[MonoTODO]
		XmlSchema IXmlSerializable.GetSchema ()
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		void IXmlSerializable.ReadXml (XmlReader r)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		void IXmlSerializable.WriteXml (XmlWriter writer)
		{
			throw new NotImplementedException ();
		}

		[MonoTODO]
		void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
		{
			throw new NotImplementedException ();
		}

		public long Read (long offset, byte [] buffer, int offsetInBuffer, int count)
		{
			if (buffer == null)
				throw new ArgumentNullException ("buffer");

			if (IsNull)
				throw new SqlNullValueException ("There is no buffer. Read or write failed");
			
			if (count > MaxLength || count > buffer.Length || 
			    count < 0 || ((offsetInBuffer + count) > buffer.Length))
				throw new ArgumentOutOfRangeException ("count");
			
			if (offset < 0 || offset > MaxLength)
				throw new ArgumentOutOfRangeException ("offset");
			
			if (offsetInBuffer < 0 || offsetInBuffer > buffer.Length)
				throw new ArgumentOutOfRangeException ("offsetInBuffer");
			
			/* Final count of what will be copied */
			long actualCount = count;
			if (count + offset > Length )
				actualCount = Length - offset;
			
			Array.Copy (this.buffer, offset, buffer, offsetInBuffer, actualCount);
			
			return actualCount;
		}

		public void Write (long offset, byte [] buffer, int offsetInBuffer, int count)
		{
			if (buffer == null)
				throw new ArgumentNullException ("buffer");

			if (IsNull)
				throw new SqlTypeException ("There is no buffer. Read or write operation failed.");
							
			if (offset < 0) 
				throw new ArgumentOutOfRangeException ("offset");
			
			if (offsetInBuffer < 0 || offsetInBuffer > buffer.Length 
			    || offsetInBuffer > Length 
			    || offsetInBuffer + count > Length
			    || offsetInBuffer + count > buffer.Length)
				throw new ArgumentOutOfRangeException ("offsetInBuffer");
			
			if (count < 0 || count > MaxLength)
				throw new ArgumentOutOfRangeException ("count");
			
			if (offset > MaxLength || offset+count > MaxLength)
				throw new SqlTypeException ("The buffer is insufficient. Read or write operation failed.");
			
			if (count + offset > Length && 
			    count + offset <= MaxLength)
				SetLength (count);
			
			Array.Copy (buffer, offsetInBuffer, this.buffer, offset, count);
		}

		#endregion
	}
}

#endif