BsonBinaryWriterSettings.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Text;
  17. namespace MongoDB.Bson.IO
  18. {
  19. /// <summary>
  20. /// Represents settings for a BsonBinaryWriter.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonBinaryWriterSettings : BsonWriterSettings
  24. {
  25. // private static fields
  26. private static BsonBinaryWriterSettings __defaults = null; // delay creation to pick up the latest default values
  27. // private fields
  28. private bool _closeOutput = false;
  29. private UTF8Encoding _encoding = new UTF8Encoding(false, true);
  30. private bool _fixOldBinarySubTypeOnOutput = true;
  31. private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the BsonBinaryWriterSettings class.
  35. /// </summary>
  36. public BsonBinaryWriterSettings()
  37. {
  38. }
  39. /// <summary>
  40. /// Initializes a new instance of the BsonBinaryWriterSettings class.
  41. /// </summary>
  42. /// <param name="closeOutput">Whether to close the output stream when the writer is closed.</param>
  43. /// <param name="fixOldBinarySubTypeOnOutput">Whether to fix old binary data subtype on output.</param>
  44. /// <param name="guidRepresentation">The representation for Guids.</param>
  45. /// <param name="maxDocumentSize">The max document size.</param>
  46. [Obsolete("Use the no-argument constructor instead and set the properties.")]
  47. public BsonBinaryWriterSettings(
  48. bool closeOutput,
  49. bool fixOldBinarySubTypeOnOutput,
  50. GuidRepresentation guidRepresentation,
  51. int maxDocumentSize)
  52. : base(guidRepresentation)
  53. {
  54. _closeOutput = closeOutput;
  55. _fixOldBinarySubTypeOnOutput = fixOldBinarySubTypeOnOutput;
  56. _maxDocumentSize = maxDocumentSize;
  57. }
  58. // public static properties
  59. /// <summary>
  60. /// Gets or sets the default BsonBinaryWriter settings.
  61. /// </summary>
  62. public static BsonBinaryWriterSettings Defaults
  63. {
  64. get
  65. {
  66. if (__defaults == null)
  67. {
  68. __defaults = new BsonBinaryWriterSettings();
  69. }
  70. return __defaults;
  71. }
  72. set { __defaults = value; }
  73. }
  74. // public properties
  75. /// <summary>
  76. /// Gets or sets whether to close the output when the writer is closed.
  77. /// </summary>
  78. public bool CloseOutput
  79. {
  80. get { return _closeOutput; }
  81. set
  82. {
  83. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  84. _closeOutput = value;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets or sets the Encoding.
  89. /// </summary>
  90. public UTF8Encoding Encoding
  91. {
  92. get { return _encoding; }
  93. set
  94. {
  95. if (value == null)
  96. {
  97. throw new ArgumentNullException("value");
  98. }
  99. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  100. _encoding = value;
  101. }
  102. }
  103. /// <summary>
  104. /// Gets or sets whether to fix the old binary data subtype on output.
  105. /// </summary>
  106. public bool FixOldBinarySubTypeOnOutput
  107. {
  108. get { return _fixOldBinarySubTypeOnOutput; }
  109. set
  110. {
  111. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  112. _fixOldBinarySubTypeOnOutput = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets the max document size.
  117. /// </summary>
  118. public int MaxDocumentSize
  119. {
  120. get { return _maxDocumentSize; }
  121. set
  122. {
  123. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  124. _maxDocumentSize = value;
  125. }
  126. }
  127. // public methods
  128. /// <summary>
  129. /// Creates a clone of the settings.
  130. /// </summary>
  131. /// <returns>A clone of the settings.</returns>
  132. public new BsonBinaryWriterSettings Clone()
  133. {
  134. return (BsonBinaryWriterSettings)CloneImplementation();
  135. }
  136. // protected methods
  137. /// <summary>
  138. /// Creates a clone of the settings.
  139. /// </summary>
  140. /// <returns>A clone of the settings.</returns>
  141. protected override BsonWriterSettings CloneImplementation()
  142. {
  143. var clone = new BsonBinaryWriterSettings
  144. {
  145. CloseOutput = _closeOutput,
  146. Encoding = _encoding,
  147. FixOldBinarySubTypeOnOutput = _fixOldBinarySubTypeOnOutput,
  148. GuidRepresentation = GuidRepresentation,
  149. MaxDocumentSize = _maxDocumentSize,
  150. MaxSerializationDepth = MaxSerializationDepth
  151. };
  152. return clone;
  153. }
  154. }
  155. }