-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathBsonBinaryReader.cs
832 lines (750 loc) · 30 KB
/
BsonBinaryReader.cs
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Globalization;
using System.IO;
namespace MongoDB.Bson.IO
{
/// <summary>
/// Represents a BSON reader for a binary BSON byte array.
/// </summary>
public class BsonBinaryReader : BsonReader
{
// private fields
#pragma warning disable CA2213 // Disposable never disposed
private readonly Stream _baseStream;
#pragma warning restore CA2213 // Disposable never disposed
private readonly BsonStream _bsonStream;
private BsonBinaryReaderContext _context;
// constructors
/// <summary>
/// Initializes a new instance of the BsonBinaryReader class.
/// </summary>
/// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
public BsonBinaryReader(Stream stream)
: this(stream, BsonBinaryReaderSettings.Defaults)
{
}
/// <summary>
/// Initializes a new instance of the BsonBinaryReader class.
/// </summary>
/// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
/// <param name="settings">A BsonBinaryReaderSettings.</param>
public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
: base(settings)
{
if (stream == null)
{
throw new ArgumentNullException("stream");
}
if (!stream.CanSeek)
{
throw new ArgumentException("The stream must be capable of seeking.", "stream");
}
_baseStream = stream;
_bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);
_context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
}
// public properties
/// <summary>
/// Gets the base stream.
/// </summary>
/// <value>
/// The base stream.
/// </value>
public Stream BaseStream
{
get { return _baseStream; }
}
/// <summary>
/// Gets the BSON stream.
/// </summary>
/// <value>
/// The BSON stream.
/// </value>
public BsonStream BsonStream
{
get { return _bsonStream; }
}
/// <summary>
/// Gets the settings of the writer.
/// </summary>
public new BsonBinaryReaderSettings Settings
{
get { return (BsonBinaryReaderSettings)base.Settings; }
}
// public methods
/// <summary>
/// Closes the reader.
/// </summary>
public override void Close()
{
// Close can be called on Disposed objects
State = BsonReaderState.Closed;
}
/// <summary>
/// Gets a bookmark to the reader's current position and state.
/// </summary>
/// <returns>A bookmark.</returns>
public override BsonReaderBookmark GetBookmark()
{
return new BsonBinaryReaderBookmark(State, CurrentBsonType, CurrentName, _context, _bsonStream.Position);
}
/// <summary>
/// Determines whether this reader is at end of file.
/// </summary>
/// <returns>
/// Whether this reader is at end of file.
/// </returns>
public override bool IsAtEndOfFile()
{
return _bsonStream.Position >= _bsonStream.Length;
}
/// <summary>
/// Reads BSON binary data from the reader.
/// </summary>
/// <returns>A BsonBinaryData.</returns>
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override BsonBinaryData ReadBinaryData()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBinaryData", BsonType.Binary);
int size = ReadSize();
var subType = _bsonStream.ReadBinarySubType();
if (subType == BsonBinarySubType.OldBinary)
{
// sub type OldBinary has two sizes (for historical reasons)
int size2 = ReadSize();
if (size2 != size - 4)
{
throw new FormatException("Binary sub type OldBinary has inconsistent sizes");
}
size = size2;
if (Settings.FixOldBinarySubTypeOnInput)
{
subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type
}
}
var bytes = _bsonStream.ReadBytes(size);
if ((subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) &&
bytes.Length != 16)
{
throw new FormatException($"Length must be 16, not {bytes.Length}, when subType is {subType}.");
}
var binaryData = new BsonBinaryData(bytes, subType);
State = GetNextState();
return binaryData;
}
#pragma warning restore 618
/// <summary>
/// Reads a BSON boolean from the reader.
/// </summary>
/// <returns>A Boolean.</returns>
public override bool ReadBoolean()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBoolean", BsonType.Boolean);
State = GetNextState();
return _bsonStream.ReadBoolean();
}
/// <summary>
/// Reads a BsonType from the reader.
/// </summary>
/// <returns>A BsonType.</returns>
public override BsonType ReadBsonType()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State == BsonReaderState.Initial || State == BsonReaderState.ScopeDocument)
{
// there is an implied type of Document for the top level and for scope documents
CurrentBsonType = BsonType.Document;
State = BsonReaderState.Value;
return CurrentBsonType;
}
if (State != BsonReaderState.Type)
{
ThrowInvalidState("ReadBsonType", BsonReaderState.Type);
}
if (_context.ContextType == ContextType.Array)
{
_context.CurrentArrayIndex++;
}
try
{
CurrentBsonType = _bsonStream.ReadBsonType();
}
catch (FormatException ex)
{
if (ex.Message.StartsWith("Detected unknown BSON type", StringComparison.Ordinal))
{
// insert the element name into the error message
var periodIndex = ex.Message.IndexOf('.');
var dottedElementName = GenerateDottedElementName();
var message = ex.Message.Substring(0, periodIndex) + $" for fieldname \"{dottedElementName}\"" + ex.Message.Substring(periodIndex);
throw new FormatException(message);
}
throw;
}
if (CurrentBsonType == BsonType.EndOfDocument)
{
switch (_context.ContextType)
{
case ContextType.Array:
State = BsonReaderState.EndOfArray;
return BsonType.EndOfDocument;
case ContextType.Document:
case ContextType.ScopeDocument:
State = BsonReaderState.EndOfDocument;
return BsonType.EndOfDocument;
default:
var message = string.Format("BsonType EndOfDocument is not valid when ContextType is {0}.", _context.ContextType);
throw new FormatException(message);
}
}
else
{
switch (_context.ContextType)
{
case ContextType.Array:
_bsonStream.SkipCString(); // ignore array element names
State = BsonReaderState.Value;
break;
case ContextType.Document:
case ContextType.ScopeDocument:
State = BsonReaderState.Name;
break;
default:
throw new BsonInternalException("Unexpected ContextType.");
}
return CurrentBsonType;
}
}
/// <summary>
/// Reads BSON binary data from the reader.
/// </summary>
/// <returns>A byte array.</returns>
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
public override byte[] ReadBytes()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadBytes", BsonType.Binary);
int size = ReadSize();
var subType = _bsonStream.ReadBinarySubType();
if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary)
{
var message = string.Format("ReadBytes requires the binary sub type to be Binary, not {0}.", subType);
throw new FormatException(message);
}
State = GetNextState();
return _bsonStream.ReadBytes(size);
}
#pragma warning restore 618
/// <summary>
/// Reads a BSON DateTime from the reader.
/// </summary>
/// <returns>The number of milliseconds since the Unix epoch.</returns>
public override long ReadDateTime()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadDateTime", BsonType.DateTime);
State = GetNextState();
var value = _bsonStream.ReadInt64();
if (value == BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch + 1)
{
if (Settings.FixOldDateTimeMaxValueOnInput)
{
value = BsonConstants.DateTimeMaxValueMillisecondsSinceEpoch;
}
}
return value;
}
/// <inheritdoc />
public override Decimal128 ReadDecimal128()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType(nameof(ReadDecimal128), BsonType.Decimal128);
State = GetNextState();
return _bsonStream.ReadDecimal128();
}
/// <summary>
/// Reads a BSON Double from the reader.
/// </summary>
/// <returns>A Double.</returns>
public override double ReadDouble()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadDouble", BsonType.Double);
State = GetNextState();
return _bsonStream.ReadDouble();
}
/// <summary>
/// Reads the end of a BSON array from the reader.
/// </summary>
public override void ReadEndArray()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (_context.ContextType != ContextType.Array)
{
ThrowInvalidContextType("ReadEndArray", _context.ContextType, ContextType.Array);
}
if (State == BsonReaderState.Type)
{
ReadBsonType(); // will set state to EndOfArray if at end of array
}
if (State != BsonReaderState.EndOfArray)
{
ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
}
_context = _context.PopContext(_bsonStream.Position);
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
}
/// <summary>
/// Reads the end of a BSON document from the reader.
/// </summary>
public override void ReadEndDocument()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (_context.ContextType != ContextType.Document && _context.ContextType != ContextType.ScopeDocument)
{
ThrowInvalidContextType("ReadEndDocument", _context.ContextType, ContextType.Document, ContextType.ScopeDocument);
}
if (State == BsonReaderState.Type)
{
ReadBsonType(); // will set state to EndOfDocument if at end of document
}
if (State != BsonReaderState.EndOfDocument)
{
ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
}
_context = _context.PopContext(_bsonStream.Position);
if (_context.ContextType == ContextType.JavaScriptWithScope)
{
_context = _context.PopContext(_bsonStream.Position); // JavaScriptWithScope
}
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
}
/// <summary>
/// Reads a BSON Int32 from the reader.
/// </summary>
/// <returns>An Int32.</returns>
public override int ReadInt32()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadInt32", BsonType.Int32);
State = GetNextState();
return _bsonStream.ReadInt32();
}
/// <summary>
/// Reads a BSON Int64 from the reader.
/// </summary>
/// <returns>An Int64.</returns>
public override long ReadInt64()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadInt64", BsonType.Int64);
State = GetNextState();
return _bsonStream.ReadInt64();
}
/// <summary>
/// Reads a BSON JavaScript from the reader.
/// </summary>
/// <returns>A string.</returns>
public override string ReadJavaScript()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadJavaScript", BsonType.JavaScript);
State = GetNextState();
return _bsonStream.ReadString(Settings.Encoding);
}
/// <summary>
/// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
/// </summary>
/// <returns>A string.</returns>
public override string ReadJavaScriptWithScope()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = _context.PushContext(ContextType.JavaScriptWithScope, startPosition, size);
var code = _bsonStream.ReadString(Settings.Encoding);
State = BsonReaderState.ScopeDocument;
return code;
}
/// <summary>
/// Reads a BSON MaxKey from the reader.
/// </summary>
public override void ReadMaxKey()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadMaxKey", BsonType.MaxKey);
State = GetNextState();
}
/// <summary>
/// Reads a BSON MinKey from the reader.
/// </summary>
public override void ReadMinKey()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadMinKey", BsonType.MinKey);
State = GetNextState();
}
/// <summary>
/// Reads the name of an element from the reader.
/// </summary>
/// <param name="nameDecoder">The name decoder.</param>
/// <returns>The name of the element.</returns>
public override string ReadName(INameDecoder nameDecoder)
{
if (nameDecoder == null)
{
throw new ArgumentNullException("nameDecoder");
}
if (Disposed) { ThrowObjectDisposedException(); }
if (State == BsonReaderState.Type)
{
ReadBsonType();
}
if (State != BsonReaderState.Name)
{
ThrowInvalidState("ReadName", BsonReaderState.Name);
}
CurrentName = nameDecoder.Decode(_bsonStream, Settings.Encoding);
State = BsonReaderState.Value;
if (_context.ContextType == ContextType.Document)
{
_context.CurrentElementName = CurrentName;
}
return CurrentName;
}
/// <summary>
/// Reads a BSON null from the reader.
/// </summary>
public override void ReadNull()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadNull", BsonType.Null);
State = GetNextState();
}
/// <summary>
/// Reads a BSON ObjectId from the reader.
/// </summary>
/// <returns>An ObjectId.</returns>
public override ObjectId ReadObjectId()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadObjectId", BsonType.ObjectId);
State = GetNextState();
return _bsonStream.ReadObjectId();
}
/// <summary>
/// Reads a raw BSON array.
/// </summary>
/// <returns>
/// The raw BSON array.
/// </returns>
public override IByteBuffer ReadRawBsonArray()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadRawBsonArray", BsonType.Array);
var slice = _bsonStream.ReadSlice();
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
return slice;
}
/// <summary>
/// Reads a raw BSON document.
/// </summary>
/// <returns>
/// The raw BSON document.
/// </returns>
public override IByteBuffer ReadRawBsonDocument()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadRawBsonDocument", BsonType.Document);
var slice = _bsonStream.ReadSlice();
if (_context.ContextType == ContextType.JavaScriptWithScope)
{
_context = _context.PopContext(_bsonStream.Position); // JavaScriptWithScope
}
switch (_context.ContextType)
{
case ContextType.Array: State = BsonReaderState.Type; break;
case ContextType.Document: State = BsonReaderState.Type; break;
case ContextType.TopLevel: State = BsonReaderState.Initial; break;
default: throw new BsonInternalException("Unexpected ContextType.");
}
return slice;
}
/// <summary>
/// Reads a BSON regular expression from the reader.
/// </summary>
/// <returns>A BsonRegularExpression.</returns>
public override BsonRegularExpression ReadRegularExpression()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadRegularExpression", BsonType.RegularExpression);
State = GetNextState();
var pattern = _bsonStream.ReadCString(Settings.Encoding);
var options = _bsonStream.ReadCString(Settings.Encoding);
return new BsonRegularExpression(pattern, options);
}
/// <summary>
/// Reads the start of a BSON array.
/// </summary>
public override void ReadStartArray()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadStartArray", BsonType.Array);
var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = _context.PushContext(ContextType.Array, startPosition, size);
State = BsonReaderState.Type;
}
/// <summary>
/// Reads the start of a BSON document.
/// </summary>
public override void ReadStartDocument()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadStartDocument", BsonType.Document);
var contextType = (State == BsonReaderState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
var startPosition = _bsonStream.Position; // position of size field
var size = ReadSize();
_context = _context.PushContext(contextType, startPosition, size);
State = BsonReaderState.Type;
}
/// <summary>
/// Reads a BSON string from the reader.
/// </summary>
/// <returns>A String.</returns>
public override string ReadString()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadString", BsonType.String);
State = GetNextState();
return _bsonStream.ReadString(Settings.Encoding);
}
/// <summary>
/// Reads a BSON symbol from the reader.
/// </summary>
/// <returns>A string.</returns>
public override string ReadSymbol()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadSymbol", BsonType.Symbol);
State = GetNextState();
return _bsonStream.ReadString(Settings.Encoding);
}
/// <summary>
/// Reads a BSON timestamp from the reader.
/// </summary>
/// <returns>The combined timestamp/increment.</returns>
public override long ReadTimestamp()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadTimestamp", BsonType.Timestamp);
State = GetNextState();
return _bsonStream.ReadInt64();
}
/// <summary>
/// Reads a BSON undefined from the reader.
/// </summary>
public override void ReadUndefined()
{
if (Disposed) { ThrowObjectDisposedException(); }
VerifyBsonType("ReadUndefined", BsonType.Undefined);
State = GetNextState();
}
/// <summary>
/// Returns the reader to previously bookmarked position and state.
/// </summary>
/// <param name="bookmark">The bookmark.</param>
public override void ReturnToBookmark(BsonReaderBookmark bookmark)
{
var binaryReaderBookmark = (BsonBinaryReaderBookmark)bookmark;
State = binaryReaderBookmark.State;
CurrentBsonType = binaryReaderBookmark.CurrentBsonType;
CurrentName = binaryReaderBookmark.CurrentName;
_context = binaryReaderBookmark.CloneContext();
_bsonStream.Position = binaryReaderBookmark.Position;
}
/// <summary>
/// Skips the name (reader must be positioned on a name).
/// </summary>
public override void SkipName()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State != BsonReaderState.Name)
{
ThrowInvalidState("SkipName", BsonReaderState.Name);
}
_bsonStream.SkipCString();
CurrentName = null;
State = BsonReaderState.Value;
if (_context.ContextType == ContextType.Document)
{
_context.CurrentElementName = CurrentName;
}
}
/// <summary>
/// Skips the value (reader must be positioned on a value).
/// </summary>
public override void SkipValue()
{
if (Disposed) { ThrowObjectDisposedException(); }
if (State != BsonReaderState.Value)
{
ThrowInvalidState("SkipValue", BsonReaderState.Value);
}
int skip;
switch (CurrentBsonType)
{
case BsonType.Array: skip = ReadSize() - 4; break;
case BsonType.Binary: skip = ReadSize() + 1; break;
case BsonType.Boolean: skip = 1; break;
case BsonType.DateTime: skip = 8; break;
case BsonType.Document: skip = ReadSize() - 4; break;
case BsonType.Decimal128: skip = 16; break;
case BsonType.Double: skip = 8; break;
case BsonType.Int32: skip = 4; break;
case BsonType.Int64: skip = 8; break;
case BsonType.JavaScript: skip = ReadSize(); break;
case BsonType.JavaScriptWithScope: skip = ReadSize() - 4; break;
case BsonType.MaxKey: skip = 0; break;
case BsonType.MinKey: skip = 0; break;
case BsonType.Null: skip = 0; break;
case BsonType.ObjectId: skip = 12; break;
case BsonType.RegularExpression: _bsonStream.SkipCString(); _bsonStream.SkipCString(); skip = 0; break;
case BsonType.String: skip = ReadSize(); break;
case BsonType.Symbol: skip = ReadSize(); break;
case BsonType.Timestamp: skip = 8; break;
case BsonType.Undefined: skip = 0; break;
default: throw new BsonInternalException("Unexpected BsonType.");
}
_bsonStream.Seek(skip, SeekOrigin.Current);
State = BsonReaderState.Type;
}
// protected methods
/// <summary>
/// Disposes of any resources used by the reader.
/// </summary>
/// <param name="disposing">True if called from Dispose.</param>
protected override void Dispose(bool disposing)
{
// don't Dispose the _stream because we don't own it
if (disposing)
{
try
{
Close();
}
catch { } // ignore exceptions
}
base.Dispose(disposing);
}
// private methods
private string GenerateDottedElementName()
{
string elementName;
if (_context.ContextType == ContextType.Document)
{
try
{
elementName = _bsonStream.ReadCString(Utf8Encodings.Lenient);
}
catch
{
elementName = "?"; // ignore exception
}
}
else if (_context.ContextType == ContextType.Array)
{
elementName = _context.CurrentArrayIndex.ToString(NumberFormatInfo.InvariantInfo);
}
else
{
elementName = "?";
}
return GenerateDottedElementName(_context.ParentContext, elementName);
}
private string GenerateDottedElementName(BsonBinaryReaderContext context, string elementName)
{
if (context.ContextType == ContextType.Document)
{
return GenerateDottedElementName(context.ParentContext, (context.CurrentElementName ?? "?") + "." + elementName);
}
else if (context.ContextType == ContextType.Array)
{
var indexElementName = context.CurrentArrayIndex.ToString(NumberFormatInfo.InvariantInfo);
return GenerateDottedElementName(context.ParentContext, indexElementName + "." + elementName);
}
else if (context.ParentContext != null)
{
return GenerateDottedElementName(context.ParentContext, "?." + elementName);
}
else
{
return elementName;
}
}
private BsonReaderState GetNextState()
{
switch (_context.ContextType)
{
case ContextType.Array:
case ContextType.Document:
case ContextType.ScopeDocument:
return BsonReaderState.Type;
case ContextType.TopLevel:
return BsonReaderState.Initial;
default:
throw new BsonInternalException("Unexpected ContextType.");
}
}
private int ReadSize()
{
int size = _bsonStream.ReadInt32();
if (size < 0)
{
var message = string.Format("Size {0} is not valid because it is negative.", size);
throw new FormatException(message);
}
if (size > Settings.MaxDocumentSize)
{
var message = string.Format("Size {0} is not valid because it is larger than MaxDocumentSize {1}.", size, Settings.MaxDocumentSize);
throw new FormatException(message);
}
return size;
}
}
}