ByteString.cs 4.4 KB

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