BsonBinaryReaderSettings.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 BsonBinaryReader.
  21. /// </summary>
  22. [Serializable]
  23. public class BsonBinaryReaderSettings : BsonReaderSettings
  24. {
  25. // private static fields
  26. private static BsonBinaryReaderSettings __defaults = null; // delay creation to pick up the latest default values
  27. // private fields
  28. private UTF8Encoding _encoding = Utf8Encodings.Strict;
  29. private bool _fixOldBinarySubTypeOnInput = true;
  30. private bool _fixOldDateTimeMaxValueOnInput = true;
  31. private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
  32. // constructors
  33. /// <summary>
  34. /// Initializes a new instance of the BsonBinaryReaderSettings class.
  35. /// </summary>
  36. public BsonBinaryReaderSettings()
  37. {
  38. }
  39. // public static properties
  40. /// <summary>
  41. /// Gets or sets the default settings for a BsonBinaryReader.
  42. /// </summary>
  43. public static BsonBinaryReaderSettings Defaults
  44. {
  45. get
  46. {
  47. if (__defaults == null)
  48. {
  49. __defaults = new BsonBinaryReaderSettings();
  50. }
  51. return __defaults;
  52. }
  53. set { __defaults = value; }
  54. }
  55. // public properties
  56. /// <summary>
  57. /// Gets or sets the Encoding.
  58. /// </summary>
  59. public UTF8Encoding Encoding
  60. {
  61. get { return _encoding; }
  62. set
  63. {
  64. if (value == null)
  65. {
  66. throw new ArgumentNullException("value");
  67. }
  68. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  69. _encoding = value;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets or sets whether to fix occurrences of the old binary subtype on input.
  74. /// </summary>
  75. public bool FixOldBinarySubTypeOnInput
  76. {
  77. get { return _fixOldBinarySubTypeOnInput; }
  78. set
  79. {
  80. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  81. _fixOldBinarySubTypeOnInput = value;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets or sets whether to fix occurrences of the old representation of DateTime.MaxValue on input.
  86. /// </summary>
  87. public bool FixOldDateTimeMaxValueOnInput
  88. {
  89. get { return _fixOldDateTimeMaxValueOnInput; }
  90. set
  91. {
  92. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  93. _fixOldDateTimeMaxValueOnInput = value;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the max document size.
  98. /// </summary>
  99. public int MaxDocumentSize
  100. {
  101. get { return _maxDocumentSize; }
  102. set
  103. {
  104. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  105. _maxDocumentSize = value;
  106. }
  107. }
  108. // public methods
  109. /// <summary>
  110. /// Creates a clone of the settings.
  111. /// </summary>
  112. /// <returns>A clone of the settings.</returns>
  113. public new BsonBinaryReaderSettings Clone()
  114. {
  115. return (BsonBinaryReaderSettings)CloneImplementation();
  116. }
  117. // protected methods
  118. /// <summary>
  119. /// Creates a clone of the settings.
  120. /// </summary>
  121. /// <returns>A clone of the settings.</returns>
  122. protected override BsonReaderSettings CloneImplementation()
  123. {
  124. var clone = new BsonBinaryReaderSettings
  125. {
  126. Encoding = _encoding,
  127. FixOldBinarySubTypeOnInput = _fixOldBinarySubTypeOnInput,
  128. FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
  129. MaxDocumentSize = _maxDocumentSize
  130. };
  131. #pragma warning disable 618
  132. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  133. {
  134. clone.GuidRepresentation = GuidRepresentation;
  135. }
  136. #pragma warning restore 618
  137. return clone;
  138. }
  139. }
  140. }