Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SqlBytes.cs
Go to the documentation of this file.
1using System.IO;
3using System.Xml;
6
8
9[XmlSchemaProvider("GetXsdType")]
11{
12 internal byte[] _rgbBuf;
13
14 private long _lCurLen;
15
16 internal Stream _stream;
17
19
20 private byte[] _rgbWorkBuf;
21
22 public bool IsNull => _state == SqlBytesCharsState.Null;
23
24 public byte[]? Buffer
25 {
26 get
27 {
28 if (FStream())
29 {
31 }
32 return _rgbBuf;
33 }
34 }
35
36 public long Length => _state switch
37 {
40 _ => _lCurLen,
41 };
42
43 public long MaxLength
44 {
45 get
46 {
48 if (state == SqlBytesCharsState.Stream)
49 {
50 return -1L;
51 }
52 return (_rgbBuf == null) ? (-1) : _rgbBuf.Length;
53 }
54 }
55
56 public byte[] Value
57 {
58 get
59 {
60 byte[] array;
61 switch (_state)
62 {
63 case SqlBytesCharsState.Null:
64 throw new SqlNullValueException();
65 case SqlBytesCharsState.Stream:
66 if (_stream.Length > int.MaxValue)
67 {
69 }
70 array = new byte[_stream.Length];
71 if (_stream.Position != 0L)
72 {
73 _stream.Seek(0L, SeekOrigin.Begin);
74 }
76 break;
77 default:
78 array = new byte[_lCurLen];
80 break;
81 }
82 return array;
83 }
84 }
85
86 public byte this[long offset]
87 {
88 get
89 {
91 {
92 throw new ArgumentOutOfRangeException("offset");
93 }
94 if (_rgbWorkBuf == null)
95 {
96 _rgbWorkBuf = new byte[1];
97 }
98 Read(offset, _rgbWorkBuf, 0, 1);
99 return _rgbWorkBuf[0];
100 }
101 set
102 {
103 if (_rgbWorkBuf == null)
104 {
105 _rgbWorkBuf = new byte[1];
106 }
107 _rgbWorkBuf[0] = value;
108 Write(offset, _rgbWorkBuf, 0, 1);
109 }
110 }
111
112 public StorageState Storage => _state switch
113 {
117 _ => StorageState.UnmanagedBuffer,
118 };
119
121 {
122 get
123 {
124 if (!FStream())
125 {
126 return new StreamOnSqlBytes(this);
127 }
128 return _stream;
129 }
130 set
131 {
132 _lCurLen = -1L;
133 _stream = value;
134 _state = ((value != null) ? SqlBytesCharsState.Stream : SqlBytesCharsState.Null);
135 }
136 }
137
138 public static SqlBytes Null => new SqlBytes((byte[]?)null);
139
140 public SqlBytes()
141 {
142 SetNull();
143 }
144
145 public SqlBytes(byte[]? buffer)
146 {
147 _rgbBuf = buffer;
148 _stream = null;
149 if (_rgbBuf == null)
150 {
152 _lCurLen = -1L;
153 }
154 else
155 {
156 _state = SqlBytesCharsState.Buffer;
157 _lCurLen = _rgbBuf.Length;
158 }
159 _rgbWorkBuf = null;
160 }
161
164 {
165 }
166
168 {
169 _rgbBuf = null;
170 _lCurLen = -1L;
171 _stream = s;
172 _state = ((s != null) ? SqlBytesCharsState.Stream : SqlBytesCharsState.Null);
173 _rgbWorkBuf = null;
174 }
175
176 public void SetNull()
177 {
178 _lCurLen = -1L;
179 _stream = null;
181 }
182
183 public void SetLength(long value)
184 {
185 if (value < 0)
186 {
187 throw new ArgumentOutOfRangeException("value");
188 }
189 if (FStream())
190 {
192 return;
193 }
194 if (_rgbBuf == null)
195 {
197 }
198 if (value > _rgbBuf.Length)
199 {
200 throw new ArgumentOutOfRangeException("value");
201 }
202 if (IsNull)
203 {
204 _state = SqlBytesCharsState.Buffer;
205 }
206 _lCurLen = value;
207 }
208
209 public long Read(long offset, byte[] buffer, int offsetInBuffer, int count)
210 {
211 if (IsNull)
212 {
213 throw new SqlNullValueException();
214 }
215 if (buffer == null)
216 {
217 throw new ArgumentNullException("buffer");
218 }
219 if (offset > Length || offset < 0)
220 {
221 throw new ArgumentOutOfRangeException("offset");
222 }
223 if (offsetInBuffer > buffer.Length || offsetInBuffer < 0)
224 {
225 throw new ArgumentOutOfRangeException("offsetInBuffer");
226 }
228 {
229 throw new ArgumentOutOfRangeException("count");
230 }
231 if (count > Length - offset)
232 {
233 count = (int)(Length - offset);
234 }
235 if (count != 0)
236 {
238 if (state == SqlBytesCharsState.Stream)
239 {
240 if (_stream.Position != offset)
241 {
243 }
245 }
246 else
247 {
249 }
250 }
251 return count;
252 }
253
254 public void Write(long offset, byte[] buffer, int offsetInBuffer, int count)
255 {
256 if (FStream())
257 {
258 if (_stream.Position != offset)
259 {
261 }
263 return;
264 }
265 if (buffer == null)
266 {
267 throw new ArgumentNullException("buffer");
268 }
269 if (_rgbBuf == null)
270 {
272 }
273 if (offset < 0)
274 {
275 throw new ArgumentOutOfRangeException("offset");
276 }
277 if (offset > _rgbBuf.Length)
278 {
280 }
282 {
283 throw new ArgumentOutOfRangeException("offsetInBuffer");
284 }
286 {
287 throw new ArgumentOutOfRangeException("count");
288 }
289 if (count > _rgbBuf.Length - offset)
290 {
292 }
293 if (IsNull)
294 {
295 if (offset != 0L)
296 {
298 }
299 _lCurLen = 0L;
300 _state = SqlBytesCharsState.Buffer;
301 }
302 else if (offset > _lCurLen)
303 {
305 }
306 if (count != 0)
307 {
309 if (_lCurLen < offset + count)
310 {
312 }
313 }
314 }
315
317 {
318 if (!IsNull)
319 {
320 return new SqlBinary(Value);
321 }
322 return SqlBinary.Null;
323 }
324
325 public static explicit operator SqlBinary(SqlBytes value)
326 {
327 return value.ToSqlBinary();
328 }
329
330 public static explicit operator SqlBytes(SqlBinary value)
331 {
332 return new SqlBytes(value);
333 }
334
335 private void CopyStreamToBuffer()
336 {
337 long length = _stream.Length;
338 if (length >= int.MaxValue)
339 {
341 }
342 if (_rgbBuf == null || _rgbBuf.Length < length)
343 {
344 _rgbBuf = new byte[length];
345 }
346 if (_stream.Position != 0L)
347 {
348 _stream.Seek(0L, SeekOrigin.Begin);
349 }
350 _stream.Read(_rgbBuf, 0, (int)length);
351 _stream = null;
353 _state = SqlBytesCharsState.Buffer;
354 }
355
356 internal bool FStream()
357 {
358 return _state == SqlBytesCharsState.Stream;
359 }
360
361 private void SetBuffer(byte[] buffer)
362 {
363 _rgbBuf = buffer;
364 _lCurLen = ((_rgbBuf == null) ? (-1) : _rgbBuf.Length);
365 _stream = null;
366 _state = ((_rgbBuf != null) ? SqlBytesCharsState.Buffer : SqlBytesCharsState.Null);
367 }
368
370 {
371 return null;
372 }
373
375 {
376 byte[] buffer = null;
377 string attribute = r.GetAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance");
378 if (attribute != null && XmlConvert.ToBoolean(attribute))
379 {
380 r.ReadElementString();
381 SetNull();
382 }
383 else
384 {
385 string text = r.ReadElementString();
386 if (text == null)
387 {
388 buffer = Array.Empty<byte>();
389 }
390 else
391 {
392 text = text.Trim();
393 buffer = ((text.Length != 0) ? Convert.FromBase64String(text) : Array.Empty<byte>());
394 }
395 }
397 }
398
400 {
401 if (IsNull)
402 {
403 writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
404 return;
405 }
406 byte[] buffer = Buffer;
407 writer.WriteString(Convert.ToBase64String(buffer, 0, (int)Length));
408 }
409
411 {
412 return new XmlQualifiedName("base64Binary", "http://www.w3.org/2001/XMLSchema");
413 }
414
419}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static string ToBase64String(byte[] inArray)
Definition Convert.cs:2675
static unsafe byte[] FromBase64String(string s)
Definition Convert.cs:2904
void SetBuffer(byte[] buffer)
Definition SqlBytes.cs:361
void SetLength(long value)
Definition SqlBytes.cs:183
SqlBytes(SqlBinary value)
Definition SqlBytes.cs:162
void Write(long offset, byte[] buffer, int offsetInBuffer, int count)
Definition SqlBytes.cs:254
static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet)
Definition SqlBytes.cs:410
long Read(long offset, byte[] buffer, int offsetInBuffer, int count)
Definition SqlBytes.cs:209
SqlBytesCharsState _state
Definition SqlBytes.cs:18
void SetLength(long value)
long Seek(long offset, SeekOrigin origin)
int Read(byte[] buffer, int offset, int count)
void Write(byte[] buffer, int offset, int count)
static readonly Stream Null
Definition Stream.cs:488
static string SqlMisc_BufferInsufficientMessage
Definition SR.cs:870
static string SqlMisc_NoBufferMessage
Definition SR.cs:868
static string SqlMisc_WriteNonZeroOffsetOnNullMessage
Definition SR.cs:872
static string SqlMisc_WriteOffsetLargerThanLenMessage
Definition SR.cs:874
Definition SR.cs:7
static bool ToBoolean(string s)
void GetObjectData(SerializationInfo info, StreamingContext context)
static readonly SqlBinary Null
Definition SqlBinary.cs:14