Microsoft.WindowsAzure.Commands.Tools.Vhd.Model.Persistence.VhdDataReader.FillBuffer C# (CSharp) Method

FillBuffer() private method

private FillBuffer ( AsyncMachine machine, int numBytes ) : IEnumerable
machine AsyncMachine
numBytes int
return IEnumerable
        IEnumerable<CompletionPort> FillBuffer(AsyncMachine machine, int numBytes)
        {
            if (m_buffer != null && (numBytes < 0 || numBytes > m_buffer.Length))
            {
                throw new ArgumentOutOfRangeException("numBytes", String.Format("Expected (0-16) however found: {0}", numBytes));
            }
            int bytesRead = 0;
            int n = 0;

            // Need to find a good threshold for calling ReadByte() repeatedly 
            // vs. calling Read(byte[], int, int) for both buffered & unbuffered 
            // streams.
            if (numBytes == 1)
            {
                this.reader.BaseStream.BeginRead(m_buffer, 0, numBytes, machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                n = this.reader.BaseStream.EndRead(machine.CompletionResult);
                if (n == -1)
                {
                    throw new EndOfStreamException();
                }
                m_buffer[0] = (byte)n;
            }

            do
            {
                this.reader.BaseStream.BeginRead(m_buffer, bytesRead, numBytes - bytesRead, machine.CompletionCallback, null);
                yield return CompletionPort.SingleOperation;
                n = this.reader.BaseStream.EndRead(machine.CompletionResult);

                if (n == 0)
                {
                    throw new EndOfStreamException();
                }
                bytesRead += n;
            } while (bytesRead < numBytes);
        }