|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Runtime.Serialization; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace SubSonic.Data.Linq |
| 8 | +{ |
| 9 | + [SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification = "Microsoft: The name clearly describes function and the namespace is under a DLinq namespace which will make the distinction clear.")] |
| 10 | + [DataContract] |
| 11 | + [Serializable] |
| 12 | + public sealed class Binary : IEquatable<Binary> |
| 13 | + { |
| 14 | + [DataMember(Name = "Bytes")] |
| 15 | + readonly byte[] bytes; |
| 16 | + int? hashCode; |
| 17 | + |
| 18 | + public Binary(byte[] value) |
| 19 | + { |
| 20 | + if (value == null) |
| 21 | + { |
| 22 | + this.bytes = new byte[0]; |
| 23 | + } |
| 24 | + else |
| 25 | + { |
| 26 | + this.bytes = new byte[value.Length]; |
| 27 | + Array.Copy(value, this.bytes, value.Length); |
| 28 | + } |
| 29 | + this.ComputeHash(); |
| 30 | + } |
| 31 | + |
| 32 | + public byte[] ToArray() |
| 33 | + { |
| 34 | + byte[] copy = new byte[this.bytes.Length]; |
| 35 | + Array.Copy(this.bytes, copy, copy.Length); |
| 36 | + return copy; |
| 37 | + } |
| 38 | + |
| 39 | + public int Length |
| 40 | + { |
| 41 | + get { return this.bytes.Length; } |
| 42 | + } |
| 43 | + |
| 44 | + public static implicit operator Binary(byte[] value) |
| 45 | + { |
| 46 | + return new Binary(value); |
| 47 | + } |
| 48 | + |
| 49 | + public bool Equals(Binary other) |
| 50 | + { |
| 51 | + return this.EqualsTo(other); |
| 52 | + } |
| 53 | + |
| 54 | + public static bool operator ==(Binary binary1, Binary binary2) |
| 55 | + { |
| 56 | + if ((object)binary1 == (object)binary2) |
| 57 | + return true; |
| 58 | + if (binary1 is null && binary2 is null) |
| 59 | + return true; |
| 60 | + if (binary1 is null || binary2 is null) |
| 61 | + return false; |
| 62 | + return binary1.EqualsTo(binary2); |
| 63 | + } |
| 64 | + |
| 65 | + public static bool operator !=(Binary binary1, Binary binary2) |
| 66 | + { |
| 67 | + if (binary1 == (object)binary2) |
| 68 | + return false; |
| 69 | + if (binary1 is null && binary2 is null) |
| 70 | + return false; |
| 71 | + if (binary1 is null || binary2 is null) |
| 72 | + return true; |
| 73 | + return !binary1.EqualsTo(binary2); |
| 74 | + } |
| 75 | + |
| 76 | + public override bool Equals(object obj) |
| 77 | + { |
| 78 | + return EqualsTo(obj as Binary); |
| 79 | + } |
| 80 | + |
| 81 | + public override int GetHashCode() |
| 82 | + { |
| 83 | + if (!hashCode.HasValue) |
| 84 | + { |
| 85 | + // hash code is not marked [DataMember], so when |
| 86 | + // using the DataContractSerializer, we'll need |
| 87 | + // to recompute the hash after deserialization. |
| 88 | + ComputeHash(); |
| 89 | + } |
| 90 | + return this.hashCode.Value; |
| 91 | + } |
| 92 | + |
| 93 | + public override string ToString() |
| 94 | + { |
| 95 | + StringBuilder sb = new StringBuilder(); |
| 96 | + sb.Append("\""); |
| 97 | + sb.Append(Convert.ToBase64String(bytes, 0, bytes.Length)); |
| 98 | + sb.Append("\""); |
| 99 | + return sb.ToString(); |
| 100 | + } |
| 101 | + |
| 102 | + private bool EqualsTo(Binary binary) |
| 103 | + { |
| 104 | + if ((this) == (object)binary) |
| 105 | + return true; |
| 106 | + if (binary is null) |
| 107 | + return false; |
| 108 | + if (bytes.Length != binary.bytes.Length) |
| 109 | + return false; |
| 110 | + if (GetHashCode() != binary.GetHashCode()) |
| 111 | + return false; |
| 112 | + for (int i = 0, n = bytes.Length; i < n; i++) |
| 113 | + { |
| 114 | + if (bytes[i] != binary.bytes[i]) |
| 115 | + return false; |
| 116 | + } |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Simple hash using pseudo-random coefficients for each byte in |
| 122 | + /// the array to achieve order dependency. |
| 123 | + /// </summary> |
| 124 | + private void ComputeHash() |
| 125 | + { |
| 126 | + int s = 314, t = 159; |
| 127 | + hashCode = 0; |
| 128 | + for (int i = 0; i < bytes.Length; i++) |
| 129 | + { |
| 130 | + hashCode = hashCode * s + bytes[i]; |
| 131 | + s *= t; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments