Skip to content

Commit

Permalink
Use Buffer.BlockCopy with byte[] (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaur-Parminder authored Jan 26, 2022
1 parent 3b945ee commit 8202268
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2046,7 +2046,7 @@ private bool TryGetBytesInternal(int i, long dataIndex, byte[] buffer, int buffe
cbytes = length;
}

Array.Copy(data, ndataIndex, buffer, bufferIndex, cbytes);
Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, cbytes);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ private void ValidateActiveOnConnection(SqlInternalConnection connection)
private Guid GetGlobalTxnIdentifierFromToken()
{
byte[] txnGuid = new byte[16];
Array.Copy(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length);
Buffer.BlockCopy(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length);
return new Guid(txnGuid);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ private byte[] PrepareByteBuffer(int numberOfChars, out int byteBufferUsed)
{
// Otherwise, copy over the leftover buffer
byteBuffer = new byte[byteBufferSize];
Array.Copy(_leftOverBytes, byteBuffer, _leftOverBytes.Length);
Buffer.BlockCopy(_leftOverBytes, 0, byteBuffer, 0,_leftOverBytes.Length);
byteBufferUsed = _leftOverBytes.Length;
}
}
Expand Down Expand Up @@ -411,7 +411,7 @@ private int DecodeBytesToChars(byte[] inBuffer, int inBufferCount, char[] outBuf
if ((!completed) && (bytesUsed < inBufferCount))
{
_leftOverBytes = new byte[inBufferCount - bytesUsed];
Array.Copy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length);
Buffer.BlockCopy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ override public int Read(byte[] buffer, int offset, int count)
cb = _cachedBytes[_currentArrayIndex].Length - _currentPosition;
if (cb > count)
cb = count;
Array.Copy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb);
Buffer.BlockCopy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb);

_currentPosition += cb;
count -= (int)cb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ public SqlBinary Adjust(SqlBinary value)
{
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(rgbValue, rgbNewValue, rgbValue.Length);
Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, rgbValue.Length);
Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length);
return new SqlBinary(rgbNewValue);
}
Expand All @@ -1307,7 +1307,7 @@ public SqlBinary Adjust(SqlBinary value)
{
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(rgbValue, rgbNewValue, (int)MaxLength);
Buffer.BlockCopy(rgbValue,0, rgbNewValue,0, (int)MaxLength);
value = new SqlBinary(rgbNewValue);
}

Expand Down Expand Up @@ -1399,7 +1399,7 @@ public SqlBytes Adjust(SqlBytes value)
if (value.MaxLength < MaxLength)
{
byte[] rgbNew = new byte[MaxLength];
Array.Copy(value.Buffer, rgbNew, (int)oldLength);
Buffer.BlockCopy(value.Buffer, 0,rgbNew,0, (int)oldLength);
value = new SqlBytes(rgbNew);
}

Expand Down Expand Up @@ -1928,7 +1928,7 @@ public byte[] Adjust(byte[] value)
if (value.Length < MaxLength)
{
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(value, rgbNewValue, value.Length);
Buffer.BlockCopy(value, 0, rgbNewValue, 0, value.Length);
Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length);
return rgbNewValue;
}
Expand All @@ -1949,7 +1949,7 @@ public byte[] Adjust(byte[] value)
if (value.Length > MaxLength && Max != MaxLength)
{
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(value, rgbNewValue, (int)MaxLength);
Buffer.BlockCopy(value, 0, rgbNewValue, 0, (int)MaxLength);
value = rgbNewValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV

length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length,
fieldOffset, buffer.Length, bufferOffset, length);
Array.Copy(value.Value, checked((int)fieldOffset), buffer, bufferOffset, length);
Buffer.BlockCopy(value.Value, checked((int)fieldOffset), buffer, bufferOffset, length);
return length;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static byte[] CreateBinaryTable(SqlConnection connection, string tableNa
while (position < data.Length)
{
int copyCount = Math.Min(pattern.Length, data.Length - position);
Array.Copy(pattern, 0, data, position, copyCount);
Buffer.BlockCopy(pattern, 0, data, position, copyCount);
position += copyCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private int ReadInternal(byte[] buffer, int offset, int count)
}
if (_errorPos >= _pos && _errorPos < _pos + nRead)
throw new CustomStreamException();
Array.Copy(_data, _pos, buffer, offset, nRead);
Buffer.BlockCopy(_data, _pos, buffer, offset, nRead);
_pos += nRead;
return nRead;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public override void Write(byte[] buffer, int offset, int count)
if (packetDataToWrite > 0)
{
// Append new data to the end of the packet data
Array.Copy(buffer, bufferWrittenPosition + offset, _outgoingPacket, OutgoingPacketHeader.Length, packetDataToWrite);
Buffer.BlockCopy(buffer, bufferWrittenPosition + offset, _outgoingPacket, OutgoingPacketHeader.Length, packetDataToWrite);

// Register that we've written new data
bufferWrittenPosition += packetDataToWrite;
Expand Down

0 comments on commit 8202268

Please sign in to comment.