ByteString.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. // Protocol Buffers - Google's data interchange format
  5. // Copyright 2008 Google Inc.
  6. // http://code.google.com/p/protobuf/
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. using System.Text;
  20. namespace Google.ProtocolBuffers {
  21. /// <summary>
  22. /// Immutable array of bytes.
  23. /// TODO(jonskeet): Implement the common collection interfaces?
  24. /// </summary>
  25. public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString> {
  26. private static readonly ByteString empty = new ByteString(new byte[0]);
  27. private readonly byte[] bytes;
  28. /// <summary>
  29. /// Constructs a new ByteString from the given byte array. The array is
  30. /// *not* copied, and must not be modified after this constructor is called.
  31. /// </summary>
  32. private ByteString(byte[] bytes) {
  33. this.bytes = bytes;
  34. }
  35. /// <summary>
  36. /// Returns an empty ByteString.
  37. /// </summary>
  38. public static ByteString Empty {
  39. get { return empty; }
  40. }
  41. /// <summary>
  42. /// Returns the length of this ByteString in bytes.
  43. /// </summary>
  44. public int Length {
  45. get { return bytes.Length; }
  46. }
  47. public bool IsEmpty {
  48. get { return Length == 0; }
  49. }
  50. public byte[] ToByteArray() {
  51. return (byte[])bytes.Clone();
  52. }
  53. /// <summary>
  54. /// Constructs a ByteString from the given array. The contents
  55. /// are copied, so further modifications to the array will not
  56. /// be reflected in the returned ByteString.
  57. /// </summary>
  58. public static ByteString CopyFrom(byte[] bytes) {
  59. return new ByteString((byte[]) bytes.Clone());
  60. }
  61. /// <summary>
  62. /// Constructs a ByteString from a portion of a byte array.
  63. /// </summary>
  64. public static ByteString CopyFrom(byte[] bytes, int offset, int count) {
  65. byte[] portion = new byte[count];
  66. Array.Copy(bytes, offset, portion, 0, count);
  67. return new ByteString(portion);
  68. }
  69. /// <summary>
  70. /// Creates a new ByteString by encoding the specified text with
  71. /// the given encoding.
  72. /// </summary>
  73. public static ByteString CopyFrom(string text, Encoding encoding) {
  74. return new ByteString(encoding.GetBytes(text));
  75. }
  76. /// <summary>
  77. /// Creates a new ByteString by encoding the specified text in UTF-8.
  78. /// </summary>
  79. public static ByteString CopyFromUtf8(string text) {
  80. return CopyFrom(text, Encoding.UTF8);
  81. }
  82. /// <summary>
  83. /// Retuns the byte at the given index.
  84. /// </summary>
  85. public byte this[int index] {
  86. get { return bytes[index]; }
  87. }
  88. public string ToString(Encoding encoding) {
  89. return encoding.GetString(bytes);
  90. }
  91. public string ToStringUtf8() {
  92. return ToString(Encoding.UTF8);
  93. }
  94. public IEnumerator<byte> GetEnumerator() {
  95. return ((IEnumerable<byte>) bytes).GetEnumerator();
  96. }
  97. IEnumerator IEnumerable.GetEnumerator() {
  98. return GetEnumerator();
  99. }
  100. /// <summary>
  101. /// Creates a CodedInputStream from this ByteString's data.
  102. /// </summary>
  103. public CodedInputStream CreateCodedInput() {
  104. // We trust CodedInputStream not to reveal the provided byte array or modify it
  105. return CodedInputStream.CreateInstance(bytes);
  106. }
  107. // TODO(jonskeet): CopyTo if it turns out to be required
  108. public override bool Equals(object obj) {
  109. ByteString other = obj as ByteString;
  110. if (obj == null) {
  111. return false;
  112. }
  113. return Equals(other);
  114. }
  115. public override int GetHashCode() {
  116. int ret = 23;
  117. foreach (byte b in bytes) {
  118. ret = (ret << 8) | b;
  119. }
  120. return ret;
  121. }
  122. public bool Equals(ByteString other) {
  123. if (other.bytes.Length != bytes.Length) {
  124. return false;
  125. }
  126. for (int i = 0; i < bytes.Length; i++) {
  127. if (other.bytes[i] != bytes[i]) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. /// <summary>
  134. /// Builder for ByteStrings which allows them to be created without extra
  135. /// copying being involved. This has to be a nested type in order to have access
  136. /// to the private ByteString constructor.
  137. /// </summary>
  138. internal sealed class CodedBuilder {
  139. private readonly CodedOutputStream output;
  140. private readonly byte[] buffer;
  141. internal CodedBuilder(int size) {
  142. buffer = new byte[size];
  143. output = CodedOutputStream.CreateInstance(buffer);
  144. }
  145. public ByteString Build() {
  146. output.CheckNoSpaceLeft();
  147. // We can be confident that the CodedOutputStream will not modify the
  148. // underlying bytes anymore because it already wrote all of them. So,
  149. // no need to make a copy.
  150. return new ByteString(buffer);
  151. }
  152. public CodedOutputStream CodedOutput {
  153. get {
  154. return output;
  155. }
  156. }
  157. }
  158. }
  159. }