From aef072a46f7c902e6fa1adf18e34f8d4b1eba38a Mon Sep 17 00:00:00 2001 From: csharptest Date: Wed, 8 Jun 2011 18:00:43 -0500 Subject: [PATCH] Renamed Bytes to ByteArray and added a Reverse method. --- .../{Bytes.cs => ByteArray.cs} | 19 ++++++++++++++++--- src/ProtocolBuffers/ByteString.cs | 4 ++-- src/ProtocolBuffers/CodedInputStream.cs | 12 ++++++------ src/ProtocolBuffers/CodedOutputStream.cs | 12 ++++++------ src/ProtocolBuffers/ProtocolBuffers.csproj | 2 +- .../ProtocolBuffersLite.csproj | 2 +- 6 files changed, 32 insertions(+), 19 deletions(-) rename src/ProtocolBuffers/{Bytes.cs => ByteArray.cs} (63%) diff --git a/src/ProtocolBuffers/Bytes.cs b/src/ProtocolBuffers/ByteArray.cs similarity index 63% rename from src/ProtocolBuffers/Bytes.cs rename to src/ProtocolBuffers/ByteArray.cs index 88bc16f817..fd0b01e381 100644 --- a/src/ProtocolBuffers/Bytes.cs +++ b/src/ProtocolBuffers/ByteArray.cs @@ -3,10 +3,10 @@ /// /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy /// - static class Bytes + static class ByteArray { /// - /// The threshold above which you should use Buffer.BlockCopy rather than Bytes.Copy + /// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy /// const int CopyThreshold = 12; /// @@ -20,7 +20,7 @@ ByteCopy(src, srcOffset, dst, dstOffset, count); } /// - /// Copyies the bytes provided with a for loop, faster when there are only a few bytes to copy + /// Copy the bytes provided with a for loop, faster when there are only a few bytes to copy /// public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count) { @@ -28,5 +28,18 @@ for (int i = srcOffset; i < stop; i++) dst[dstOffset++] = src[i]; } + /// + /// Reverses the order of bytes in the array + /// + 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 diff --git a/src/ProtocolBuffers/ByteString.cs b/src/ProtocolBuffers/ByteString.cs index 06708af700..e5f422d4cf 100644 --- a/src/ProtocolBuffers/ByteString.cs +++ b/src/ProtocolBuffers/ByteString.cs @@ -123,7 +123,7 @@ namespace Google.ProtocolBuffers public static ByteString CopyFrom(byte[] bytes, int offset, int count) { byte[] portion = new byte[count]; - Bytes.Copy(bytes, offset, portion, 0, count); + ByteArray.Copy(bytes, offset, portion, 0, count); return new ByteString(portion); } @@ -261,7 +261,7 @@ namespace Google.ProtocolBuffers /// public void CopyTo(byte[] array, int position) { - Bytes.Copy(bytes, 0, array, position, bytes.Length); + ByteArray.Copy(bytes, 0, array, position, bytes.Length); } /// diff --git a/src/ProtocolBuffers/CodedInputStream.cs b/src/ProtocolBuffers/CodedInputStream.cs index 105dc3781f..000065bd4f 100644 --- a/src/ProtocolBuffers/CodedInputStream.cs +++ b/src/ProtocolBuffers/CodedInputStream.cs @@ -213,7 +213,7 @@ namespace Google.ProtocolBuffers { byte[] rawBytes = ReadRawBytes(8); if (!BitConverter.IsLittleEndian) - Array.Reverse(rawBytes); + ByteArray.Reverse(rawBytes); value = BitConverter.ToDouble(rawBytes, 0); } #else @@ -236,7 +236,7 @@ namespace Google.ProtocolBuffers { byte[] rawBytes = ReadRawBytes(4); if (!BitConverter.IsLittleEndian) - Array.Reverse(rawBytes); + ByteArray.Reverse(rawBytes); value = BitConverter.ToSingle(rawBytes, 0); } return true; @@ -1240,7 +1240,7 @@ namespace Google.ProtocolBuffers { // We have all the bytes we need already. byte[] bytes = new byte[size]; - Bytes.Copy(buffer, bufferPos, bytes, 0, size); + ByteArray.Copy(buffer, bufferPos, bytes, 0, size); bufferPos += size; return bytes; } @@ -1252,7 +1252,7 @@ namespace Google.ProtocolBuffers // First copy what we have. byte[] bytes = new byte[size]; int pos = bufferSize - bufferPos; - Bytes.Copy(buffer, bufferPos, bytes, 0, pos); + ByteArray.Copy(buffer, bufferPos, bytes, 0, pos); bufferPos = bufferSize; // We want to use RefillBuffer() and then copy from the buffer into our @@ -1268,7 +1268,7 @@ namespace Google.ProtocolBuffers RefillBuffer(true); } - Bytes.Copy(buffer, 0, bytes, pos, size - pos); + ByteArray.Copy(buffer, 0, bytes, pos, size - pos); bufferPos = size - pos; return bytes; @@ -1320,7 +1320,7 @@ namespace Google.ProtocolBuffers // Start by copying the leftover bytes from this.buffer. int newPos = originalBufferSize - originalBufferPos; - Bytes.Copy(buffer, originalBufferPos, bytes, 0, newPos); + ByteArray.Copy(buffer, originalBufferPos, bytes, 0, newPos); // And now all the chunks. foreach (byte[] chunk in chunks) diff --git a/src/ProtocolBuffers/CodedOutputStream.cs b/src/ProtocolBuffers/CodedOutputStream.cs index bd50866105..0bc4a462e8 100644 --- a/src/ProtocolBuffers/CodedOutputStream.cs +++ b/src/ProtocolBuffers/CodedOutputStream.cs @@ -648,7 +648,7 @@ namespace Google.ProtocolBuffers #if SILVERLIGHT2 || COMPACT_FRAMEWORK_35 byte[] rawBytes = BitConverter.GetBytes(value); if (!BitConverter.IsLittleEndian) - Array.Reverse(rawBytes); + ByteArray.Reverse(rawBytes); if (limit - position >= 8) { @@ -674,8 +674,8 @@ namespace Google.ProtocolBuffers public void WriteFloatNoTag(float value) { byte[] rawBytes = BitConverter.GetBytes(value); - if (!BitConverter.IsLittleEndian) - Array.Reverse(rawBytes); + if (!BitConverter.IsLittleEndian) + ByteArray.Reverse(rawBytes); if (limit - position >= 4) { @@ -1008,7 +1008,7 @@ namespace Google.ProtocolBuffers { if (limit - position >= length) { - Bytes.Copy(value, offset, buffer, position, length); + ByteArray.Copy(value, offset, buffer, position, length); // We have room in the current buffer. position += length; } @@ -1017,7 +1017,7 @@ namespace Google.ProtocolBuffers // Write extends past current buffer. Fill the rest of this buffer and // flush. int bytesWritten = limit - position; - Bytes.Copy(value, offset, buffer, position, bytesWritten); + ByteArray.Copy(value, offset, buffer, position, bytesWritten); offset += bytesWritten; length -= bytesWritten; position = limit; @@ -1029,7 +1029,7 @@ namespace Google.ProtocolBuffers if (length <= limit) { // Fits in new buffer. - Bytes.Copy(value, offset, buffer, 0, length); + ByteArray.Copy(value, offset, buffer, 0, length); position = length; } else diff --git a/src/ProtocolBuffers/ProtocolBuffers.csproj b/src/ProtocolBuffers/ProtocolBuffers.csproj index cf21290969..336b387fbd 100644 --- a/src/ProtocolBuffers/ProtocolBuffers.csproj +++ b/src/ProtocolBuffers/ProtocolBuffers.csproj @@ -99,7 +99,7 @@ Code - + diff --git a/src/ProtocolBuffers/ProtocolBuffersLite.csproj b/src/ProtocolBuffers/ProtocolBuffersLite.csproj index fc1c7f6417..44c9255077 100644 --- a/src/ProtocolBuffers/ProtocolBuffersLite.csproj +++ b/src/ProtocolBuffers/ProtocolBuffersLite.csproj @@ -78,7 +78,7 @@ - +