Skip to content

Commit

Permalink
Replace Array.Copy with Buffer.BlockCopy.
Browse files Browse the repository at this point in the history
See discussion at dotnet/corefx#6103.
  • Loading branch information
bgrainger committed Apr 22, 2016
1 parent f59c6d8 commit 662d8ac
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/MySql.Data/ByteArrayReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public byte[] ReadNullTerminatedByteString()
if (index == m_maxOffset)
throw new FormatException("Read past end of buffer looking for NUL.");
byte[] substring = new byte[index - m_offset];
Array.Copy(m_buffer, m_offset, substring, 0, substring.Length);
Buffer.BlockCopy(m_buffer, m_offset, substring, 0, substring.Length);
m_offset = index + 1;
return substring;
}
Expand All @@ -120,7 +120,7 @@ public byte[] ReadByteString(int length)
{
VerifyRead(length);
var result = new byte[length];
Array.Copy(m_buffer, m_offset, result, 0, result.Length);
Buffer.BlockCopy(m_buffer, m_offset, result, 0, result.Length);
m_offset += length;
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/MySql.Data/MySqlClient/MySqlDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int b
throw new ArgumentException("bufferOffset + length cannot exceed buffer.Length", nameof(length));

int lengthToCopy = Math.Min(m_dataLengths[ordinal] - (int) dataOffset, length);
Array.Copy(m_currentRow, checked((int) (m_dataOffsets[ordinal] + dataOffset)), buffer, bufferOffset, lengthToCopy);
Buffer.BlockCopy(m_currentRow, checked((int) (m_dataOffsets[ordinal] + dataOffset)), buffer, bufferOffset, lengthToCopy);
return lengthToCopy;
}

Expand Down Expand Up @@ -399,7 +399,7 @@ public override object GetValue(int ordinal)
if (columnDefinition.CharacterSet == CharacterSet.Binary)
{
var result = new byte[m_dataLengths[ordinal]];
Array.Copy(m_currentRow, m_dataOffsets[ordinal], result, 0, result.Length);
Buffer.BlockCopy(m_currentRow, m_dataOffsets[ordinal], result, 0, result.Length);
return Connection.OldGuids && columnDefinition.ColumnLength == 16 ? (object) new Guid(result) : result;
}
return Encoding.UTF8.GetString(data);
Expand Down
4 changes: 2 additions & 2 deletions src/MySql.Data/Serialization/AuthenticationUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public static byte[] HashPassword(byte[] challenge, int offset, string password)
using (var sha1 = SHA1.Create())
{
var combined = new byte[40];
Array.Copy(challenge, offset, combined, 0, 20);
Buffer.BlockCopy(challenge, offset, combined, 0, 20);

var passwordBytes = Encoding.UTF8.GetBytes(password);
var hashedPassword = sha1.ComputeHash(passwordBytes);

var doubleHashedPassword = sha1.ComputeHash(hashedPassword);
Array.Copy(doubleHashedPassword, 0, combined, 20, 20);
Buffer.BlockCopy(doubleHashedPassword, 0, combined, 20, 20);

var xorBytes = sha1.ComputeHash(combined);
for (int i = 0; i < hashedPassword.Length; i++)
Expand Down
4 changes: 2 additions & 2 deletions src/MySql.Data/Serialization/InitialHandshakePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ internal InitialHandshakePacket(ByteArrayReader reader)
{
var authPluginData2 = reader.ReadByteString(Math.Max(13, authPluginDataLength - 8));
var concatenated = new byte[AuthPluginData.Length + authPluginData2.Length];
Array.Copy(AuthPluginData, concatenated, AuthPluginData.Length);
Array.Copy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length);
Buffer.BlockCopy(AuthPluginData, 0, concatenated, 0, AuthPluginData.Length);
Buffer.BlockCopy(authPluginData2, 0, concatenated, AuthPluginData.Length, authPluginData2.Length);
AuthPluginData = concatenated;
}
if (ProtocolCapabilities.HasFlag(ProtocolCapabilities.PluginAuth))
Expand Down
6 changes: 3 additions & 3 deletions src/MySql.Data/Serialization/PacketTransmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private async Task DoSendAsync(PayloadData payload, CancellationToken cancellati

if (bytesToSend <= m_buffer.Length - 4)
{
Array.Copy(data.Array, data.Offset, m_buffer, 4, bytesToSend);
Buffer.BlockCopy(data.Array, data.Offset, m_buffer, 4, bytesToSend);
m_socketAwaitable.EventArgs.SetBuffer(0, bytesToSend + 4);
await m_socket.SendAsync(m_socketAwaitable);
}
Expand All @@ -85,7 +85,7 @@ private async Task<PayloadData> DoReceiveAsync(CancellationToken cancellationTok
if (m_end - m_offset < 4)
{
if (m_end - m_offset > 0)
Array.Copy(m_buffer, m_offset, m_buffer, 0, m_end - m_offset);
Buffer.BlockCopy(m_buffer, m_offset, m_buffer, 0, m_end - m_offset);
m_end -= m_offset;
m_offset = 0;
}
Expand Down Expand Up @@ -140,7 +140,7 @@ private async Task<PayloadData> DoReceiveAsync(CancellationToken cancellationTok
var readData = m_buffer;
if (payloadLength > m_buffer.Length)
readData = new byte[payloadLength];
Array.Copy(m_buffer, m_offset, readData, 0, m_end - m_offset);
Buffer.BlockCopy(m_buffer, m_offset, readData, 0, m_end - m_offset);
m_socketAwaitable.EventArgs.SetBuffer(readData, 0, 0);

// read payload
Expand Down

0 comments on commit 662d8ac

Please sign in to comment.