• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2008 Google Inc.  All rights reserved.
4 //
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file or at
7 // https://developers.google.com/open-source/licenses/bsd
8 #endregion
9 
10 using System;
11 
12 namespace Google.Protobuf
13 {
14     /// <summary>
15     /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
16     /// </summary>
17     internal static class ByteArray
18     {
19         /// <summary>
20         /// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy
21         /// </summary>
22         private const int CopyThreshold = 12;
23 
24         /// <summary>
25         /// Determines which copy routine to use based on the number of bytes to be copied.
26         /// </summary>
Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)27         internal static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
28         {
29             if (count > CopyThreshold)
30             {
31                 Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count);
32             }
33             else
34             {
35                 int stop = srcOffset + count;
36                 for (int i = srcOffset; i < stop; i++)
37                 {
38                     dst[dstOffset++] = src[i];
39                 }
40             }
41         }
42 
43         /// <summary>
44         /// Reverses the order of bytes in the array
45         /// </summary>
Reverse(byte[] bytes)46         internal static void Reverse(byte[] bytes)
47         {
48             for (int first = 0, last = bytes.Length - 1; first < last; first++, last--)
49             {
50                 byte temp = bytes[first];
51                 bytes[first] = bytes[last];
52                 bytes[last] = temp;
53             }
54         }
55     }
56 }