System.IO.BinaryReader.FillBuffer C# (CSharp) Method

FillBuffer() protected method

protected FillBuffer ( int numBytes ) : void
numBytes int
return void
        protected virtual void FillBuffer(int numBytes)
        {
            if (_buffer != null && (numBytes < 0 || numBytes > _buffer.Length))
            {
                throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_BinaryReaderFillBuffer);
            }

            int bytesRead = 0;
            int n = 0;

            if (_stream == null)
            {
                throw new ObjectDisposedException(null, SR.ObjectDisposed_FileClosed);
            }

            // Need to find a good threshold for calling ReadByte() repeatedly
            // vs. calling Read(byte[], int, int) for both buffered & unbuffered
            // streams.
            if (numBytes == 1)
            {
                n = _stream.ReadByte();
                if (n == -1)
                {
                    throw new EndOfStreamException(SR.IO_EOF_ReadBeyondEOF);
                }

                _buffer[0] = (byte)n;
                return;
            }

            do
            {
                n = _stream.Read(_buffer, bytesRead, numBytes - bytesRead);
                if (n == 0)
                {
                    throw new EndOfStreamException(SR.IO_EOF_ReadBeyondEOF);
                }
                bytesRead += n;
            } while (bytesRead < numBytes);
        }