System.Data.SqlClient.SqlStream.Read C# (CSharp) Method

Read() public method

public Read ( byte buffer, int offset, int count ) : int
buffer byte
offset int
count int
return int
        override public int Read(byte[] buffer, int offset, int count)
        {
            int intCount = 0;
            int cBufferedData = 0;

            if ((null == _reader))
            {
                throw ADP.StreamClosed();
            }
            if (null == buffer)
            {
                throw ADP.ArgumentNull(nameof(buffer));
            }
            if ((offset < 0) || (count < 0))
            {
                throw ADP.ArgumentOutOfRange(String.Empty, (offset < 0 ? nameof(offset) : nameof(count)));
            }
            if (buffer.Length - offset < count)
            {
                throw ADP.ArgumentOutOfRange(nameof(count));
            }

            // Need to find out if we should add byte order mark or not. 
            // We need to add this if we are getting ntext xml, not if we are getting binary xml
            // Binary Xml always begins with the bytes 0xDF and 0xFF
            // If we aren't getting these, then we are getting unicode xml
            if (_bom > 0)
            {
                // Read and buffer the first two bytes
                _bufferedData = new byte[2];
                cBufferedData = ReadBytes(_bufferedData, 0, 2);
                // Check to se if we should add the byte order mark
                if ((cBufferedData < 2) || ((_bufferedData[0] == 0xDF) && (_bufferedData[1] == 0xFF)))
                {
                    _bom = 0;
                }
                while (count > 0)
                {
                    if (_bom > 0)
                    {
                        buffer[offset] = (byte)_bom;
                        _bom >>= 8;
                        offset++;
                        count--;
                        intCount++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (cBufferedData > 0)
            {
                while (count > 0)
                {
                    buffer[offset++] = _bufferedData[0];
                    intCount++;
                    count--;
                    if ((cBufferedData > 1) && (count > 0))
                    {
                        buffer[offset++] = _bufferedData[1];
                        intCount++;
                        count--;
                        break;
                    }
                }
                _bufferedData = null;
            }

            intCount += ReadBytes(buffer, offset, count);

            return intCount;
        }