[NotNull] byte[] ReadNextChunk()
        {
            if (_complete)
            {
                return(new byte[0]);
            }
            var ms = new MemoryStream();

            // assuming we at the beginning of a chunk. Not thread safe!
            var length = ReadChunkLength(_source);

            if (length == 0)
            {
                _complete = true;
                return(new byte[0]);
            }

            var maybeNewline = _source.ReadByte();

            if (maybeNewline >= 0 && maybeNewline != '\r' && maybeNewline != '\n')
            {
                ms.WriteByte((byte)maybeNewline);
                length--;
            }

            StreamTools.CopyBytesToLength(_source, ms, length, _timeout, null);

            // Should end with CRLF:
            if (!SkipCRLF(_source))
            {
                throw new InvalidDataException("HTTP Chunk did not end in CRLF");
            }

            return(ms.ToArray());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read raw bytes up to the declared response length.
        /// If response is chunked, this will read until an empty chunk is received.
        /// </summary>
        public byte[] ReadBytesToLength(Action <long> receiveProgress = null)
        {
            var ms = new MemoryStream((int)ExpectedLength);

            lock (_lock)
            {
                ExpectedLength = StreamTools.CopyBytesToLength(_source, ms, ExpectedLength, Timeout, receiveProgress);
            }
            _readSoFar += ms.Length;
            return(ms.ToArray());
        }