BsonBinaryWriterSettings.cs 4.2 KB

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