Renamed Bytes to ByteArray and added a Reverse method.

pull/288/head
csharptest 14 years ago committed by rogerk
parent 4ba365d793
commit aef072a46f
  1. 19
      src/ProtocolBuffers/ByteArray.cs
  2. 4
      src/ProtocolBuffers/ByteString.cs
  3. 12
      src/ProtocolBuffers/CodedInputStream.cs
  4. 10
      src/ProtocolBuffers/CodedOutputStream.cs
  5. 2
      src/ProtocolBuffers/ProtocolBuffers.csproj
  6. 2
      src/ProtocolBuffers/ProtocolBuffersLite.csproj

@ -3,10 +3,10 @@
/// <summary> /// <summary>
/// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
/// </summary> /// </summary>
static class Bytes static class ByteArray
{ {
/// <summary> /// <summary>
/// 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
/// </summary> /// </summary>
const int CopyThreshold = 12; const int CopyThreshold = 12;
/// <summary> /// <summary>
@ -20,7 +20,7 @@
ByteCopy(src, srcOffset, dst, dstOffset, count); ByteCopy(src, srcOffset, dst, dstOffset, count);
} }
/// <summary> /// <summary>
/// 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
/// </summary> /// </summary>
public static void ByteCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count) 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++) for (int i = srcOffset; i < stop; i++)
dst[dstOffset++] = src[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;
}
}
} }
} }

@ -123,7 +123,7 @@ namespace Google.ProtocolBuffers
public static ByteString CopyFrom(byte[] bytes, int offset, int count) public static ByteString CopyFrom(byte[] bytes, int offset, int count)
{ {
byte[] portion = new byte[count]; byte[] portion = new byte[count];
Bytes.Copy(bytes, offset, portion, 0, count); ByteArray.Copy(bytes, offset, portion, 0, count);
return new ByteString(portion); return new ByteString(portion);
} }
@ -261,7 +261,7 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
public void CopyTo(byte[] array, int position) public void CopyTo(byte[] array, int position)
{ {
Bytes.Copy(bytes, 0, array, position, bytes.Length); ByteArray.Copy(bytes, 0, array, position, bytes.Length);
} }
/// <summary> /// <summary>

@ -213,7 +213,7 @@ namespace Google.ProtocolBuffers
{ {
byte[] rawBytes = ReadRawBytes(8); byte[] rawBytes = ReadRawBytes(8);
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
Array.Reverse(rawBytes); ByteArray.Reverse(rawBytes);
value = BitConverter.ToDouble(rawBytes, 0); value = BitConverter.ToDouble(rawBytes, 0);
} }
#else #else
@ -236,7 +236,7 @@ namespace Google.ProtocolBuffers
{ {
byte[] rawBytes = ReadRawBytes(4); byte[] rawBytes = ReadRawBytes(4);
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
Array.Reverse(rawBytes); ByteArray.Reverse(rawBytes);
value = BitConverter.ToSingle(rawBytes, 0); value = BitConverter.ToSingle(rawBytes, 0);
} }
return true; return true;
@ -1240,7 +1240,7 @@ namespace Google.ProtocolBuffers
{ {
// We have all the bytes we need already. // We have all the bytes we need already.
byte[] bytes = new byte[size]; byte[] bytes = new byte[size];
Bytes.Copy(buffer, bufferPos, bytes, 0, size); ByteArray.Copy(buffer, bufferPos, bytes, 0, size);
bufferPos += size; bufferPos += size;
return bytes; return bytes;
} }
@ -1252,7 +1252,7 @@ namespace Google.ProtocolBuffers
// First copy what we have. // First copy what we have.
byte[] bytes = new byte[size]; byte[] bytes = new byte[size];
int pos = bufferSize - bufferPos; int pos = bufferSize - bufferPos;
Bytes.Copy(buffer, bufferPos, bytes, 0, pos); ByteArray.Copy(buffer, bufferPos, bytes, 0, pos);
bufferPos = bufferSize; bufferPos = bufferSize;
// We want to use RefillBuffer() and then copy from the buffer into our // We want to use RefillBuffer() and then copy from the buffer into our
@ -1268,7 +1268,7 @@ namespace Google.ProtocolBuffers
RefillBuffer(true); RefillBuffer(true);
} }
Bytes.Copy(buffer, 0, bytes, pos, size - pos); ByteArray.Copy(buffer, 0, bytes, pos, size - pos);
bufferPos = size - pos; bufferPos = size - pos;
return bytes; return bytes;
@ -1320,7 +1320,7 @@ namespace Google.ProtocolBuffers
// Start by copying the leftover bytes from this.buffer. // Start by copying the leftover bytes from this.buffer.
int newPos = originalBufferSize - originalBufferPos; int newPos = originalBufferSize - originalBufferPos;
Bytes.Copy(buffer, originalBufferPos, bytes, 0, newPos); ByteArray.Copy(buffer, originalBufferPos, bytes, 0, newPos);
// And now all the chunks. // And now all the chunks.
foreach (byte[] chunk in chunks) foreach (byte[] chunk in chunks)

@ -648,7 +648,7 @@ namespace Google.ProtocolBuffers
#if SILVERLIGHT2 || COMPACT_FRAMEWORK_35 #if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
byte[] rawBytes = BitConverter.GetBytes(value); byte[] rawBytes = BitConverter.GetBytes(value);
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
Array.Reverse(rawBytes); ByteArray.Reverse(rawBytes);
if (limit - position >= 8) if (limit - position >= 8)
{ {
@ -675,7 +675,7 @@ namespace Google.ProtocolBuffers
{ {
byte[] rawBytes = BitConverter.GetBytes(value); byte[] rawBytes = BitConverter.GetBytes(value);
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
Array.Reverse(rawBytes); ByteArray.Reverse(rawBytes);
if (limit - position >= 4) if (limit - position >= 4)
{ {
@ -1008,7 +1008,7 @@ namespace Google.ProtocolBuffers
{ {
if (limit - position >= length) 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. // We have room in the current buffer.
position += length; position += length;
} }
@ -1017,7 +1017,7 @@ namespace Google.ProtocolBuffers
// Write extends past current buffer. Fill the rest of this buffer and // Write extends past current buffer. Fill the rest of this buffer and
// flush. // flush.
int bytesWritten = limit - position; int bytesWritten = limit - position;
Bytes.Copy(value, offset, buffer, position, bytesWritten); ByteArray.Copy(value, offset, buffer, position, bytesWritten);
offset += bytesWritten; offset += bytesWritten;
length -= bytesWritten; length -= bytesWritten;
position = limit; position = limit;
@ -1029,7 +1029,7 @@ namespace Google.ProtocolBuffers
if (length <= limit) if (length <= limit)
{ {
// Fits in new buffer. // Fits in new buffer.
Bytes.Copy(value, offset, buffer, 0, length); ByteArray.Copy(value, offset, buffer, 0, length);
position = length; position = length;
} }
else else

@ -99,7 +99,7 @@
<Compile Include="AbstractMessageLite.cs"> <Compile Include="AbstractMessageLite.cs">
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Bytes.cs" /> <Compile Include="ByteArray.cs" />
<Compile Include="ByteString.cs" /> <Compile Include="ByteString.cs" />
<Compile Include="Collections\Enumerables.cs" /> <Compile Include="Collections\Enumerables.cs" />
<Compile Include="Collections\IPopsicleList.cs" /> <Compile Include="Collections\IPopsicleList.cs" />

@ -78,7 +78,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AbstractBuilderLite.cs" /> <Compile Include="AbstractBuilderLite.cs" />
<Compile Include="AbstractMessageLite.cs" /> <Compile Include="AbstractMessageLite.cs" />
<Compile Include="Bytes.cs" /> <Compile Include="ByteArray.cs" />
<Compile Include="CodedOutputStream.ComputeSize.cs" /> <Compile Include="CodedOutputStream.ComputeSize.cs" />
<Compile Include="Collections\Dictionaries.cs" /> <Compile Include="Collections\Dictionaries.cs" />
<Compile Include="Collections\Enumerables.cs" /> <Compile Include="Collections\Enumerables.cs" />

Loading…
Cancel
Save