ParseBuffer() private method

private ParseBuffer ( byte buffer, int bytesReady, int &bytesConsumed, bool isFinal ) : ParserState
buffer byte
bytesReady int
bytesConsumed int
isFinal bool
return ParserState
        internal static ParserState ParseBufferInSteps(
            FormUrlEncodedParser parser,
            byte[] buffer,
            int readsize,
            out int totalBytesConsumed
            )
        {
            ParserState state = ParserState.Invalid;

            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int    size        = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(
                    parseBuffer,
                    parseBuffer.Length,
                    ref bytesConsumed,
                    totalBytesConsumed == buffer.Length - size
                    );
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return(state);
                }
            }

            return(state);
        }
        public void ParseBufferThrowsOnNullBuffer()
        {
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(128, out collection);
            int bytesConsumed           = 0;

            Assert.ThrowsArgumentNull(() => { parser.ParseBuffer(null, 0, ref bytesConsumed, false); }, "buffer");
        }
        public void HeaderParserDataTooBig()
        {
            byte[] data = CreateBuffer("N=V");
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(MinMessageSize, out collection);

            int         bytesConsumed = 0;
            ParserState state         = parser.ParseBuffer(data, data.Length, ref bytesConsumed, true);

            Assert.Equal(ParserState.DataTooBig, state);
            Assert.Equal(MinMessageSize, bytesConsumed);
        }
        public void ParseBufferHandlesEmptyBuffer()
        {
            byte[] data = CreateBuffer();
            ICollection <KeyValuePair <string, string> > collection;
            FormUrlEncodedParser parser = CreateParser(MinMessageSize, out collection);

            int         bytesConsumed = 0;
            ParserState state         = parser.ParseBuffer(data, data.Length, ref bytesConsumed, true);

            Assert.Equal(ParserState.Done, state);
            Assert.Equal(data.Length, bytesConsumed);
            Assert.Equal(0, collection.Count());
        }
        internal static ParserState ParseBufferInSteps(FormUrlEncodedParser parser, byte[] buffer, int readsize, out int totalBytesConsumed)
        {
            ParserState state = ParserState.Invalid;
            totalBytesConsumed = 0;
            while (totalBytesConsumed <= buffer.Length)
            {
                int size = Math.Min(buffer.Length - totalBytesConsumed, readsize);
                byte[] parseBuffer = new byte[size];
                Buffer.BlockCopy(buffer, totalBytesConsumed, parseBuffer, 0, size);

                int bytesConsumed = 0;
                state = parser.ParseBuffer(parseBuffer, parseBuffer.Length, ref bytesConsumed, totalBytesConsumed == buffer.Length - size);
                totalBytesConsumed += bytesConsumed;

                if (state != ParserState.NeedMoreData)
                {
                    return state;
                }
            }

            return state;
        }
        /// <summary>
        /// Reads all name-value pairs encoded as HTML Form URL encoded data and add them to 
        /// a collection as UNescaped URI strings.
        /// </summary>
        /// <param name="input">Stream to read from.</param>
        /// <param name="bufferSize">Size of the buffer used to read the contents.</param>
        /// <returns>Collection of name-value pairs.</returns>
        private static IEnumerable<KeyValuePair<string, string>> ReadFormUrlEncoded(Stream input, int bufferSize)
        {
            Contract.Assert(input != null, "input stream cannot be null");
            Contract.Assert(bufferSize >= MinBufferSize, "buffer size cannot be less than MinBufferSize");

            byte[] data = new byte[bufferSize];

            int bytesRead;
            bool isFinal = false;
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();
            FormUrlEncodedParser parser = new FormUrlEncodedParser(result, Int64.MaxValue);
            ParserState state;

            while (true)
            {
                try
                {
                    bytesRead = input.Read(data, 0, data.Length);
                    if (bytesRead == 0)
                    {
                        isFinal = true;
                    }
                }
                catch (Exception e)
                {
                    throw Error.InvalidOperation(e, Properties.Resources.ErrorReadingFormUrlEncodedStream);
                }

                int bytesConsumed = 0;
                state = parser.ParseBuffer(data, bytesRead, ref bytesConsumed, isFinal);
                if (state != ParserState.NeedMoreData && state != ParserState.Done)
                {
                    throw Error.InvalidOperation(Properties.Resources.FormUrlEncodedParseError, bytesConsumed);
                }

                if (isFinal)
                {
                    return result;
                }
            }
        }