1
- /* Copyright 2010-2016 MongoDB Inc.
1
+ /* Copyright 2010-2017 MongoDB Inc.
2
2
*
3
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License.
@@ -27,8 +27,6 @@ public class BsonBinaryWriter : BsonWriter
27
27
// private fields
28
28
private readonly Stream _baseStream ;
29
29
private readonly BsonStream _bsonStream ;
30
- private readonly BsonBinaryWriterSettings _settings ; // same value as in base class just declared as derived class
31
- private readonly Stack < int > _maxDocumentSizeStack = new Stack < int > ( ) ;
32
30
private BsonBinaryWriterContext _context ;
33
31
34
32
// constructors
@@ -60,8 +58,6 @@ public BsonBinaryWriter(Stream stream, BsonBinaryWriterSettings settings)
60
58
61
59
_baseStream = stream ;
62
60
_bsonStream = ( stream as BsonStream ) ?? new BsonStreamAdapter ( stream ) ;
63
- _settings = settings ; // already frozen by base class
64
- _maxDocumentSizeStack . Push ( _settings . MaxDocumentSize ) ;
65
61
66
62
_context = null ;
67
63
State = BsonWriterState . Initial ;
@@ -90,6 +86,18 @@ public BsonStream BsonStream
90
86
get { return _bsonStream ; }
91
87
}
92
88
89
+ /// <inheritdoc />
90
+ public override long Position
91
+ {
92
+ get { return _baseStream . Position ; }
93
+ }
94
+
95
+ /// <inheritdoc />
96
+ public new BsonBinaryWriterSettings Settings
97
+ {
98
+ get { return ( BsonBinaryWriterSettings ) base . Settings ; }
99
+ }
100
+
93
101
// public methods
94
102
/// <summary>
95
103
/// Closes the writer. Also closes the base stream.
@@ -128,18 +136,20 @@ public override void Flush()
128
136
/// <summary>
129
137
/// Pops the max document size stack, restoring the previous max document size.
130
138
/// </summary>
139
+ [ Obsolete ( "Use PopSettings instead." ) ]
131
140
public void PopMaxDocumentSize ( )
132
141
{
133
- _maxDocumentSizeStack . Pop ( ) ;
142
+ PopSettings ( ) ;
134
143
}
135
144
136
145
/// <summary>
137
146
/// Pushes a new max document size onto the max document size stack.
138
147
/// </summary>
139
148
/// <param name="maxDocumentSize">The maximum size of the document.</param>
149
+ [ Obsolete ( "Use PushSettings instead." ) ]
140
150
public void PushMaxDocumentSize ( int maxDocumentSize )
141
151
{
142
- _maxDocumentSizeStack . Push ( Math . Min ( maxDocumentSize , _maxDocumentSizeStack . Peek ( ) ) ) ;
152
+ PushSettings ( s => ( ( BsonBinaryWriterSettings ) s ) . MaxDocumentSize = maxDocumentSize ) ;
143
153
}
144
154
145
155
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
@@ -161,28 +171,28 @@ public override void WriteBinaryData(BsonBinaryData binaryData)
161
171
switch ( subType )
162
172
{
163
173
case BsonBinarySubType . OldBinary :
164
- if ( _settings . FixOldBinarySubTypeOnOutput )
174
+ if ( Settings . FixOldBinarySubTypeOnOutput )
165
175
{
166
176
subType = BsonBinarySubType . Binary ; // replace obsolete OldBinary with new Binary sub type
167
177
}
168
178
break ;
169
179
case BsonBinarySubType . UuidLegacy :
170
180
case BsonBinarySubType . UuidStandard :
171
- if ( _settings . GuidRepresentation != GuidRepresentation . Unspecified )
181
+ if ( Settings . GuidRepresentation != GuidRepresentation . Unspecified )
172
182
{
173
- var expectedSubType = ( _settings . GuidRepresentation == GuidRepresentation . Standard ) ? BsonBinarySubType . UuidStandard : BsonBinarySubType . UuidLegacy ;
183
+ var expectedSubType = ( Settings . GuidRepresentation == GuidRepresentation . Standard ) ? BsonBinarySubType . UuidStandard : BsonBinarySubType . UuidLegacy ;
174
184
if ( subType != expectedSubType )
175
185
{
176
186
var message = string . Format (
177
187
"The GuidRepresentation for the writer is {0}, which requires the subType argument to be {1}, not {2}." ,
178
- _settings . GuidRepresentation , expectedSubType , subType ) ;
188
+ Settings . GuidRepresentation , expectedSubType , subType ) ;
179
189
throw new BsonSerializationException ( message ) ;
180
190
}
181
- if ( guidRepresentation != _settings . GuidRepresentation )
191
+ if ( guidRepresentation != Settings . GuidRepresentation )
182
192
{
183
193
var message = string . Format (
184
194
"The GuidRepresentation for the writer is {0}, which requires the the guidRepresentation argument to also be {0}, not {1}." ,
185
- _settings . GuidRepresentation , guidRepresentation ) ;
195
+ Settings . GuidRepresentation , guidRepresentation ) ;
186
196
throw new BsonSerializationException ( message ) ;
187
197
}
188
198
}
@@ -413,7 +423,7 @@ public override void WriteJavaScript(string code)
413
423
414
424
_bsonStream . WriteBsonType ( BsonType . JavaScript ) ;
415
425
WriteNameHelper ( ) ;
416
- _bsonStream . WriteString ( code , _settings . Encoding ) ;
426
+ _bsonStream . WriteString ( code , Settings . Encoding ) ;
417
427
418
428
State = GetNextState ( ) ;
419
429
}
@@ -434,7 +444,7 @@ public override void WriteJavaScriptWithScope(string code)
434
444
WriteNameHelper ( ) ;
435
445
_context = new BsonBinaryWriterContext ( _context , ContextType . JavaScriptWithScope , _bsonStream . Position ) ;
436
446
_bsonStream . WriteInt32 ( 0 ) ; // reserve space for size of JavaScript with scope value
437
- _bsonStream . WriteString ( code , _settings . Encoding ) ;
447
+ _bsonStream . WriteString ( code , Settings . Encoding ) ;
438
448
439
449
State = BsonWriterState . ScopeDocument ;
440
450
}
@@ -640,7 +650,7 @@ public override void WriteString(string value)
640
650
641
651
_bsonStream . WriteBsonType ( BsonType . String ) ;
642
652
WriteNameHelper ( ) ;
643
- _bsonStream . WriteString ( value , _settings . Encoding ) ;
653
+ _bsonStream . WriteString ( value , Settings . Encoding ) ;
644
654
645
655
State = GetNextState ( ) ;
646
656
}
@@ -659,7 +669,7 @@ public override void WriteSymbol(string value)
659
669
660
670
_bsonStream . WriteBsonType ( BsonType . Symbol ) ;
661
671
WriteNameHelper ( ) ;
662
- _bsonStream . WriteString ( value , _settings . Encoding ) ;
672
+ _bsonStream . WriteString ( value , Settings . Encoding ) ;
663
673
664
674
State = GetNextState ( ) ;
665
675
}
@@ -722,9 +732,9 @@ protected override void Dispose(bool disposing)
722
732
private void BackpatchSize ( )
723
733
{
724
734
var size = _bsonStream . Position - _context . StartPosition ;
725
- if ( size > _maxDocumentSizeStack . Peek ( ) )
735
+ if ( size > Settings . MaxDocumentSize )
726
736
{
727
- var message = string . Format ( "Size {0} is larger than MaxDocumentSize {1}." , size , _maxDocumentSizeStack . Peek ( ) ) ;
737
+ var message = string . Format ( "Size {0} is larger than MaxDocumentSize {1}." , size , Settings . MaxDocumentSize ) ;
728
738
throw new FormatException ( message ) ;
729
739
}
730
740
0 commit comments