aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/ByteArray.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-08 18:00:43 -0500
committerrogerk <devnull@localhost>2011-06-08 18:00:43 -0500
commitaef072a46f7c902e6fa1adf18e34f8d4b1eba38a (patch)
tree0de385dfaf8e42a1084665267808344017783504 /src/ProtocolBuffers/ByteArray.cs
parent4ba365d7932ca661640a402c93d2dfb2816ffd62 (diff)
downloadprotobuf-aef072a46f7c902e6fa1adf18e34f8d4b1eba38a.tar.gz
protobuf-aef072a46f7c902e6fa1adf18e34f8d4b1eba38a.tar.bz2
protobuf-aef072a46f7c902e6fa1adf18e34f8d4b1eba38a.zip
Renamed Bytes to ByteArray and added a Reverse method.
Diffstat (limited to 'src/ProtocolBuffers/ByteArray.cs')
-rw-r--r--src/ProtocolBuffers/ByteArray.cs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/ProtocolBuffers/ByteArray.cs b/src/ProtocolBuffers/ByteArray.cs
new file mode 100644
index 00000000..fd0b01e3
--- /dev/null
+++ b/src/ProtocolBuffers/ByteArray.cs
@@ -0,0 +1,45 @@
+namespace Google.ProtocolBuffers
+{
+ /// <summary>
+ /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
+ /// </summary>
+ static class ByteArray
+ {
+ /// <summary>
+ /// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy
+ /// </summary>
+ const int CopyThreshold = 12;
+ /// <summary>
+ /// Determines which copy routine to use based on the number of bytes to be copied.
+ /// </summary>
+ public static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ {
+ if (count > CopyThreshold)
+ global::System.Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count);
+ else
+ ByteCopy(src, srcOffset, dst, dstOffset, count);
+ }
+ /// <summary>
+ /// Copy the bytes provided with a for loop, faster when there are only a few bytes to copy
+ /// </summary>
+ public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
+ {
+ int stop = srcOffset + count;
+ for (int i = srcOffset; i < stop; i++)
+ dst[dstOffset++] = src[i];
+ }
+ /// <summary>
+ /// Reverses the order of bytes in the array
+ /// </summary>
+ public static void Reverse(byte[] bytes)
+ {
+ byte temp;
+ for (int first = 0, last = bytes.Length - 1; first < last; first++, last--)
+ {
+ temp = bytes[first];
+ bytes[first] = bytes[last];
+ bytes[last] = temp;
+ }
+ }
+ }
+} \ No newline at end of file