123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- #region Copyright notice and license
- #endregion
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Google.Protobuf.Compatibility;
- using MongoDB.Bson.Serialization.Attributes;
- namespace Google.Protobuf
- {
-
-
-
- public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString>
- {
- private static readonly ByteString empty = new ByteString(new byte[0]);
- public byte[] bytes;
-
-
-
- public static class Unsafe
- {
-
-
-
-
- public static ByteString FromBytes(byte[] bytes)
- {
- return new ByteString(bytes);
- }
-
-
-
-
- public static byte[] GetBuffer(ByteString bytes)
- {
- return bytes.bytes;
- }
- }
-
-
-
- internal static ByteString AttachBytes(byte[] bytes)
- {
- return new ByteString(bytes);
- }
-
- public ByteString()
- {}
- public ByteString(List<byte> list)
- {
- this.bytes = list.ToArray();
- }
-
-
-
-
- public ByteString(byte[] bytes)
- {
- this.bytes = bytes;
- }
-
-
-
- public static ByteString Empty
- {
- get { return empty; }
- }
-
-
-
- [BsonIgnore]
- public int Length
- {
- get { return bytes.Length; }
- }
-
-
-
- [BsonIgnore]
- public bool IsEmpty
- {
- get { return Length == 0; }
- }
-
-
-
-
-
- public byte[] ToByteArray()
- {
- return (byte[]) bytes.Clone();
- }
-
-
-
-
- public string ToBase64()
- {
- return Convert.ToBase64String(bytes);
- }
-
-
-
- public static ByteString FromBase64(string bytes)
- {
-
-
- return bytes == "" ? Empty : new ByteString(Convert.FromBase64String(bytes));
- }
-
-
-
-
-
-
-
- public static ByteString FromStream(Stream stream)
- {
- ProtoPreconditions.CheckNotNull(stream, "stream");
- int capacity = stream.CanSeek ? checked((int) (stream.Length - stream.Position)) : 0;
- var memoryStream = new MemoryStream(capacity);
- stream.CopyTo(memoryStream);
-
- byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
- return AttachBytes(bytes);
- }
-
-
-
-
-
-
-
- public static ByteString CopyFrom(params byte[] bytes)
- {
- return new ByteString((byte[]) bytes.Clone());
- }
-
-
-
- public static ByteString CopyFrom(byte[] bytes, int offset, int count)
- {
- byte[] portion = new byte[count];
- ByteArray.Copy(bytes, offset, portion, 0, count);
- return new ByteString(portion);
- }
-
-
-
-
- public static ByteString CopyFrom(string text, Encoding encoding)
- {
- return new ByteString(encoding.GetBytes(text));
- }
-
-
-
- public static ByteString CopyFromUtf8(string text)
- {
- return CopyFrom(text, Encoding.UTF8);
- }
-
-
-
- [BsonIgnore]
- public byte this[int index]
- {
- get { return bytes[index]; }
- }
-
-
-
-
-
-
-
-
-
- public string ToString(Encoding encoding)
- {
- return encoding.GetString(bytes, 0, bytes.Length);
- }
-
-
-
-
-
-
-
-
- public string ToStringUtf8()
- {
- return ToString(Encoding.UTF8);
- }
-
-
-
-
- public IEnumerator<byte> GetEnumerator()
- {
- return ((IEnumerable<byte>) bytes).GetEnumerator();
- }
-
-
-
-
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
-
-
-
- public CodedInputStream CreateCodedInput()
- {
-
- return new CodedInputStream(bytes);
- }
-
-
-
-
-
-
- public static bool operator ==(ByteString lhs, ByteString rhs)
- {
- if (ReferenceEquals(lhs, rhs))
- {
- return true;
- }
- if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null))
- {
- return false;
- }
- if (lhs.bytes.Length != rhs.bytes.Length)
- {
- return false;
- }
- for (int i = 0; i < lhs.Length; i++)
- {
- if (rhs.bytes[i] != lhs.bytes[i])
- {
- return false;
- }
- }
- return true;
- }
-
-
-
-
-
-
- public static bool operator !=(ByteString lhs, ByteString rhs)
- {
- return !(lhs == rhs);
- }
-
-
-
-
-
- public override bool Equals(object obj)
- {
- return this == (obj as ByteString);
- }
-
-
-
-
-
- public override int GetHashCode()
- {
- int ret = 23;
- foreach (byte b in bytes)
- {
- ret = (ret * 31) + b;
- }
- return ret;
- }
-
-
-
-
-
- public bool Equals(ByteString other)
- {
- return this == other;
- }
-
-
-
- internal void WriteRawBytesTo(CodedOutputStream outputStream)
- {
- outputStream.WriteRawBytes(bytes, 0, bytes.Length);
- }
-
-
-
- public void CopyTo(byte[] array, int position)
- {
- ByteArray.Copy(bytes, 0, array, position, bytes.Length);
- }
-
-
-
- public void WriteTo(Stream outputStream)
- {
- outputStream.Write(bytes, 0, bytes.Length);
- }
- }
- }
|