示例#1
0
 public ReadBufferState(
     byte[] buffer, int offset, int count, HttpStreamAsyncResult asyncResult)
 {
     _buffer       = buffer;
     _offset       = offset;
     _count        = count;
     _initialCount = count;
     _asyncResult  = asyncResult;
 }
        public override IAsyncResult BeginRead(
            byte[] buffer, int offset, int count, AsyncCallback callback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", "A negative value.");
            }

            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", "A negative value.");
            }

            var len = buffer.Length;

            if (offset + count > len)
            {
                throw new ArgumentException(
                          "The sum of 'offset' and 'count' is greater than 'buffer' length.");
            }

            var ares = new HttpStreamAsyncResult(callback, state);

            if (_noMoreData)
            {
                ares.Complete();
                return(ares);
            }

            var nread = _decoder.Read(buffer, offset, count);

            offset += nread;
            count  -= nread;
            if (count == 0)
            {
                // Got all we wanted, no need to bother the decoder yet.
                ares.Count = nread;
                ares.Complete();

                return(ares);
            }

            if (!_decoder.WantMore)
            {
                _noMoreData = nread == 0;
                ares.Count  = nread;
                ares.Complete();

                return(ares);
            }

            ares.Buffer = new byte[_bufferLength];
            ares.Offset = 0;
            ares.Count  = _bufferLength;

            var rstate = new ReadBufferState(buffer, offset, count, ares);

            rstate.InitialCount += nread;
            base.BeginRead(ares.Buffer, ares.Offset, ares.Count, onRead, rstate);

            return(ares);
        }