Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/ByteString.cs @ 3932

Last change on this file since 3932 was 3857, checked in by abeham, 15 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 6.6 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections;
37using System.Collections.Generic;
38using System.Text;
39
40namespace Google.ProtocolBuffers {
41  /// <summary>
42  /// Immutable array of bytes.
43  /// TODO(jonskeet): Implement the common collection interfaces?
44  /// </summary>
45  public sealed class ByteString : IEnumerable<byte>, IEquatable<ByteString> {
46
47    private static readonly ByteString empty = new ByteString(new byte[0]);
48
49    private readonly byte[] bytes;
50
51    /// <summary>
52    /// Constructs a new ByteString from the given byte array. The array is
53    /// *not* copied, and must not be modified after this constructor is called.
54    /// </summary>
55    private ByteString(byte[] bytes) {
56      this.bytes = bytes;
57    }
58
59    /// <summary>
60    /// Returns an empty ByteString.
61    /// </summary>
62    public static ByteString Empty {
63      get { return empty; }
64    }
65
66    /// <summary>
67    /// Returns the length of this ByteString in bytes.
68    /// </summary>
69    public int Length {
70      get { return bytes.Length; }
71    }
72
73    public bool IsEmpty {
74      get { return Length == 0; }
75    }
76
77    public byte[] ToByteArray() {
78      return (byte[])bytes.Clone();
79    }
80
81    /// <summary>
82    /// Constructs a ByteString from the given array. The contents
83    /// are copied, so further modifications to the array will not
84    /// be reflected in the returned ByteString.
85    /// </summary>
86    public static ByteString CopyFrom(byte[] bytes) {
87      return new ByteString((byte[]) bytes.Clone());
88    }
89
90    /// <summary>
91    /// Constructs a ByteString from a portion of a byte array.
92    /// </summary>
93    public static ByteString CopyFrom(byte[] bytes, int offset, int count) {
94      byte[] portion = new byte[count];
95      Array.Copy(bytes, offset, portion, 0, count);
96      return new ByteString(portion);
97    }
98
99    /// <summary>
100    /// Creates a new ByteString by encoding the specified text with
101    /// the given encoding.
102    /// </summary>
103    public static ByteString CopyFrom(string text, Encoding encoding) {
104      return new ByteString(encoding.GetBytes(text));
105    }
106
107    /// <summary>
108    /// Creates a new ByteString by encoding the specified text in UTF-8.
109    /// </summary>
110    public static ByteString CopyFromUtf8(string text) {
111      return CopyFrom(text, Encoding.UTF8);
112    }
113   
114    /// <summary>
115    /// Retuns the byte at the given index.
116    /// </summary>
117    public byte this[int index] {
118      get { return bytes[index]; }
119    }
120
121    public string ToString(Encoding encoding) {
122      return encoding.GetString(bytes, 0, bytes.Length);
123    }
124
125    public string ToStringUtf8() {
126      return ToString(Encoding.UTF8);
127    }
128
129    public IEnumerator<byte> GetEnumerator() {
130      return ((IEnumerable<byte>) bytes).GetEnumerator();
131    }
132
133    IEnumerator IEnumerable.GetEnumerator() {
134      return GetEnumerator();
135    }
136
137    /// <summary>
138    /// Creates a CodedInputStream from this ByteString's data.
139    /// </summary>
140    public CodedInputStream CreateCodedInput() {
141     
142      // We trust CodedInputStream not to reveal the provided byte array or modify it
143      return CodedInputStream.CreateInstance(bytes);
144    }
145
146    // TODO(jonskeet): CopyTo if it turns out to be required
147
148    public override bool Equals(object obj) {
149      ByteString other = obj as ByteString;
150      if (obj == null) {
151        return false;
152      }
153      return Equals(other);
154    }
155
156    public override int GetHashCode() {
157      int ret = 23;
158      foreach (byte b in bytes) {
159        ret = (ret << 8) | b;
160      }
161      return ret;
162    }
163
164    public bool Equals(ByteString other) {
165      if (other.bytes.Length != bytes.Length) {
166        return false;
167      }
168      for (int i = 0; i < bytes.Length; i++) {
169        if (other.bytes[i] != bytes[i]) {
170          return false;
171        }
172      }
173      return true;
174    }
175
176    /// <summary>
177    /// Builder for ByteStrings which allows them to be created without extra
178    /// copying being involved. This has to be a nested type in order to have access
179    /// to the private ByteString constructor.
180    /// </summary>
181    internal sealed class CodedBuilder {
182      private readonly CodedOutputStream output;
183      private readonly byte[] buffer;
184
185      internal CodedBuilder(int size) {
186        buffer = new byte[size];
187        output = CodedOutputStream.CreateInstance(buffer);
188      }
189
190      internal ByteString Build() {
191        output.CheckNoSpaceLeft();
192
193        // We can be confident that the CodedOutputStream will not modify the
194        // underlying bytes anymore because it already wrote all of them.  So,
195        // no need to make a copy.
196        return new ByteString(buffer);
197      }
198
199      internal CodedOutputStream CodedOutput {
200        get {
201          return output;
202        }
203      }
204    }
205  }
206}
Note: See TracBrowser for help on using the repository browser.