Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Use Buffer.BlockCopy with byte[]s #6103

Merged
merged 1 commit into from
Feb 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Common/src/System/Net/Logging/GlobalLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static void Dump(byte[] buffer, int offset, int length)
}

var bufferSegment = new byte[length];
Array.Copy(buffer, offset, bufferSegment, 0, length);
Buffer.BlockCopy(buffer, offset, bufferSegment, 0, length);
EventSourceLogging.Log.DebugDumpArray(bufferSegment);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Common/src/System/Security/Cryptography/RSAOpenSsl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
}

byte[] plainBytes = new byte[returnValue];
Array.Copy(buf, 0, plainBytes, 0, returnValue);
Buffer.BlockCopy(buf, 0, plainBytes, 0, returnValue);
return plainBytes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public TypeArray AllocParams(params CType[] types)
public TypeArray ConcatParams(CType[] prgtype1, CType[] prgtype2)
{
CType[] combined = new CType[prgtype1.Length + prgtype2.Length];
Array.Copy(prgtype1, combined, prgtype1.Length);
Array.Copy(prgtype1, 0, combined, 0, prgtype1.Length);
Array.Copy(prgtype2, 0, combined, prgtype1.Length, prgtype2.Length);
return AllocParams(combined);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public UIHintImplementation(string uiHint, string presentationLayer, params obje
if (controlParameters != null)
{
_inputControlParameters = new object[controlParameters.Length];
Array.Copy(controlParameters, _inputControlParameters, controlParameters.Length);
Array.Copy(controlParameters, 0, _inputControlParameters, 0, controlParameters.Length);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public override long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIn
}

// until arrays are 64 bit, we have to do these casts
Array.Copy(data, ndataIndex, buffer, bufferIndex, (int)cbytes);
Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, (int)cbytes);
}
catch (Exception e)
{
Expand Down
10 changes: 5 additions & 5 deletions src/System.Data.SqlClient/src/System/Data/Sql/SqlMetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public SqlBinary Adjust(SqlBinary value)
{
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(rgbValue, 0, rgbNewValue, 0, 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 @@ -963,7 +963,7 @@ public SqlBinary Adjust(SqlBinary value)
{
byte[] rgbValue = value.Value;
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(rgbValue, 0, rgbNewValue, 0, (int)MaxLength);
Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, (int)MaxLength);
value = new SqlBinary(rgbNewValue);
}

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

Expand Down Expand Up @@ -1380,7 +1380,7 @@ public byte[] Adjust(byte[] value)
if (value.Length < MaxLength)
{
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(value, 0, rgbNewValue, 0, value.Length);
Buffer.BlockCopy(value, 0, rgbNewValue, 0, value.Length);
Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length);
return rgbNewValue;
}
Expand All @@ -1400,7 +1400,7 @@ public byte[] Adjust(byte[] value)
if (value.Length > MaxLength && Max != MaxLength)
{
byte[] rgbNewValue = new byte[MaxLength];
Array.Copy(value, 0, rgbNewValue, 0, (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 @@ public SNIPacket Clone()
{
SNIPacket packet = new SNIPacket(null);
packet._data = new byte[_length];
Array.Copy(_data, 0, packet._data, 0, _length);
Buffer.BlockCopy(_data, 0, packet._data, 0, _length);
packet._length = _length;

return packet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,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 @@ -371,7 +371,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 @@ -410,7 +410,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 @@ -194,7 +194,7 @@ public byte[] Value

default:
buffer = new byte[_lCurLen];
Array.Copy(m_rgbBuf, buffer, (int)_lCurLen);
System.Buffer.BlockCopy(m_rgbBuf, 0, buffer, 0, (int)_lCurLen);
break;
}

Expand Down Expand Up @@ -343,7 +343,7 @@ public long Read(long offset, byte[] buffer, int offsetInBuffer, int count)
default:
// ProjectK\Core doesn't support long-typed array indexers
Debug.Assert(offset < int.MaxValue);
Array.Copy(m_rgbBuf, checked((int)offset), buffer, offsetInBuffer, count);
System.Buffer.BlockCopy(m_rgbBuf, checked((int)offset), buffer, offsetInBuffer, count);
break;
}
}
Expand Down Expand Up @@ -409,7 +409,7 @@ public void Write(long offset, byte[] buffer, int offsetInBuffer, int count)
{
// ProjectK\Core doesn't support long-typed array indexers
Debug.Assert(offset < int.MaxValue);
Array.Copy(buffer, offsetInBuffer, m_rgbBuf, checked((int)offset), count);
System.Buffer.BlockCopy(buffer, offsetInBuffer, m_rgbBuf, checked((int)offset), count);

// If the last position that has been written is after
// the current data length, reset the length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public char[] Value

default:
buffer = new char[_lCurLen];
Array.Copy(m_rgchBuf, buffer, (int)_lCurLen);
Array.Copy(m_rgchBuf, 0, buffer, 0, (int)_lCurLen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you leave this out to use Array.Copy intentionally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was only changing those using byte[] to use BlockCopy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK.

break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private static void EnsureEventSourceIndexAvailable(int eventSourceIndex)
else if (eventSourceIndex >= EventCounterGroup.s_eventCounterGroups.Length)
{
EventCounterGroup[] newEventCounterGroups = new EventCounterGroup[eventSourceIndex + 1];
Array.Copy(EventCounterGroup.s_eventCounterGroups, newEventCounterGroups, EventCounterGroup.s_eventCounterGroups.Length);
Array.Copy(EventCounterGroup.s_eventCounterGroups, 0, newEventCounterGroups, 0, EventCounterGroup.s_eventCounterGroups.Length);
EventCounterGroup.s_eventCounterGroups = newEventCounterGroups;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3645,7 +3645,7 @@ private static void AddProviderEnumKind(ManifestBuilder manifest, FieldInfo stat
if (eventData == null || eventData.Length <= eventAttribute.EventId)
{
EventMetadata[] newValues = new EventMetadata[Math.Max(eventData.Length + 16, eventAttribute.EventId + 1)];
Array.Copy(eventData, newValues, eventData.Length);
Array.Copy(eventData, 0, newValues, 0, eventData.Length);
eventData = newValues;
}

Expand Down Expand Up @@ -3684,7 +3684,7 @@ private static void TrimEventDescriptors(ref EventMetadata[] eventData)
if (eventData.Length - idx > 2) // allow one wasted slot.
{
EventMetadata[] newValues = new EventMetadata[idx + 1];
Array.Copy(eventData, newValues, newValues.Length);
Array.Copy(eventData, 0, newValues, 0, newValues.Length);
eventData = newValues;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/System.IO/src/System/IO/BufferedStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void EnsureShadowBufferAllocated()
return;

byte[] shadowBuffer = new byte[Math.Min(_bufferSize + _bufferSize, MaxShadowBufferSize)];
Array.Copy(_buffer, 0, shadowBuffer, 0, _writePos);
Buffer.BlockCopy(_buffer, 0, shadowBuffer, 0, _writePos);
_buffer = shadowBuffer;
}

Expand Down Expand Up @@ -416,7 +416,7 @@ private int ReadFromBuffer(byte[] array, int offset, int count)

if (readbytes > count)
readbytes = count;
Array.Copy(_buffer, _readPos, array, offset, readbytes);
Buffer.BlockCopy(_buffer, _readPos, array, offset, readbytes);
_readPos += readbytes;

return readbytes;
Expand Down Expand Up @@ -683,7 +683,7 @@ private void WriteToBuffer(byte[] array, ref int offset, ref int count)
return;

EnsureBufferAllocated();
Array.Copy(array, offset, _buffer, _writePos, bytesToWrite);
Buffer.BlockCopy(array, offset, _buffer, _writePos, bytesToWrite);

_writePos += bytesToWrite;
count -= bytesToWrite;
Expand Down Expand Up @@ -825,7 +825,7 @@ public override void Write(byte[] array, int offset, int count)
if (totalUserbytes <= (_bufferSize + _bufferSize) && totalUserbytes <= MaxShadowBufferSize)
{
EnsureShadowBufferAllocated();
Array.Copy(array, offset, _buffer, _writePos, count);
Buffer.BlockCopy(array, offset, _buffer, _writePos, count);
_stream.Write(_buffer, 0, totalUserbytes);
_writePos = 0;
return;
Expand Down Expand Up @@ -977,7 +977,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
if (totalUserBytes <= (_bufferSize + _bufferSize) && totalUserBytes <= MaxShadowBufferSize)
{
EnsureShadowBufferAllocated();
Array.Copy(array, offset, _buffer, _writePos, count);
Buffer.BlockCopy(array, offset, _buffer, _writePos, count);

await _stream.WriteAsync(_buffer, 0, totalUserBytes, cancellationToken).ConfigureAwait(false);
_writePos = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public override Task<int> ReadAsync(byte[] buffer, int offset, int count, Cancel
if (_remainingDataCount > 0)
{
int bytesToCopy = Math.Min(count, _remainingDataCount);
Array.Copy(_remainingData, _remainingDataOffset, buffer, offset, bytesToCopy);
Buffer.BlockCopy(_remainingData, _remainingDataOffset, buffer, offset, bytesToCopy);

_remainingDataOffset += bytesToCopy;
_remainingDataCount -= bytesToCopy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ public override PhysicalAddress GetPhysicalAddress()
{
byte[] newAddr = new byte[_addressLength];

// Array.Copy only supports int and long while addressLength is uint (see IpAdapterAddresses).
// Buffer.BlockCopy only supports int while addressLength is uint (see IpAdapterAddresses).
// Will throw OverflowException if addressLength > Int32.MaxValue.
Array.Copy(_physicalAddress, 0, newAddr, 0, checked((int)_addressLength));
Buffer.BlockCopy(_physicalAddress, 0, newAddr, 0, checked((int)_addressLength));
return new PhysicalAddress(newAddr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private async Task<PingReply> SendIcmpEchoRequestOverRawSocket(IPAddress address
int dataOffset = ipHeaderLength + IcmpHeaderLengthInBytes;
// We want to return a buffer with the actual data we sent out, not including the header data.
byte[] dataBuffer = new byte[bytesReceived - dataOffset];
Array.Copy(receiveBuffer, dataOffset, dataBuffer, 0, dataBuffer.Length);
Buffer.BlockCopy(receiveBuffer, dataOffset, dataBuffer, 0, dataBuffer.Length);

IPStatus status = isIpv4
? IcmpV4MessageConstants.MapV4TypeToIPStatus(type, code)
Expand Down
2 changes: 1 addition & 1 deletion src/System.Net.Sockets/src/System/Net/Sockets/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4116,7 +4116,7 @@ internal Socket EndAccept(out byte[] buffer, IAsyncResult asyncResult)

Socket socket = EndAccept(out innerBuffer, out bytesTransferred, asyncResult);
buffer = new byte[bytesTransferred];
Array.Copy(innerBuffer, 0, buffer, 0, bytesTransferred);
Buffer.BlockCopy(innerBuffer, 0, buffer, 0, bytesTransferred);
return socket;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,9 @@ private int ReadBytes(Encoding encoding, int byteBlock, int charBlock, byte[] bu
if (_trailByteCount > 0)
{
int actual = Math.Min(_trailByteCount, byteCount);
Array.Copy(_trailBytes, 0, buffer, offset, actual);
Buffer.BlockCopy(_trailBytes, 0, buffer, offset, actual);
_trailByteCount -= actual;
Array.Copy(_trailBytes, actual, _trailBytes, 0, _trailByteCount);
Buffer.BlockCopy(_trailBytes, actual, _trailBytes, 0, _trailByteCount);
return actual;
}
XmlNodeType nodeType = _node.NodeType;
Expand Down Expand Up @@ -1440,9 +1440,9 @@ private int ReadBytes(Encoding encoding, int byteBlock, int charBlock, byte[] bu
_trailBytes = new byte[3];
_trailByteCount = encoding.GetBytes(chars, 0, charCount, _trailBytes, 0);
int actual = Math.Min(_trailByteCount, byteCount);
Array.Copy(_trailBytes, 0, buffer, offset, actual);
Buffer.BlockCopy(_trailBytes, 0, buffer, offset, actual);
_trailByteCount -= actual;
Array.Copy(_trailBytes, actual, _trailBytes, 0, _trailByteCount);
Buffer.BlockCopy(_trailBytes, actual, _trailBytes, 0, _trailByteCount);
return actual;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ unsafe protected int UnsafeGetUTF8Chars(char* chars, int charCount, byte[] buffe
string tmp = new string(charsStart, 0, (int)(chars - charsStart));
byte[] newBytes = _encoding != null ? _encoding.GetBytes(tmp) : s_UTF8Encoding.GetBytes(tmp);
int toCopy = Math.Min(newBytes.Length, (int)(bytesMax - bytes));
Array.Copy(newBytes, 0, buffer, (int)(bytes - _bytes) + offset, toCopy);
Buffer.BlockCopy(newBytes, 0, buffer, (int)(bytes - _bytes) + offset, toCopy);

bytes += toCopy;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public byte[] ToArray(int start, int byteCount)
break;
}

Array.Copy(chunk._buffer, Math.Max(start - chunkStartPosition, 0), result, resultOffset, bytesToCopy);
Buffer.BlockCopy(chunk._buffer, Math.Max(start - chunkStartPosition, 0), result, resultOffset, bytesToCopy);

resultOffset += bytesToCopy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public byte[] ToArray(int start, int byteCount)
BlobUtilities.ValidateRange(Length, start, byteCount);

var result = new byte[byteCount];
Array.Copy(_buffer, _start + start, result, 0, byteCount);
Buffer.BlockCopy(_buffer, _start + start, result, 0, byteCount);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void EnsureShadowBufferAllocated()
return;

Byte[] shadowBuffer = new Byte[Math.Min(_bufferSize + _bufferSize, MaxShadowBufferSize)];
Array.Copy(_buffer, 0, shadowBuffer, 0, _writePos);
Buffer.BlockCopy(_buffer, 0, shadowBuffer, 0, _writePos);
_buffer = shadowBuffer;
}

Expand Down Expand Up @@ -485,7 +485,7 @@ private Int32 ReadFromBuffer(Byte[] array, Int32 offset, Int32 count)

if (readBytes > count)
readBytes = count;
Array.Copy(_buffer, _readPos, array, offset, readBytes);
Buffer.BlockCopy(_buffer, _readPos, array, offset, readBytes);
_readPos += readBytes;

return readBytes;
Expand Down Expand Up @@ -842,7 +842,7 @@ private void WriteToBuffer(Byte[] array, ref Int32 offset, ref Int32 count)
return;

EnsureBufferAllocated();
Array.Copy(array, offset, _buffer, _writePos, bytesToWrite);
Buffer.BlockCopy(array, offset, _buffer, _writePos, bytesToWrite);

_writePos += bytesToWrite;
count -= bytesToWrite;
Expand Down Expand Up @@ -986,7 +986,7 @@ public override void Write(Byte[] array, Int32 offset, Int32 count)
if (totalUserBytes <= (_bufferSize + _bufferSize) && totalUserBytes <= MaxShadowBufferSize)
{
EnsureShadowBufferAllocated();
Array.Copy(array, offset, _buffer, _writePos, count);
Buffer.BlockCopy(array, offset, _buffer, _writePos, count);
_stream.Write(_buffer, 0, totalUserBytes);
_writePos = 0;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static IBuffer Create(Byte[] data, Int32 offset, Int32 length, Int32 capa
Contract.EndContractBlock();

Byte[] underlyingData = new Byte[capacity];
Array.Copy(data, offset, underlyingData, 0, length);
Buffer.BlockCopy(data, offset, underlyingData, 0, length);
return new WindowsRuntimeBuffer(underlyingData, 0, length, capacity);
}

Expand Down
Loading