-
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathGdsBlob.cs
More file actions
556 lines (462 loc) · 14 KB
/
GdsBlob.cs
File metadata and controls
556 lines (462 loc) · 14 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FirebirdSql.Data.Common;
namespace FirebirdSql.Data.Client.Managed.Version10;
internal sealed class GdsBlob : BlobBase
{
const int DataSegment = 0;
const int SeekMode = 0;
#region Fields
private readonly GdsDatabase _database;
private int _blobHandle;
#endregion
#region Properties
public override DatabaseBase Database
{
get { return _database; }
}
public override int Handle
{
get { return _blobHandle; }
}
#endregion
#region Constructors
public GdsBlob(GdsDatabase database, GdsTransaction transaction)
: this(database, transaction, 0)
{ }
public GdsBlob(GdsDatabase database, GdsTransaction transaction, long blobId)
: base(database)
{
_database = database;
_transaction = transaction;
_position = 0;
_blobHandle = 0;
_blobId = blobId;
}
#endregion
#region Protected Methods
public override void Create()
{
try
{
CreateOrOpen(IscCodes.op_create_blob, null);
RblAddValue(IscCodes.RBL_create);
}
catch (IscException)
{
throw;
}
}
public override async ValueTask CreateAsync(CancellationToken cancellationToken = default)
{
try
{
await CreateOrOpenAsync(IscCodes.op_create_blob, null, cancellationToken).ConfigureAwait(false);
RblAddValue(IscCodes.RBL_create);
}
catch (IscException)
{
throw;
}
}
public override void Open()
{
try
{
CreateOrOpen(IscCodes.op_open_blob, null);
}
catch (IscException)
{
throw;
}
}
public override async ValueTask OpenAsync(CancellationToken cancellationToken = default)
{
try
{
await CreateOrOpenAsync(IscCodes.op_open_blob, null, cancellationToken).ConfigureAwait(false);
}
catch (IscException)
{
throw;
}
}
public override int GetLength()
{
try
{
if (!IsOpen)
Open();
var bufferLength = 20;
var buffer = new byte[bufferLength];
_database.Xdr.Write(IscCodes.op_info_blob);
_database.Xdr.Write(_blobHandle);
_database.Xdr.Write(0);
_database.Xdr.WriteBuffer(new byte[] { IscCodes.isc_info_blob_total_length }, 1);
_database.Xdr.Write(bufferLength);
_database.Xdr.Flush();
var response = (GenericResponse)_database.ReadResponse();
var responseLength = bufferLength;
if (response.Data.Length < bufferLength)
{
responseLength = response.Data.Length;
}
Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength);
var length = IscHelper.VaxInteger(buffer, 1, 2);
var size = IscHelper.VaxInteger(buffer, 3, (int)length);
return (int)size;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override async ValueTask<int> GetLengthAsync(CancellationToken cancellationToken = default)
{
try
{
if (!IsOpen)
await OpenAsync(cancellationToken).ConfigureAwait(false);
var bufferLength = 20;
var buffer = new byte[bufferLength];
await _database.Xdr.WriteAsync(IscCodes.op_info_blob, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobHandle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(0, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteBufferAsync(new byte[] { IscCodes.isc_info_blob_total_length }, 1, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(bufferLength, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
var response = (GenericResponse)await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
var responseLength = bufferLength;
if (response.Data.Length < bufferLength)
{
responseLength = response.Data.Length;
}
Buffer.BlockCopy(response.Data, 0, buffer, 0, responseLength);
var length = IscHelper.VaxInteger(buffer, 1, 2);
var size = IscHelper.VaxInteger(buffer, 3, (int)length);
return (int)size;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override void GetSegment(Stream stream)
{
var requested = SegmentSize;
try
{
_database.Xdr.Write(IscCodes.op_get_segment);
_database.Xdr.Write(_blobHandle);
_database.Xdr.Write(requested < ushort.MaxValue ? requested : ushort.MaxValue);
_database.Xdr.Write(DataSegment);
_database.Xdr.Flush();
var response = (GenericResponse)_database.ReadResponse();
RblRemoveValue(IscCodes.RBL_segment);
if (response.ObjectHandle == 1)
{
RblAddValue(IscCodes.RBL_segment);
}
else if (response.ObjectHandle == 2)
{
RblAddValue(IscCodes.RBL_eof_pending);
}
var buffer = response.Data;
if (buffer.Length == 0)
{
// previous segment was last, this has no data
return;
}
var len = 0;
var srcpos = 0;
while (srcpos < buffer.Length)
{
len = (int)IscHelper.VaxInteger(buffer, srcpos, 2);
srcpos += 2;
stream.Write(buffer, srcpos, len);
srcpos += len;
}
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override async ValueTask GetSegmentAsync(Stream stream, CancellationToken cancellationToken = default)
{
var requested = SegmentSize;
try
{
await _database.Xdr.WriteAsync(IscCodes.op_get_segment, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobHandle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(requested < ushort.MaxValue ? requested : ushort.MaxValue, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(DataSegment, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
var response = (GenericResponse)await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
RblRemoveValue(IscCodes.RBL_segment);
if (response.ObjectHandle == 1)
{
RblAddValue(IscCodes.RBL_segment);
}
else if (response.ObjectHandle == 2)
{
RblAddValue(IscCodes.RBL_eof_pending);
}
var buffer = response.Data;
if (buffer.Length == 0)
{
//previous segment was last, this has no data
return;
}
var len = 0;
var srcpos = 0;
while (srcpos < buffer.Length)
{
len = (int)IscHelper.VaxInteger(buffer, srcpos, 2);
srcpos += 2;
stream.Write(buffer, srcpos, len);
srcpos += len;
}
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override byte[] GetSegment()
{
var requested = SegmentSize;
try
{
_database.Xdr.Write(IscCodes.op_get_segment);
_database.Xdr.Write(_blobHandle);
_database.Xdr.Write(requested < ushort.MaxValue ? requested : ushort.MaxValue);
_database.Xdr.Write(DataSegment);
_database.Xdr.Flush();
var response = (GenericResponse)_database.ReadResponse();
RblRemoveValue(IscCodes.RBL_segment);
if (response.ObjectHandle == 1)
{
RblAddValue(IscCodes.RBL_segment);
}
else if (response.ObjectHandle == 2)
{
RblAddValue(IscCodes.RBL_eof_pending);
}
var buffer = response.Data;
if (buffer.Length == 0)
{
//previous segment was last, this has no data
return Array.Empty<byte>();
}
var posInInput = 0;
var posInOutput = 0;
var tmp = new byte[requested * 2];
while (posInInput < buffer.Length)
{
var len = (int)IscHelper.VaxInteger(buffer, posInInput, 2);
posInInput += 2;
Array.Copy(buffer, posInInput, tmp, posInOutput, len);
posInOutput += len;
posInInput += len;
}
var actualBuffer = new byte[posInOutput];
Array.Copy(tmp, actualBuffer, posInOutput);
return actualBuffer;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override async ValueTask<byte[]> GetSegmentAsync(CancellationToken cancellationToken = default)
{
var requested = SegmentSize;
try
{
await _database.Xdr.WriteAsync(IscCodes.op_get_segment, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobHandle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(requested < ushort.MaxValue ? requested : ushort.MaxValue, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(DataSegment, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
var response = (GenericResponse)await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
RblRemoveValue(IscCodes.RBL_segment);
if (response.ObjectHandle == 1)
{
RblAddValue(IscCodes.RBL_segment);
}
else if (response.ObjectHandle == 2)
{
RblAddValue(IscCodes.RBL_eof_pending);
}
var buffer = response.Data;
if (buffer.Length == 0)
{
// previous segment was last, this has no data
return Array.Empty<byte>();
}
var posInInput = 0;
var posInOutput = 0;
var tmp = new byte[requested * 2];
while (posInInput < buffer.Length)
{
var len = (int)IscHelper.VaxInteger(buffer, posInInput, 2);
posInInput += 2;
Array.Copy(buffer, posInInput, tmp, posInOutput, len);
posInOutput += len;
posInInput += len;
}
var actualBuffer = new byte[posInOutput];
Array.Copy(tmp, actualBuffer, posInOutput);
return actualBuffer;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override void PutSegment(byte[] buffer)
{
try
{
_database.Xdr.Write(IscCodes.op_batch_segments);
_database.Xdr.Write(_blobHandle);
_database.Xdr.WriteBlobBuffer(buffer);
_database.Xdr.Flush();
_database.ReadResponse();
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override async ValueTask PutSegmentAsync(byte[] buffer, CancellationToken cancellationToken = default)
{
try
{
await _database.Xdr.WriteAsync(IscCodes.op_batch_segments, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobHandle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteBlobBufferAsync(buffer, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override void Seek(int offset, int seekMode)
{
try
{
_database.Xdr.Write(IscCodes.op_seek_blob);
_database.Xdr.Write(_blobHandle);
_database.Xdr.Write(seekMode);
_database.Xdr.Write(offset);
_database.Xdr.Flush();
var response = (GenericResponse)_database.ReadResponse();
_position = response.ObjectHandle;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override async ValueTask SeekAsync(int offset, int seekMode, CancellationToken cancellationToken = default)
{
try
{
await _database.Xdr.WriteAsync(IscCodes.op_seek_blob, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobHandle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(seekMode, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(offset, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
var response = (GenericResponse)await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
_position = response.ObjectHandle;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
public override void Close()
{
_database.ReleaseObject(IscCodes.op_close_blob, _blobHandle);
}
public override ValueTask CloseAsync(CancellationToken cancellationToken = default)
{
return _database.ReleaseObjectAsync(IscCodes.op_close_blob, _blobHandle, cancellationToken);
}
public override void Cancel()
{
_database.ReleaseObject(IscCodes.op_cancel_blob, _blobHandle);
}
public override ValueTask CancelAsync(CancellationToken cancellationToken = default)
{
return _database.ReleaseObjectAsync(IscCodes.op_cancel_blob, _blobHandle, cancellationToken);
}
#endregion
#region Private API Methods
private void CreateOrOpen(int op, BlobParameterBuffer bpb)
{
try
{
_database.Xdr.Write(op);
if (bpb != null)
{
_database.Xdr.WriteTyped(IscCodes.isc_bpb_version1, bpb.ToArray());
}
_database.Xdr.Write(_transaction.Handle);
_database.Xdr.Write(_blobId);
_database.Xdr.Flush();
var response = (GenericResponse)_database.ReadResponse();
_blobId = response.BlobId;
_blobHandle = response.ObjectHandle;
_isOpen = true;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
private async ValueTask CreateOrOpenAsync(int op, BlobParameterBuffer bpb, CancellationToken cancellationToken = default)
{
try
{
await _database.Xdr.WriteAsync(op, cancellationToken).ConfigureAwait(false);
if (bpb != null)
{
await _database.Xdr.WriteTypedAsync(IscCodes.isc_bpb_version1, bpb.ToArray(), cancellationToken).ConfigureAwait(false);
}
await _database.Xdr.WriteAsync(_transaction.Handle, cancellationToken).ConfigureAwait(false);
await _database.Xdr.WriteAsync(_blobId, cancellationToken).ConfigureAwait(false);
await _database.Xdr.FlushAsync(cancellationToken).ConfigureAwait(false);
var response = (GenericResponse)await _database.ReadResponseAsync(cancellationToken).ConfigureAwait(false);
_blobId = response.BlobId;
_blobHandle = response.ObjectHandle;
_isOpen = true;
}
catch (IOException ex)
{
throw IscException.ForIOException(ex);
}
}
#endregion
}