1- /* Copyright 2010-2016 MongoDB Inc.
1+ /* Copyright 2010-2017 MongoDB Inc.
22*
33* Licensed under the Apache License, Version 2.0 (the "License");
44* you may not use this file except in compliance with the License.
@@ -27,8 +27,6 @@ public class BsonBinaryWriter : BsonWriter
2727 // private fields
2828 private readonly Stream _baseStream ;
2929 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 > ( ) ;
3230 private BsonBinaryWriterContext _context ;
3331
3432 // constructors
@@ -60,8 +58,6 @@ public BsonBinaryWriter(Stream stream, BsonBinaryWriterSettings settings)
6058
6159 _baseStream = stream ;
6260 _bsonStream = ( stream as BsonStream ) ?? new BsonStreamAdapter ( stream ) ;
63- _settings = settings ; // already frozen by base class
64- _maxDocumentSizeStack . Push ( _settings . MaxDocumentSize ) ;
6561
6662 _context = null ;
6763 State = BsonWriterState . Initial ;
@@ -90,6 +86,18 @@ public BsonStream BsonStream
9086 get { return _bsonStream ; }
9187 }
9288
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+
93101 // public methods
94102 /// <summary>
95103 /// Closes the writer. Also closes the base stream.
@@ -128,18 +136,20 @@ public override void Flush()
128136 /// <summary>
129137 /// Pops the max document size stack, restoring the previous max document size.
130138 /// </summary>
139+ [ Obsolete ( "Use PopSettings instead." ) ]
131140 public void PopMaxDocumentSize ( )
132141 {
133- _maxDocumentSizeStack . Pop ( ) ;
142+ PopSettings ( ) ;
134143 }
135144
136145 /// <summary>
137146 /// Pushes a new max document size onto the max document size stack.
138147 /// </summary>
139148 /// <param name="maxDocumentSize">The maximum size of the document.</param>
149+ [ Obsolete ( "Use PushSettings instead." ) ]
140150 public void PushMaxDocumentSize ( int maxDocumentSize )
141151 {
142- _maxDocumentSizeStack . Push ( Math . Min ( maxDocumentSize , _maxDocumentSizeStack . Peek ( ) ) ) ;
152+ PushSettings ( s => ( ( BsonBinaryWriterSettings ) s ) . MaxDocumentSize = maxDocumentSize ) ;
143153 }
144154
145155#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
@@ -161,28 +171,28 @@ public override void WriteBinaryData(BsonBinaryData binaryData)
161171 switch ( subType )
162172 {
163173 case BsonBinarySubType . OldBinary :
164- if ( _settings . FixOldBinarySubTypeOnOutput )
174+ if ( Settings . FixOldBinarySubTypeOnOutput )
165175 {
166176 subType = BsonBinarySubType . Binary ; // replace obsolete OldBinary with new Binary sub type
167177 }
168178 break ;
169179 case BsonBinarySubType . UuidLegacy :
170180 case BsonBinarySubType . UuidStandard :
171- if ( _settings . GuidRepresentation != GuidRepresentation . Unspecified )
181+ if ( Settings . GuidRepresentation != GuidRepresentation . Unspecified )
172182 {
173- var expectedSubType = ( _settings . GuidRepresentation == GuidRepresentation . Standard ) ? BsonBinarySubType . UuidStandard : BsonBinarySubType . UuidLegacy ;
183+ var expectedSubType = ( Settings . GuidRepresentation == GuidRepresentation . Standard ) ? BsonBinarySubType . UuidStandard : BsonBinarySubType . UuidLegacy ;
174184 if ( subType != expectedSubType )
175185 {
176186 var message = string . Format (
177187 "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 ) ;
179189 throw new BsonSerializationException ( message ) ;
180190 }
181- if ( guidRepresentation != _settings . GuidRepresentation )
191+ if ( guidRepresentation != Settings . GuidRepresentation )
182192 {
183193 var message = string . Format (
184194 "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 ) ;
186196 throw new BsonSerializationException ( message ) ;
187197 }
188198 }
@@ -413,7 +423,7 @@ public override void WriteJavaScript(string code)
413423
414424 _bsonStream . WriteBsonType ( BsonType . JavaScript ) ;
415425 WriteNameHelper ( ) ;
416- _bsonStream . WriteString ( code , _settings . Encoding ) ;
426+ _bsonStream . WriteString ( code , Settings . Encoding ) ;
417427
418428 State = GetNextState ( ) ;
419429 }
@@ -434,7 +444,7 @@ public override void WriteJavaScriptWithScope(string code)
434444 WriteNameHelper ( ) ;
435445 _context = new BsonBinaryWriterContext ( _context , ContextType . JavaScriptWithScope , _bsonStream . Position ) ;
436446 _bsonStream . WriteInt32 ( 0 ) ; // reserve space for size of JavaScript with scope value
437- _bsonStream . WriteString ( code , _settings . Encoding ) ;
447+ _bsonStream . WriteString ( code , Settings . Encoding ) ;
438448
439449 State = BsonWriterState . ScopeDocument ;
440450 }
@@ -640,7 +650,7 @@ public override void WriteString(string value)
640650
641651 _bsonStream . WriteBsonType ( BsonType . String ) ;
642652 WriteNameHelper ( ) ;
643- _bsonStream . WriteString ( value , _settings . Encoding ) ;
653+ _bsonStream . WriteString ( value , Settings . Encoding ) ;
644654
645655 State = GetNextState ( ) ;
646656 }
@@ -659,7 +669,7 @@ public override void WriteSymbol(string value)
659669
660670 _bsonStream . WriteBsonType ( BsonType . Symbol ) ;
661671 WriteNameHelper ( ) ;
662- _bsonStream . WriteString ( value , _settings . Encoding ) ;
672+ _bsonStream . WriteString ( value , Settings . Encoding ) ;
663673
664674 State = GetNextState ( ) ;
665675 }
@@ -722,9 +732,9 @@ protected override void Dispose(bool disposing)
722732 private void BackpatchSize ( )
723733 {
724734 var size = _bsonStream . Position - _context . StartPosition ;
725- if ( size > _maxDocumentSizeStack . Peek ( ) )
735+ if ( size > Settings . MaxDocumentSize )
726736 {
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 ) ;
728738 throw new FormatException ( message ) ;
729739 }
730740
0 commit comments