This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
/
BsonReader.cs
412 lines (361 loc) · 14.1 KB
/
BsonReader.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
using System;
using System.IO;
using System.Text;
namespace MongoDB.Bson
{
/// <summary>
/// Reads binary streams containing BSON data and converts them to native types.
/// </summary>
public class BsonReader
{
private const int MaxCharBytesSize = 128;
private readonly IBsonObjectBuilder _builder;
private readonly BinaryReader _reader;
private readonly byte[] _seqRange1 = new byte[]{0, 127}; //Range of 1-byte sequence
private readonly byte[] _seqRange2 = new byte[]{194, 223}; //Range of 2-byte sequence
private readonly byte[] _seqRange3 = new byte[]{224, 239}; //Range of 3-byte sequence
private readonly byte[] _seqRange4 = new byte[]{240, 244}; //Range of 4-byte sequence
private readonly Stream _stream;
private readonly bool _readLocalTime;
private byte[] _byteBuffer;
private char[] _charBuffer;
/// <summary>
/// Initializes a new instance of the <see cref="BsonReader"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="settings">The settings.</param>
public BsonReader(Stream stream, BsonReaderSettings settings)
{
if(settings == null)
throw new ArgumentNullException("settings");
_builder = settings.Builder;
_readLocalTime = settings.ReadLocalTime;
Position = 0;
_stream = stream;
_reader = new BinaryReader(_stream);
}
/// <summary>
/// Initializes a new instance of the <see cref="BsonReader"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="builder">The builder.</param>
public BsonReader(Stream stream, IBsonObjectBuilder builder){
_builder = builder;
Position = 0;
_stream = stream;
_reader = new BinaryReader(_stream);
}
/// <summary>
/// Gets or sets the position.
/// </summary>
/// <value>The position.</value>
public int Position { get; private set; }
/// <summary>
/// Reads this instance.
/// </summary>
/// <returns></returns>
public Document Read(){
Position = 0;
var doc = (Document)ReadObject();
return doc;
}
/// <summary>
/// Reads the object.
/// </summary>
/// <returns></returns>
public object ReadObject(){
var instance = _builder.BeginObject();
ReadElements(instance);
return _builder.EndObject(instance);
}
/// <summary>
/// Reads the array.
/// </summary>
/// <returns></returns>
public object ReadArray(){
var instance = _builder.BeginArray();
ReadElements(instance);
return _builder.EndArray(instance);
}
/// <summary>
/// Reads the elements.
/// </summary>
/// <param name="instance">The instance.</param>
private void ReadElements(object instance){
var startPosition = Position;
var size = _reader.ReadInt32();
Position += 4;
while((Position - startPosition) + 1 < size)
ReadElement(instance);
Position++;
if(_reader.ReadByte() != 0)
throw new InvalidDataException("Document not null terminated");
if(size != Position - startPosition)
throw new InvalidDataException(string.Format("Should have read {0} bytes from stream but only read {1}",
size,
(Position - startPosition)));
}
/// <summary>
/// Reads the element.
/// </summary>
/// <param name="instance">The instance.</param>
private void ReadElement(object instance){
Position++;
var typeNumber = (sbyte)_reader.ReadByte();
var key = ReadString();
_builder.BeginProperty(instance, key);
var element = ReadElementType(typeNumber);
_builder.EndProperty(instance, key, element);
}
/// <summary>
/// Reads the type of the element.
/// </summary>
/// <param name="typeNumber">The type number.</param>
/// <returns></returns>
public Object ReadElementType(int typeNumber){
switch((BsonType)typeNumber){
case BsonType.Null:
case BsonType.Undefined:
return null;
case BsonType.MinKey:
return MongoMinKey.Value;
case BsonType.MaxKey:
return MongoMaxKey.Value;
case BsonType.Boolean:
Position++;
return _reader.ReadBoolean();
case BsonType.Integer:
Position += 4;
return _reader.ReadInt32();
case BsonType.Long:
Position += 8;
return _reader.ReadInt64();
case BsonType.Date:
return ReadDateTime();
case BsonType.Oid:
Position += 12;
return new Oid(_reader.ReadBytes(12));
case BsonType.Number:
Position += 8;
return _reader.ReadDouble();
case BsonType.String:
return ReadLengthString();
case BsonType.Symbol:
return new MongoSymbol(ReadLengthString());
case BsonType.Obj:
return ReadObject();
case BsonType.Array:
return ReadArray();
case BsonType.Regex:
return ReadRegex();
case BsonType.Code:
return ReadCode();
case BsonType.CodeWScope:
return ReadScope();
case BsonType.Binary:
return ReadBinary();
default:
throw new ArgumentOutOfRangeException(String.Format("Type Number: {0} not recognized", typeNumber));
}
}
/// <summary>
/// Reads the date time.
/// </summary>
/// <returns></returns>
private object ReadDateTime(){
Position += 8;
var milliseconds = _reader.ReadInt64();
var time = BsonInfo.Epoch.AddMilliseconds(milliseconds);
if(_readLocalTime)
time = time.ToLocalTime();
return time;
}
/// <summary>
/// Reads the string.
/// </summary>
/// <returns></returns>
public string ReadString(){
EnsureBuffers();
var builder = new StringBuilder();
var offset = 0;
do{
var count = offset;
byte readByte = 0;
while(count < MaxCharBytesSize && (readByte = _reader.ReadByte()) > 0)
_byteBuffer[count++] = readByte;
var byteCount = count - offset;
Position += byteCount;
if(count == 0)
break; //first byte read was the terminator.
var lastFullCharStop = GetLastFullCharStop(count - 1);
var charCount = Encoding.UTF8.GetChars(_byteBuffer, 0, lastFullCharStop + 1, _charBuffer, 0);
builder.Append(_charBuffer, 0, charCount);
if(lastFullCharStop < byteCount - 1){
offset = byteCount - lastFullCharStop - 1;
//Copy end bytes to begining
Array.Copy(_byteBuffer, lastFullCharStop + 1, _byteBuffer, 0, offset);
}
else
offset = 0;
if(readByte == 0)
break;
}
while(true);
Position++;
return builder.ToString();
}
/// <summary>
/// Reads the length string.
/// </summary>
/// <returns></returns>
public string ReadLengthString(){
var length = _reader.ReadInt32();
var str = GetString(length - 1);
_reader.ReadByte();
Position += (4 + 1);
return str;
}
/// <summary>
/// Gets the string.
/// </summary>
/// <param name="length">The length.</param>
/// <returns></returns>
private string GetString(int length){
if(length == 0)
return string.Empty;
EnsureBuffers();
var builder = new StringBuilder(length);
var totalBytesRead = 0;
var offset = 0;
do{
var count = ((length - totalBytesRead) > MaxCharBytesSize - offset)
? (MaxCharBytesSize - offset)
:
(length - totalBytesRead);
var byteCount = _reader.BaseStream.Read(_byteBuffer, offset, count);
totalBytesRead += byteCount;
byteCount += offset;
var lastFullCharStop = GetLastFullCharStop(byteCount - 1);
if(byteCount == 0)
throw new EndOfStreamException("Unable to read beyond the end of the stream.");
var charCount = Encoding.UTF8.GetChars(_byteBuffer, 0, lastFullCharStop + 1, _charBuffer, 0);
builder.Append(_charBuffer, 0, charCount);
if(lastFullCharStop < byteCount - 1){
offset = byteCount - lastFullCharStop - 1;
//Copy end bytes to begining
Array.Copy(_byteBuffer, lastFullCharStop + 1, _byteBuffer, 0, offset);
}
else
offset = 0;
}
while(totalBytesRead < length);
Position += totalBytesRead;
return builder.ToString();
}
/// <summary>
/// Reads the scope.
/// </summary>
/// <returns></returns>
private object ReadScope(){
var startpos = Position;
var size = _reader.ReadInt32();
Position += 4;
var val = ReadLengthString();
var scope = (Document)ReadObject();
if(size != Position - startpos)
throw new InvalidDataException(string.Format("Should have read {0} bytes from stream but read {1} in CodeWScope",
size,
Position - startpos));
return new CodeWScope(val, scope);
}
/// <summary>
/// Reads the code.
/// </summary>
/// <returns></returns>
private object ReadCode(){
return new Code{Value = ReadLengthString()};
}
/// <summary>
/// Reads the regex.
/// </summary>
/// <returns></returns>
private object ReadRegex(){
return new MongoRegex{
Expression = ReadString(),
RawOptions = ReadString()
};
}
/// <summary>
/// Reads the binary.
/// </summary>
/// <returns></returns>
private object ReadBinary(){
var size = _reader.ReadInt32();
Position += 4;
var subtype = _reader.ReadByte();
Position ++;
if(subtype == (byte)BinarySubtype.General){
size = _reader.ReadInt32();
Position += 4;
}
var bytes = _reader.ReadBytes(size);
Position += size;
// From http://en.wikipedia.org/wiki/Universally_Unique_Identifier
// The most widespread use of this standard is in Microsoft's Globally Unique Identifiers (GUIDs).
if(subtype == 3 && 16 == size)
return new Guid(bytes);
return new Binary{
Bytes = bytes,
Subtype = (BinarySubtype)subtype
};
}
/// <summary>
/// Gets the last full char stop.
/// </summary>
/// <param name="start">The start.</param>
/// <returns></returns>
private int GetLastFullCharStop(int start){
var lookbackPos = start;
var bis = 0;
while(lookbackPos >= 0){
bis = BytesInSequence(_byteBuffer[lookbackPos]);
if(bis == 0){
lookbackPos--;
continue;
}
if(bis == 1)
break;
lookbackPos--;
break;
}
return bis == start - lookbackPos ? start : lookbackPos;
}
/// <summary>
/// Byteses the in sequence.
/// </summary>
/// <param name="b">The b.</param>
/// <returns></returns>
private int BytesInSequence(byte b){
if(b <= _seqRange1[1])
return 1;
if(b >= _seqRange2[0] && b <= _seqRange2[1])
return 2;
if(b >= _seqRange3[0] && b <= _seqRange3[1])
return 3;
if(b >= _seqRange4[0] && b <= _seqRange4[1])
return 4;
return 0;
}
/// <summary>
/// Ensures the buffers.
/// </summary>
private void EnsureBuffers(){
if(_byteBuffer == null)
_byteBuffer = new byte[MaxCharBytesSize];
if(_charBuffer != null)
return;
var charBufferSize = Encoding.UTF8.GetMaxCharCount(MaxCharBytesSize);
_charBuffer = new char[charBufferSize];
}
}
}