ByteString.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #if !NET35
  38. using System.Threading;
  39. using System.Threading.Tasks;
  40. #endif
  41. #if NET35
  42. using Google.Protobuf.Compatibility;
  43. #endif
  44. namespace Google.Protobuf
  45. {
  46. /// <summary>
  47. /// Immutable array of bytes.
  48. /// </summary>
  49. public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString>
  50. {
  51. private static readonly ByteString empty = new ByteString(new byte[0]);
  52. private readonly byte[] bytes;
  53. /// <summary>
  54. /// Unsafe operations that can cause IO Failure and/or other catestrophic side-effects.
  55. /// </summary>
  56. internal static class Unsafe
  57. {
  58. /// <summary>
  59. /// Constructs a new ByteString from the given byte array. The array is
  60. /// *not* copied, and must not be modified after this constructor is called.
  61. /// </summary>
  62. internal static ByteString FromBytes(byte[] bytes)
  63. {
  64. return new ByteString(bytes);
  65. }
  66. }
  67. /// <summary>
  68. /// Internal use only. Ensure that the provided array is not mutated and belongs to this instance.
  69. /// </summary>
  70. internal static ByteString AttachBytes(byte[] bytes)
  71. {
  72. return new ByteString(bytes);
  73. }
  74. /// <summary>
  75. /// Constructs a new ByteString from the given byte array. The array is
  76. /// *not* copied, and must not be modified after this constructor is called.
  77. /// </summary>
  78. private ByteString(byte[] bytes)
  79. {
  80. this.bytes = bytes;
  81. }
  82. /// <summary>
  83. /// Returns an empty ByteString.
  84. /// </summary>
  85. public static ByteString Empty
  86. {
  87. get { return empty; }
  88. }
  89. /// <summary>
  90. /// Returns the length of this ByteString in bytes.
  91. /// </summary>
  92. public int Length
  93. {
  94. get { return bytes.Length; }
  95. }
  96. /// <summary>
  97. /// Returns <c>true</c> if this byte string is empty, <c>false</c> otherwise.
  98. /// </summary>
  99. public bool IsEmpty
  100. {
  101. get { return Length == 0; }
  102. }
  103. #if NETSTANDARD2_0
  104. /// <summary>
  105. /// Provides read-only access to the data of this <see cref="ByteString"/>.
  106. /// No data is copied so this is the most efficient way of accessing.
  107. /// </summary>
  108. public ReadOnlySpan<byte> Span => new ReadOnlySpan<byte>(bytes);
  109. #endif
  110. /// <summary>
  111. /// Converts this <see cref="ByteString"/> into a byte array.
  112. /// </summary>
  113. /// <remarks>The data is copied - changes to the returned array will not be reflected in this <c>ByteString</c>.</remarks>
  114. /// <returns>A byte array with the same data as this <c>ByteString</c>.</returns>
  115. public byte[] ToByteArray()
  116. {
  117. return (byte[]) bytes.Clone();
  118. }
  119. /// <summary>
  120. /// Converts this <see cref="ByteString"/> into a standard base64 representation.
  121. /// </summary>
  122. /// <returns>A base64 representation of this <c>ByteString</c>.</returns>
  123. public string ToBase64()
  124. {
  125. return Convert.ToBase64String(bytes);
  126. }
  127. /// <summary>
  128. /// Constructs a <see cref="ByteString" /> from the Base64 Encoded String.
  129. /// </summary>
  130. public static ByteString FromBase64(string bytes)
  131. {
  132. // By handling the empty string explicitly, we not only optimize but we fix a
  133. // problem on CF 2.0. See issue 61 for details.
  134. return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes));
  135. }
  136. /// <summary>
  137. /// Constructs a <see cref="ByteString"/> from data in the given stream, synchronously.
  138. /// </summary>
  139. /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
  140. /// at the start of the call.</remarks>
  141. /// <param name="stream">The stream to copy into a ByteString.</param>
  142. /// <returns>A ByteString with content read from the given stream.</returns>
  143. public static ByteString FromStream(Stream stream)
  144. {
  145. ProtoPreconditions.CheckNotNull(stream, nameof(stream));
  146. int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0;
  147. var memoryStream = new MemoryStream(capacity);
  148. stream.CopyTo(memoryStream);
  149. #if NETSTANDARD1_0 || NETSTANDARD2_0
  150. byte[] bytes = memoryStream.ToArray();
  151. #else
  152. // Avoid an extra copy if we can.
  153. byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
  154. #endif
  155. return AttachBytes(bytes);
  156. }
  157. #if !NET35
  158. /// <summary>
  159. /// Constructs a <see cref="ByteString"/> from data in the given stream, asynchronously.
  160. /// </summary>
  161. /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
  162. /// at the start of the call.</remarks>
  163. /// <param name="stream">The stream to copy into a ByteString.</param>
  164. /// <param name="cancellationToken">The cancellation token to use when reading from the stream, if any.</param>
  165. /// <returns>A ByteString with content read from the given stream.</returns>
  166. public async static Task<ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
  167. {
  168. ProtoPreconditions.CheckNotNull(stream, nameof(stream));
  169. int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0;
  170. var memoryStream = new MemoryStream(capacity);
  171. // We have to specify the buffer size here, as there's no overload accepting the cancellation token
  172. // alone. But it's documented to use 81920 by default if not specified.
  173. await stream.CopyToAsync(memoryStream, 81920, cancellationToken);
  174. #if NETSTANDARD1_0 || NETSTANDARD2_0
  175. byte[] bytes = memoryStream.ToArray();
  176. #else
  177. // Avoid an extra copy if we can.
  178. byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
  179. #endif
  180. return AttachBytes(bytes);
  181. }
  182. #endif
  183. /// <summary>
  184. /// Constructs a <see cref="ByteString" /> from the given array. The contents
  185. /// are copied, so further modifications to the array will not
  186. /// be reflected in the returned ByteString.
  187. /// This method can also be invoked in <c>ByteString.CopyFrom(0xaa, 0xbb, ...)</c> form
  188. /// which is primarily useful for testing.
  189. /// </summary>
  190. public static ByteString CopyFrom(params byte[] bytes)
  191. {
  192. return new ByteString((byte[]) bytes.Clone());
  193. }
  194. /// <summary>
  195. /// Constructs a <see cref="ByteString" /> from a portion of a byte array.
  196. /// </summary>
  197. public static ByteString CopyFrom(byte[] bytes, int offset, int count)
  198. {
  199. byte[] portion = new byte[count];
  200. ByteArray.Copy(bytes, offset, portion, 0, count);
  201. return new ByteString(portion);
  202. }
  203. #if NETSTANDARD2_0
  204. /// <summary>
  205. /// Constructs a <see cref="ByteString" /> from a read only span. The contents
  206. /// are copied, so further modifications to the span will not
  207. /// be reflected in the returned <see cref="ByteString" />.
  208. /// </summary>
  209. public static ByteString CopyFrom(ReadOnlySpan<byte> bytes)
  210. {
  211. return new ByteString(bytes.ToArray());
  212. }
  213. #endif
  214. /// <summary>
  215. /// Creates a new <see cref="ByteString" /> by encoding the specified text with
  216. /// the given encoding.
  217. /// </summary>
  218. public static ByteString CopyFrom(string text, Encoding encoding)
  219. {
  220. return new ByteString(encoding.GetBytes(text));
  221. }
  222. /// <summary>
  223. /// Creates a new <see cref="ByteString" /> by encoding the specified text in UTF-8.
  224. /// </summary>
  225. public static ByteString CopyFromUtf8(string text)
  226. {
  227. return CopyFrom(text, Encoding.UTF8);
  228. }
  229. /// <summary>
  230. /// Retuns the byte at the given index.
  231. /// </summary>
  232. public byte this[int index]
  233. {
  234. get { return bytes[index]; }
  235. }
  236. /// <summary>
  237. /// Converts this <see cref="ByteString"/> into a string by applying the given encoding.
  238. /// </summary>
  239. /// <remarks>
  240. /// This method should only be used to convert binary data which was the result of encoding
  241. /// text with the given encoding.
  242. /// </remarks>
  243. /// <param name="encoding">The encoding to use to decode the binary data into text.</param>
  244. /// <returns>The result of decoding the binary data with the given decoding.</returns>
  245. public string ToString(Encoding encoding)
  246. {
  247. return encoding.GetString(bytes, 0, bytes.Length);
  248. }
  249. /// <summary>
  250. /// Converts this <see cref="ByteString"/> into a string by applying the UTF-8 encoding.
  251. /// </summary>
  252. /// <remarks>
  253. /// This method should only be used to convert binary data which was the result of encoding
  254. /// text with UTF-8.
  255. /// </remarks>
  256. /// <returns>The result of decoding the binary data with the given decoding.</returns>
  257. public string ToStringUtf8()
  258. {
  259. return ToString(Encoding.UTF8);
  260. }
  261. /// <summary>
  262. /// Returns an iterator over the bytes in this <see cref="ByteString"/>.
  263. /// </summary>
  264. /// <returns>An iterator over the bytes in this object.</returns>
  265. public IEnumerator<byte> GetEnumerator()
  266. {
  267. return ((IEnumerable<byte>) bytes).GetEnumerator();
  268. }
  269. /// <summary>
  270. /// Returns an iterator over the bytes in this <see cref="ByteString"/>.
  271. /// </summary>
  272. /// <returns>An iterator over the bytes in this object.</returns>
  273. IEnumerator IEnumerable.GetEnumerator()
  274. {
  275. return GetEnumerator();
  276. }
  277. /// <summary>
  278. /// Creates a CodedInputStream from this ByteString's data.
  279. /// </summary>
  280. public CodedInputStream CreateCodedInput()
  281. {
  282. // We trust CodedInputStream not to reveal the provided byte array or modify it
  283. return new CodedInputStream(bytes);
  284. }
  285. /// <summary>
  286. /// Compares two byte strings for equality.
  287. /// </summary>
  288. /// <param name="lhs">The first byte string to compare.</param>
  289. /// <param name="rhs">The second byte string to compare.</param>
  290. /// <returns><c>true</c> if the byte strings are equal; false otherwise.</returns>
  291. public static bool operator ==(ByteString lhs, ByteString rhs)
  292. {
  293. if (ReferenceEquals(lhs, rhs))
  294. {
  295. return true;
  296. }
  297. if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
  298. {
  299. return false;
  300. }
  301. if (lhs.bytes.Length != rhs.bytes.Length)
  302. {
  303. return false;
  304. }
  305. for (int i = 0; i < lhs.Length; i++)
  306. {
  307. if (rhs.bytes[i] != lhs.bytes[i])
  308. {
  309. return false;
  310. }
  311. }
  312. return true;
  313. }
  314. /// <summary>
  315. /// Compares two byte strings for inequality.
  316. /// </summary>
  317. /// <param name="lhs">The first byte string to compare.</param>
  318. /// <param name="rhs">The second byte string to compare.</param>
  319. /// <returns><c>false</c> if the byte strings are equal; true otherwise.</returns>
  320. public static bool operator !=(ByteString lhs, ByteString rhs)
  321. {
  322. return !(lhs == rhs);
  323. }
  324. /// <summary>
  325. /// Compares this byte string with another object.
  326. /// </summary>
  327. /// <param name="obj">The object to compare this with.</param>
  328. /// <returns><c>true</c> if <paramref name="obj"/> refers to an equal <see cref="ByteString"/>; <c>false</c> otherwise.</returns>
  329. public override bool Equals(object obj)
  330. {
  331. return this == (obj as ByteString);
  332. }
  333. /// <summary>
  334. /// Returns a hash code for this object. Two equal byte strings
  335. /// will return the same hash code.
  336. /// </summary>
  337. /// <returns>A hash code for this object.</returns>
  338. public override int GetHashCode()
  339. {
  340. int ret = 23;
  341. foreach (byte b in bytes)
  342. {
  343. ret = (ret * 31) + b;
  344. }
  345. return ret;
  346. }
  347. /// <summary>
  348. /// Compares this byte string with another.
  349. /// </summary>
  350. /// <param name="other">The <see cref="ByteString"/> to compare this with.</param>
  351. /// <returns><c>true</c> if <paramref name="other"/> refers to an equal byte string; <c>false</c> otherwise.</returns>
  352. public bool Equals(ByteString other)
  353. {
  354. return this == other;
  355. }
  356. /// <summary>
  357. /// Used internally by CodedOutputStream to avoid creating a copy for the write
  358. /// </summary>
  359. internal void WriteRawBytesTo(CodedOutputStream outputStream)
  360. {
  361. outputStream.WriteRawBytes(bytes, 0, bytes.Length);
  362. }
  363. /// <summary>
  364. /// Copies the entire byte array to the destination array provided at the offset specified.
  365. /// </summary>
  366. public void CopyTo(byte[] array, int position)
  367. {
  368. ByteArray.Copy(bytes, 0, array, position, bytes.Length);
  369. }
  370. /// <summary>
  371. /// Writes the entire byte array to the provided stream
  372. /// </summary>
  373. public void WriteTo(Stream outputStream)
  374. {
  375. outputStream.Write(bytes, 0, bytes.Length);
  376. }
  377. }
  378. }