BsonBinaryData.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.Linq;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents BSON binary data.
  21. /// </summary>
  22. #if NET452
  23. [Serializable]
  24. #endif
  25. public class BsonBinaryData : BsonValue, IComparable<BsonBinaryData>, IEquatable<BsonBinaryData>
  26. {
  27. // private fields
  28. private readonly byte[] _bytes;
  29. private readonly BsonBinarySubType _subType;
  30. private readonly GuidRepresentation _guidRepresentation; // only relevant if subType is UuidStandard or UuidLegacy
  31. // constructors
  32. /// <summary>
  33. /// Initializes a new instance of the BsonBinaryData class.
  34. /// </summary>
  35. /// <param name="bytes">The binary data.</param>
  36. public BsonBinaryData(byte[] bytes)
  37. : this(bytes, BsonBinarySubType.Binary)
  38. {
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the BsonBinaryData class.
  42. /// </summary>
  43. /// <param name="bytes">The binary data.</param>
  44. /// <param name="subType">The binary data subtype.</param>
  45. public BsonBinaryData(byte[] bytes, BsonBinarySubType subType)
  46. : this(bytes, subType, subType == BsonBinarySubType.UuidStandard ? GuidRepresentation.Standard : GuidRepresentation.Unspecified)
  47. {
  48. }
  49. /// <summary>
  50. /// Initializes a new instance of the BsonBinaryData class.
  51. /// </summary>
  52. /// <param name="bytes">The binary data.</param>
  53. /// <param name="subType">The binary data subtype.</param>
  54. /// <param name="guidRepresentation">The representation for Guids.</param>
  55. public BsonBinaryData(byte[] bytes, BsonBinarySubType subType, GuidRepresentation guidRepresentation)
  56. {
  57. if (bytes == null)
  58. {
  59. throw new ArgumentNullException(nameof(bytes));
  60. }
  61. if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
  62. {
  63. if (bytes.Length != 16)
  64. {
  65. var message = string.Format(
  66. "Length must be 16, not {0}, when subType is {1}.",
  67. bytes.Length, subType);
  68. throw new ArgumentException(message, nameof(bytes));
  69. }
  70. BsonBinarySubType expectedSubType;
  71. switch (guidRepresentation)
  72. {
  73. case GuidRepresentation.CSharpLegacy:
  74. case GuidRepresentation.JavaLegacy:
  75. case GuidRepresentation.PythonLegacy:
  76. case GuidRepresentation.Unspecified:
  77. expectedSubType = BsonBinarySubType.UuidLegacy;
  78. break;
  79. case GuidRepresentation.Standard:
  80. expectedSubType = BsonBinarySubType.UuidStandard;
  81. break;
  82. default:
  83. throw new ArgumentException($"Invalid guidRepresentation: {guidRepresentation}.", nameof(guidRepresentation));
  84. }
  85. if (subType != expectedSubType)
  86. {
  87. throw new ArgumentException($"GuidRepresentation {guidRepresentation} is only valid with subType {expectedSubType}, not with subType {subType}.", nameof(guidRepresentation));
  88. }
  89. }
  90. else
  91. {
  92. if (guidRepresentation != GuidRepresentation.Unspecified)
  93. {
  94. var message = string.Format(
  95. "GuidRepresentation must be Unspecified, not {0}, when subType is not UuidStandard or UuidLegacy.",
  96. guidRepresentation);
  97. throw new ArgumentException(message, nameof(guidRepresentation));
  98. }
  99. }
  100. _bytes = bytes;
  101. _subType = subType;
  102. _guidRepresentation = guidRepresentation;
  103. }
  104. /// <summary>
  105. /// Initializes a new instance of the BsonBinaryData class.
  106. /// </summary>
  107. /// <param name="guid">A Guid.</param>
  108. public BsonBinaryData(Guid guid)
  109. : this(guid, BsonDefaults.GuidRepresentation)
  110. {
  111. }
  112. /// <summary>
  113. /// Initializes a new instance of the BsonBinaryData class.
  114. /// </summary>
  115. /// <param name="guid">A Guid.</param>
  116. /// <param name="guidRepresentation">The representation for Guids.</param>
  117. public BsonBinaryData(Guid guid, GuidRepresentation guidRepresentation)
  118. : this(GuidConverter.ToBytes(guid, guidRepresentation), (guidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy, guidRepresentation)
  119. {
  120. }
  121. // public properties
  122. /// <summary>
  123. /// Gets the BsonType of this BsonValue.
  124. /// </summary>
  125. public override BsonType BsonType
  126. {
  127. get { return BsonType.Binary; }
  128. }
  129. /// <summary>
  130. /// Gets the binary data.
  131. /// </summary>
  132. public byte[] Bytes
  133. {
  134. get { return _bytes; }
  135. }
  136. /// <summary>
  137. /// Gets the representation to use when representing the Guid as BSON binary data.
  138. /// </summary>
  139. public GuidRepresentation GuidRepresentation
  140. {
  141. get { return _guidRepresentation; }
  142. }
  143. /// <summary>
  144. /// Gets the BsonBinaryData as a Guid if the subtype is UuidStandard or UuidLegacy, otherwise null.
  145. /// </summary>
  146. #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
  147. [Obsolete("Use Value instead.")]
  148. public override object RawValue
  149. {
  150. get
  151. {
  152. if (_subType == BsonBinarySubType.Binary || _subType == BsonBinarySubType.OldBinary)
  153. {
  154. return _bytes;
  155. }
  156. else if (_subType == BsonBinarySubType.UuidStandard || _subType == BsonBinarySubType.UuidLegacy)
  157. {
  158. return ToGuid();
  159. }
  160. else
  161. {
  162. return null;
  163. }
  164. }
  165. }
  166. #pragma warning restore 618
  167. /// <summary>
  168. /// Gets the binary data subtype.
  169. /// </summary>
  170. public BsonBinarySubType SubType
  171. {
  172. get { return _subType; }
  173. }
  174. // public operators
  175. /// <summary>
  176. /// Converts a byte array to a BsonBinaryData.
  177. /// </summary>
  178. /// <param name="bytes">A byte array.</param>
  179. /// <returns>A BsonBinaryData.</returns>
  180. public static implicit operator BsonBinaryData(byte[] bytes)
  181. {
  182. return new BsonBinaryData(bytes);
  183. }
  184. /// <summary>
  185. /// Converts a Guid to a BsonBinaryData.
  186. /// </summary>
  187. /// <param name="value">A Guid.</param>
  188. /// <returns>A BsonBinaryData.</returns>
  189. public static implicit operator BsonBinaryData(Guid value)
  190. {
  191. return new BsonBinaryData(value);
  192. }
  193. /// <summary>
  194. /// Compares two BsonBinaryData values.
  195. /// </summary>
  196. /// <param name="lhs">The first BsonBinaryData.</param>
  197. /// <param name="rhs">The other BsonBinaryData.</param>
  198. /// <returns>True if the two BsonBinaryData values are not equal according to ==.</returns>
  199. public static bool operator !=(BsonBinaryData lhs, BsonBinaryData rhs)
  200. {
  201. return !(lhs == rhs);
  202. }
  203. /// <summary>
  204. /// Compares two BsonBinaryData values.
  205. /// </summary>
  206. /// <param name="lhs">The first BsonBinaryData.</param>
  207. /// <param name="rhs">The other BsonBinaryData.</param>
  208. /// <returns>True if the two BsonBinaryData values are equal according to ==.</returns>
  209. public static bool operator ==(BsonBinaryData lhs, BsonBinaryData rhs)
  210. {
  211. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  212. return lhs.Equals(rhs);
  213. }
  214. // public static methods
  215. /// <summary>
  216. /// Creates a new BsonBinaryData.
  217. /// </summary>
  218. /// <param name="value">An object to be mapped to a BsonBinaryData.</param>
  219. /// <returns>A BsonBinaryData or null.</returns>
  220. public new static BsonBinaryData Create(object value)
  221. {
  222. if (value == null)
  223. {
  224. throw new ArgumentNullException("value");
  225. }
  226. return (BsonBinaryData)BsonTypeMapper.MapToBsonValue(value, BsonType.Binary);
  227. }
  228. // public methods
  229. /// <summary>
  230. /// Compares this BsonBinaryData to another BsonBinaryData.
  231. /// </summary>
  232. /// <param name="other">The other BsonBinaryData.</param>
  233. /// <returns>A 32-bit signed integer that indicates whether this BsonBinaryData is less than, equal to, or greather than the other.</returns>
  234. public int CompareTo(BsonBinaryData other)
  235. {
  236. if (other == null) { return 1; }
  237. int r = _subType.CompareTo(other._subType);
  238. if (r != 0) { return r; }
  239. for (int i = 0; i < _bytes.Length && i < other._bytes.Length; i++)
  240. {
  241. r = _bytes[i].CompareTo(other._bytes[i]);
  242. if (r != 0) { return r; }
  243. }
  244. return _bytes.Length.CompareTo(other._bytes.Length);
  245. }
  246. /// <summary>
  247. /// Compares the BsonBinaryData to another BsonValue.
  248. /// </summary>
  249. /// <param name="other">The other BsonValue.</param>
  250. /// <returns>A 32-bit signed integer that indicates whether this BsonBinaryData is less than, equal to, or greather than the other BsonValue.</returns>
  251. public override int CompareTo(BsonValue other)
  252. {
  253. if (other == null) { return 1; }
  254. var otherBinaryData = other as BsonBinaryData;
  255. if (otherBinaryData != null)
  256. {
  257. return CompareTo(otherBinaryData);
  258. }
  259. return CompareTypeTo(other);
  260. }
  261. /// <summary>
  262. /// Compares this BsonBinaryData to another BsonBinaryData.
  263. /// </summary>
  264. /// <param name="rhs">The other BsonBinaryData.</param>
  265. /// <returns>True if the two BsonBinaryData values are equal.</returns>
  266. public bool Equals(BsonBinaryData rhs)
  267. {
  268. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  269. // note: guidRepresentation is not considered when testing for Equality
  270. return object.ReferenceEquals(this, rhs) || _subType == rhs._subType && _bytes.SequenceEqual(rhs._bytes);
  271. }
  272. /// <summary>
  273. /// Compares this BsonBinaryData to another object.
  274. /// </summary>
  275. /// <param name="obj">The other object.</param>
  276. /// <returns>True if the other object is a BsonBinaryData and equal to this one.</returns>
  277. public override bool Equals(object obj)
  278. {
  279. return Equals(obj as BsonBinaryData); // works even if obj is null or of a different type
  280. }
  281. /// <summary>
  282. /// Gets the hash code.
  283. /// </summary>
  284. /// <returns>The hash code.</returns>
  285. public override int GetHashCode()
  286. {
  287. // see Effective Java by Joshua Bloch
  288. // note: guidRepresentation is not considered when computing the hash code
  289. int hash = 17;
  290. hash = 37 * hash + BsonType.GetHashCode();
  291. foreach (byte b in _bytes)
  292. {
  293. hash = 37 * hash + b;
  294. }
  295. hash = 37 * hash + _subType.GetHashCode();
  296. return hash;
  297. }
  298. /// <summary>
  299. /// Converts this BsonBinaryData to a Guid.
  300. /// </summary>
  301. /// <returns>A Guid.</returns>
  302. public Guid ToGuid()
  303. {
  304. return ToGuid(_guidRepresentation);
  305. }
  306. /// <summary>
  307. /// Converts this BsonBinaryData to a Guid.
  308. /// </summary>
  309. /// <param name="guidRepresentation">The representation for Guids.</param>
  310. /// <returns>A Guid.</returns>
  311. public Guid ToGuid(GuidRepresentation guidRepresentation)
  312. {
  313. if (_subType != BsonBinarySubType.UuidStandard && _subType != BsonBinarySubType.UuidLegacy)
  314. {
  315. var message = string.Format("SubType must be UuidStandard or UuidLegacy, not {0}.", _subType);
  316. throw new InvalidOperationException(message);
  317. }
  318. if (guidRepresentation == GuidRepresentation.Unspecified)
  319. {
  320. throw new ArgumentException("GuidRepresentation cannot be Unspecified.");
  321. }
  322. return GuidConverter.FromBytes(_bytes, guidRepresentation);
  323. }
  324. /// <summary>
  325. /// Returns a string representation of the binary data.
  326. /// </summary>
  327. /// <returns>A string representation of the binary data.</returns>
  328. public override string ToString()
  329. {
  330. return string.Format("{0}:0x{1}", _subType, BsonUtils.ToHexString(_bytes));
  331. }
  332. }
  333. }