PageRenderTime 54ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/protobuf-2.5.0/csharp/src/Google.Protobuf/ByteString.cs

https://gitlab.com/f1ssi0n/sniffles
C# | 345 lines | 156 code | 31 blank | 158 comment | 13 complexity | 16a85c9dd67396a832dcb221ada11520 MD5 | raw file
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System;
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Text;
  37. namespace Google.Protobuf
  38. {
  39. /// <summary>
  40. /// Immutable array of bytes.
  41. /// </summary>
  42. public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString>
  43. {
  44. private static readonly ByteString empty = new ByteString(new byte[0]);
  45. private readonly byte[] bytes;
  46. /// <summary>
  47. /// Unsafe operations that can cause IO Failure and/or other catestrophic side-effects.
  48. /// </summary>
  49. internal static class Unsafe
  50. {
  51. /// <summary>
  52. /// Constructs a new ByteString from the given byte array. The array is
  53. /// *not* copied, and must not be modified after this constructor is called.
  54. /// </summary>
  55. internal static ByteString FromBytes(byte[] bytes)
  56. {
  57. return new ByteString(bytes);
  58. }
  59. /// <summary>
  60. /// Provides direct, unrestricted access to the bytes contained in this instance.
  61. /// You must not modify or resize the byte array returned by this method.
  62. /// </summary>
  63. internal static byte[] GetBuffer(ByteString bytes)
  64. {
  65. return bytes.bytes;
  66. }
  67. }
  68. /// <summary>
  69. /// Internal use only. Ensure that the provided array is not mutated and belongs to this instance.
  70. /// </summary>
  71. internal static ByteString AttachBytes(byte[] bytes)
  72. {
  73. return new ByteString(bytes);
  74. }
  75. /// <summary>
  76. /// Constructs a new ByteString from the given byte array. The array is
  77. /// *not* copied, and must not be modified after this constructor is called.
  78. /// </summary>
  79. private ByteString(byte[] bytes)
  80. {
  81. this.bytes = bytes;
  82. }
  83. /// <summary>
  84. /// Returns an empty ByteString.
  85. /// </summary>
  86. public static ByteString Empty
  87. {
  88. get { return empty; }
  89. }
  90. /// <summary>
  91. /// Returns the length of this ByteString in bytes.
  92. /// </summary>
  93. public int Length
  94. {
  95. get { return bytes.Length; }
  96. }
  97. /// <summary>
  98. /// Returns <c>true</c> if this byte string is empty, <c>false</c> otherwise.
  99. /// </summary>
  100. public bool IsEmpty
  101. {
  102. get { return Length == 0; }
  103. }
  104. /// <summary>
  105. /// Converts this <see cref="ByteString"/> into a byte array.
  106. /// </summary>
  107. /// <remarks>The data is copied - changes to the returned array will not be reflected in this <c>ByteString</c>.</remarks>
  108. /// <returns>A byte array with the same data as this <c>ByteString</c>.</returns>
  109. public byte[] ToByteArray()
  110. {
  111. return (byte[]) bytes.Clone();
  112. }
  113. /// <summary>
  114. /// Converts this <see cref="ByteString"/> into a standard base64 representation.
  115. /// </summary>
  116. /// <returns>A base64 representation of this <c>ByteString</c>.</returns>
  117. public string ToBase64()
  118. {
  119. return Convert.ToBase64String(bytes);
  120. }
  121. /// <summary>
  122. /// Constructs a <see cref="ByteString" /> from the Base64 Encoded String.
  123. /// </summary>
  124. public static ByteString FromBase64(string bytes)
  125. {
  126. // By handling the empty string explicitly, we not only optimize but we fix a
  127. // problem on CF 2.0. See issue 61 for details.
  128. return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes));
  129. }
  130. /// <summary>
  131. /// Constructs a <see cref="ByteString" /> from the given array. The contents
  132. /// are copied, so further modifications to the array will not
  133. /// be reflected in the returned ByteString.
  134. /// This method can also be invoked in <c>ByteString.CopyFrom(0xaa, 0xbb, ...)</c> form
  135. /// which is primarily useful for testing.
  136. /// </summary>
  137. public static ByteString CopyFrom(params byte[] bytes)
  138. {
  139. return new ByteString((byte[]) bytes.Clone());
  140. }
  141. /// <summary>
  142. /// Constructs a <see cref="ByteString" /> from a portion of a byte array.
  143. /// </summary>
  144. public static ByteString CopyFrom(byte[] bytes, int offset, int count)
  145. {
  146. byte[] portion = new byte[count];
  147. ByteArray.Copy(bytes, offset, portion, 0, count);
  148. return new ByteString(portion);
  149. }
  150. /// <summary>
  151. /// Creates a new <see cref="ByteString" /> by encoding the specified text with
  152. /// the given encoding.
  153. /// </summary>
  154. public static ByteString CopyFrom(string text, Encoding encoding)
  155. {
  156. return new ByteString(encoding.GetBytes(text));
  157. }
  158. /// <summary>
  159. /// Creates a new <see cref="ByteString" /> by encoding the specified text in UTF-8.
  160. /// </summary>
  161. public static ByteString CopyFromUtf8(string text)
  162. {
  163. return CopyFrom(text, Encoding.UTF8);
  164. }
  165. /// <summary>
  166. /// Retuns the byte at the given index.
  167. /// </summary>
  168. public byte this[int index]
  169. {
  170. get { return bytes[index]; }
  171. }
  172. /// <summary>
  173. /// Converts this <see cref="ByteString"/> into a string by applying the given encoding.
  174. /// </summary>
  175. /// <remarks>
  176. /// This method should only be used to convert binary data which was the result of encoding
  177. /// text with the given encoding.
  178. /// </remarks>
  179. /// <param name="encoding">The encoding to use to decode the binary data into text.</param>
  180. /// <returns>The result of decoding the binary data with the given decoding.</returns>
  181. public string ToString(Encoding encoding)
  182. {
  183. return encoding.GetString(bytes, 0, bytes.Length);
  184. }
  185. /// <summary>
  186. /// Converts this <see cref="ByteString"/> into a string by applying the UTF-8 encoding.
  187. /// </summary>
  188. /// <remarks>
  189. /// This method should only be used to convert binary data which was the result of encoding
  190. /// text with UTF-8.
  191. /// </remarks>
  192. /// <returns>The result of decoding the binary data with the given decoding.</returns>
  193. public string ToStringUtf8()
  194. {
  195. return ToString(Encoding.UTF8);
  196. }
  197. /// <summary>
  198. /// Returns an iterator over the bytes in this <see cref="ByteString"/>.
  199. /// </summary>
  200. /// <returns>An iterator over the bytes in this object.</returns>
  201. public IEnumerator<byte> GetEnumerator()
  202. {
  203. return ((IEnumerable<byte>) bytes).GetEnumerator();
  204. }
  205. /// <summary>
  206. /// Returns an iterator over the bytes in this <see cref="ByteString"/>.
  207. /// </summary>
  208. /// <returns>An iterator over the bytes in this object.</returns>
  209. IEnumerator IEnumerable.GetEnumerator()
  210. {
  211. return GetEnumerator();
  212. }
  213. /// <summary>
  214. /// Creates a CodedInputStream from this ByteString's data.
  215. /// </summary>
  216. public CodedInputStream CreateCodedInput()
  217. {
  218. // We trust CodedInputStream not to reveal the provided byte array or modify it
  219. return new CodedInputStream(bytes);
  220. }
  221. /// <summary>
  222. /// Compares two byte strings for equality.
  223. /// </summary>
  224. /// <param name="lhs">The first byte string to compare.</param>
  225. /// <param name="rhs">The second byte string to compare.</param>
  226. /// <returns><c>true</c> if the byte strings are equal; false otherwise.</returns>
  227. public static bool operator ==(ByteString lhs, ByteString rhs)
  228. {
  229. if (ReferenceEquals(lhs, rhs))
  230. {
  231. return true;
  232. }
  233. if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
  234. {
  235. return false;
  236. }
  237. if (lhs.bytes.Length != rhs.bytes.Length)
  238. {
  239. return false;
  240. }
  241. for (int i = 0; i < lhs.Length; i++)
  242. {
  243. if (rhs.bytes[i] != lhs.bytes[i])
  244. {
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. /// <summary>
  251. /// Compares two byte strings for inequality.
  252. /// </summary>
  253. /// <param name="lhs">The first byte string to compare.</param>
  254. /// <param name="rhs">The second byte string to compare.</param>
  255. /// <returns><c>false</c> if the byte strings are equal; true otherwise.</returns>
  256. public static bool operator !=(ByteString lhs, ByteString rhs)
  257. {
  258. return !(lhs == rhs);
  259. }
  260. /// <summary>
  261. /// Compares this byte string with another object.
  262. /// </summary>
  263. /// <param name="obj">The object to compare this with.</param>
  264. /// <returns><c>true</c> if <paramref name="obj"/> refers to an equal <see cref="ByteString"/>; <c>false</c> otherwise.</returns>
  265. public override bool Equals(object obj)
  266. {
  267. return this == (obj as ByteString);
  268. }
  269. /// <summary>
  270. /// Returns a hash code for this object. Two equal byte strings
  271. /// will return the same hash code.
  272. /// </summary>
  273. /// <returns>A hash code for this object.</returns>
  274. public override int GetHashCode()
  275. {
  276. int ret = 23;
  277. foreach (byte b in bytes)
  278. {
  279. ret = (ret << 8) | b;
  280. }
  281. return ret;
  282. }
  283. /// <summary>
  284. /// Compares this byte string with another.
  285. /// </summary>
  286. /// <param name="other">The <see cref="ByteString"/> to compare this with.</param>
  287. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal byte string; <c>false</c> otherwise.</returns>
  288. public bool Equals(ByteString other)
  289. {
  290. return this == other;
  291. }
  292. /// <summary>
  293. /// Used internally by CodedOutputStream to avoid creating a copy for the write
  294. /// </summary>
  295. internal void WriteRawBytesTo(CodedOutputStream outputStream)
  296. {
  297. outputStream.WriteRawBytes(bytes, 0, bytes.Length);
  298. }
  299. /// <summary>
  300. /// Copies the entire byte array to the destination array provided at the offset specified.
  301. /// </summary>
  302. public void CopyTo(byte[] array, int position)
  303. {
  304. ByteArray.Copy(bytes, 0, array, position, bytes.Length);
  305. }
  306. /// <summary>
  307. /// Writes the entire byte array to the provided stream
  308. /// </summary>
  309. public void WriteTo(Stream outputStream)
  310. {
  311. outputStream.Write(bytes, 0, bytes.Length);
  312. }
  313. }
  314. }