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