BsonBinaryData.cs 13 KB

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