BsonBinaryWriterSettings.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright 2010-present 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 UTF8Encoding _encoding = Utf8Encodings.Strict;
  29. private bool _fixOldBinarySubTypeOnOutput = true;
  30. private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the BsonBinaryWriterSettings class.
  34. /// </summary>
  35. public BsonBinaryWriterSettings()
  36. {
  37. }
  38. // public static properties
  39. /// <summary>
  40. /// Gets or sets the default BsonBinaryWriter settings.
  41. /// </summary>
  42. public static BsonBinaryWriterSettings Defaults
  43. {
  44. get
  45. {
  46. if (__defaults == null)
  47. {
  48. __defaults = new BsonBinaryWriterSettings();
  49. }
  50. return __defaults;
  51. }
  52. set { __defaults = value; }
  53. }
  54. // public properties
  55. /// <summary>
  56. /// Gets or sets the Encoding.
  57. /// </summary>
  58. public UTF8Encoding Encoding
  59. {
  60. get { return _encoding; }
  61. set
  62. {
  63. if (value == null)
  64. {
  65. throw new ArgumentNullException("value");
  66. }
  67. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  68. _encoding = value;
  69. }
  70. }
  71. /// <summary>
  72. /// Gets or sets whether to fix the old binary data subtype on output.
  73. /// </summary>
  74. public bool FixOldBinarySubTypeOnOutput
  75. {
  76. get { return _fixOldBinarySubTypeOnOutput; }
  77. set
  78. {
  79. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  80. _fixOldBinarySubTypeOnOutput = value;
  81. }
  82. }
  83. /// <summary>
  84. /// Gets or sets the max document size.
  85. /// </summary>
  86. public int MaxDocumentSize
  87. {
  88. get { return _maxDocumentSize; }
  89. set
  90. {
  91. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryWriterSettings is frozen."); }
  92. _maxDocumentSize = value;
  93. }
  94. }
  95. // public methods
  96. /// <summary>
  97. /// Creates a clone of the settings.
  98. /// </summary>
  99. /// <returns>A clone of the settings.</returns>
  100. public new BsonBinaryWriterSettings Clone()
  101. {
  102. return (BsonBinaryWriterSettings)CloneImplementation();
  103. }
  104. // protected methods
  105. /// <summary>
  106. /// Creates a clone of the settings.
  107. /// </summary>
  108. /// <returns>A clone of the settings.</returns>
  109. protected override BsonWriterSettings CloneImplementation()
  110. {
  111. var clone = new BsonBinaryWriterSettings
  112. {
  113. Encoding = _encoding,
  114. FixOldBinarySubTypeOnOutput = _fixOldBinarySubTypeOnOutput,
  115. MaxDocumentSize = _maxDocumentSize,
  116. MaxSerializationDepth = MaxSerializationDepth
  117. };
  118. #pragma warning disable 618
  119. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  120. {
  121. clone.GuidRepresentation = GuidRepresentation;
  122. }
  123. #pragma warning restore 618
  124. return clone;
  125. }
  126. }
  127. }