BsonBinaryReaderSettings.cs 4.7 KB

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