diff --git a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs index 4c0620ba7b8c..1555a8218d40 100644 --- a/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -89,71 +89,51 @@ public override Task ReadAsync(byte[] buffer, int offset, int count, Cancel /// private async Task ReadInternal(byte[] buffer, int offset, int count, CancellationToken token, bool async) { + int readBytes = 0; + byte[] packetData = null; + byte[] readTarget = buffer; + int readOffset = offset; if (_encapsulate) { - return await ReadInternalEncapsulate(buffer, offset, count, token, async).ConfigureAwait(false); - } - else if (async) - { - return await ReadInternalAsync(buffer, offset, count, token).ConfigureAwait(false); - } - else - { - return ReadInternalSync(buffer, offset, count); - } - } + packetData = ArrayPool.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count); + readTarget = packetData; + readOffset = 0; + if (_packetBytes == 0) + { + // Account for split packets + while (readBytes < TdsEnums.HEADER_LEN) + { + readBytes += async ? + await _stream.ReadAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) : + _stream.Read(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes); + } - private async Task ReadInternalEncapsulate(byte[] buffer, int offset, int count, CancellationToken token, bool async) - { - int readBytes = 0; - byte[] packetData = ArrayPool.Shared.Rent(count < TdsEnums.HEADER_LEN ? TdsEnums.HEADER_LEN : count); + _packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]; + _packetBytes -= TdsEnums.HEADER_LEN; + } - if (_packetBytes == 0) - { - // Account for split packets - while (readBytes < TdsEnums.HEADER_LEN) + if (count > _packetBytes) { - readBytes += (async ? - await ReadInternalAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) : - ReadInternalSync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes) - ); + count = _packetBytes; } - - _packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1]; - _packetBytes -= TdsEnums.HEADER_LEN; } - if (count > _packetBytes) + readBytes = async ? + await _stream.ReadAsync(readTarget, readOffset, count, token).ConfigureAwait(false) : + _stream.Read(readTarget, readOffset, count); + + if (_encapsulate) { - count = _packetBytes; + _packetBytes -= readBytes; + } + if (packetData != null) + { + Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes); + ArrayPool.Shared.Return(packetData, clearArray: true); } - - readBytes = (async ? - await ReadInternalAsync(packetData, 0, count, token).ConfigureAwait(false) : - ReadInternalSync(packetData, 0, count) - ); - - - _packetBytes -= readBytes; - - Buffer.BlockCopy(packetData, 0, buffer, offset, readBytes); - - Array.Clear(packetData, 0, readBytes); - ArrayPool.Shared.Return(packetData, clearArray: false); - return readBytes; } - private async Task ReadInternalAsync(byte[] buffer, int offset, int count, CancellationToken token) - { - return await _stream.ReadAsync(buffer, 0, count, token).ConfigureAwait(false); - } - - private int ReadInternalSync(byte[] buffer, int offset, int count) - { - return _stream.Read(buffer, 0, count); - } - /// /// The internal write method calls Sync APIs when Async flag is false ///