BsonBinaryReaderSettings.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 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 bool _closeInput = false;
  29. private UTF8Encoding _encoding = new UTF8Encoding(false, true);
  30. private bool _fixOldBinarySubTypeOnInput = true;
  31. private bool _fixOldDateTimeMaxValueOnInput = true;
  32. private int _maxDocumentSize = BsonDefaults.MaxDocumentSize;
  33. // constructors
  34. /// <summary>
  35. /// Initializes a new instance of the BsonBinaryReaderSettings class.
  36. /// </summary>
  37. public BsonBinaryReaderSettings()
  38. {
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the BsonBinaryReaderSettings class.
  42. /// </summary>
  43. /// <param name="closeInput">Whether to close the input stream when the reader is closed.</param>
  44. /// <param name="fixOldBinarySubTypeOnInput">Whether to fix occurrences of the old binary subtype on input.</param>
  45. /// <param name="fixOldDateTimeMaxValueOnInput">Whether to fix occurrences of the old representation of DateTime.MaxValue on input.</param>
  46. /// <param name="guidRepresentation">The representation for Guids.</param>
  47. /// <param name="maxDocumentSize">The max document size.</param>
  48. [Obsolete("Use the no-argument constructor instead and set the properties.")]
  49. public BsonBinaryReaderSettings(
  50. bool closeInput,
  51. bool fixOldBinarySubTypeOnInput,
  52. bool fixOldDateTimeMaxValueOnInput,
  53. GuidRepresentation guidRepresentation,
  54. int maxDocumentSize)
  55. : base(guidRepresentation)
  56. {
  57. _closeInput = closeInput;
  58. _fixOldBinarySubTypeOnInput = fixOldBinarySubTypeOnInput;
  59. _fixOldDateTimeMaxValueOnInput = fixOldDateTimeMaxValueOnInput;
  60. _maxDocumentSize = maxDocumentSize;
  61. }
  62. // public static properties
  63. /// <summary>
  64. /// Gets or sets the default settings for a BsonBinaryReader.
  65. /// </summary>
  66. public static BsonBinaryReaderSettings Defaults
  67. {
  68. get
  69. {
  70. if (__defaults == null)
  71. {
  72. __defaults = new BsonBinaryReaderSettings();
  73. }
  74. return __defaults;
  75. }
  76. set { __defaults = value; }
  77. }
  78. // public properties
  79. /// <summary>
  80. /// Gets or sets whether to close the input stream when the reader is closed.
  81. /// </summary>
  82. public bool CloseInput
  83. {
  84. get { return _closeInput; }
  85. set
  86. {
  87. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  88. _closeInput = value;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the Encoding.
  93. /// </summary>
  94. public UTF8Encoding Encoding
  95. {
  96. get { return _encoding; }
  97. set
  98. {
  99. if (value == null)
  100. {
  101. throw new ArgumentNullException("value");
  102. }
  103. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  104. _encoding = value;
  105. }
  106. }
  107. /// <summary>
  108. /// Gets or sets whether to fix occurrences of the old binary subtype on input.
  109. /// </summary>
  110. public bool FixOldBinarySubTypeOnInput
  111. {
  112. get { return _fixOldBinarySubTypeOnInput; }
  113. set
  114. {
  115. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  116. _fixOldBinarySubTypeOnInput = value;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets whether to fix occurrences of the old representation of DateTime.MaxValue on input.
  121. /// </summary>
  122. public bool FixOldDateTimeMaxValueOnInput
  123. {
  124. get { return _fixOldDateTimeMaxValueOnInput; }
  125. set
  126. {
  127. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  128. _fixOldDateTimeMaxValueOnInput = value;
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the max document size.
  133. /// </summary>
  134. public int MaxDocumentSize
  135. {
  136. get { return _maxDocumentSize; }
  137. set
  138. {
  139. if (IsFrozen) { throw new InvalidOperationException("BsonBinaryReaderSettings is frozen."); }
  140. _maxDocumentSize = value;
  141. }
  142. }
  143. // public methods
  144. /// <summary>
  145. /// Creates a clone of the settings.
  146. /// </summary>
  147. /// <returns>A clone of the settings.</returns>
  148. public new BsonBinaryReaderSettings Clone()
  149. {
  150. return (BsonBinaryReaderSettings)CloneImplementation();
  151. }
  152. // protected methods
  153. /// <summary>
  154. /// Creates a clone of the settings.
  155. /// </summary>
  156. /// <returns>A clone of the settings.</returns>
  157. protected override BsonReaderSettings CloneImplementation()
  158. {
  159. var clone = new BsonBinaryReaderSettings
  160. {
  161. CloseInput = _closeInput,
  162. Encoding = _encoding,
  163. FixOldBinarySubTypeOnInput = _fixOldBinarySubTypeOnInput,
  164. FixOldDateTimeMaxValueOnInput = _fixOldDateTimeMaxValueOnInput,
  165. GuidRepresentation = GuidRepresentation,
  166. MaxDocumentSize = _maxDocumentSize
  167. };
  168. return clone;
  169. }
  170. }
  171. }