1 | #region Copyright notice and license
|
---|
2 |
|
---|
3 | // Protocol Buffers - Google's data interchange format
|
---|
4 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
5 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
6 | // Original C++/Java/Python code:
|
---|
7 | // http://code.google.com/p/protobuf/
|
---|
8 | //
|
---|
9 | // Redistribution and use in source and binary forms, with or without
|
---|
10 | // modification, are permitted provided that the following conditions are
|
---|
11 | // met:
|
---|
12 | //
|
---|
13 | // * Redistributions of source code must retain the above copyright
|
---|
14 | // notice, this list of conditions and the following disclaimer.
|
---|
15 | // * Redistributions in binary form must reproduce the above
|
---|
16 | // copyright notice, this list of conditions and the following disclaimer
|
---|
17 | // in the documentation and/or other materials provided with the
|
---|
18 | // distribution.
|
---|
19 | // * Neither the name of Google Inc. nor the names of its
|
---|
20 | // contributors may be used to endorse or promote products derived from
|
---|
21 | // this software without specific prior written permission.
|
---|
22 | //
|
---|
23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | using System;
|
---|
38 |
|
---|
39 | namespace Google.ProtocolBuffers
|
---|
40 | {
|
---|
41 | /// <summary>
|
---|
42 | /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
|
---|
43 | /// </summary>
|
---|
44 | internal static class ByteArray
|
---|
45 | {
|
---|
46 | /// <summary>
|
---|
47 | /// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy
|
---|
48 | /// </summary>
|
---|
49 | private const int CopyThreshold = 12;
|
---|
50 |
|
---|
51 | /// <summary>
|
---|
52 | /// Determines which copy routine to use based on the number of bytes to be copied.
|
---|
53 | /// </summary>
|
---|
54 | public static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
|
---|
55 | {
|
---|
56 | if (count > CopyThreshold)
|
---|
57 | {
|
---|
58 | Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count);
|
---|
59 | }
|
---|
60 | else
|
---|
61 | {
|
---|
62 | ByteCopy(src, srcOffset, dst, dstOffset, count);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | /// <summary>
|
---|
67 | /// Copy the bytes provided with a for loop, faster when there are only a few bytes to copy
|
---|
68 | /// </summary>
|
---|
69 | public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
|
---|
70 | {
|
---|
71 | int stop = srcOffset + count;
|
---|
72 | for (int i = srcOffset; i < stop; i++)
|
---|
73 | {
|
---|
74 | dst[dstOffset++] = src[i];
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /// <summary>
|
---|
79 | /// Reverses the order of bytes in the array
|
---|
80 | /// </summary>
|
---|
81 | public static void Reverse(byte[] bytes)
|
---|
82 | {
|
---|
83 | byte temp;
|
---|
84 | for (int first = 0, last = bytes.Length - 1; first < last; first++, last--)
|
---|
85 | {
|
---|
86 | temp = bytes[first];
|
---|
87 | bytes[first] = bytes[last];
|
---|
88 | bytes[last] = temp;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | } |
---|