コード例 #1
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }

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

            int len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), SR.offset_out_of_range);
            }

            if (count < 0 || offset > len - count)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.offset_out_of_range);
            }

            HttpStreamAsyncResult ares = new HttpStreamAsyncResult();

            ares._callback = cback;
            ares._state    = state;
            if (_no_more_data)
            {
                ares.Complete();
                return(ares);
            }
            int 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)
            {
                _no_more_data = nread == 0;
                ares._count   = nread;
                ares.Complete();
                return(ares);
            }
            ares._buffer = new byte[8192];
            ares._offset = 0;
            ares._count  = 8192;
            ReadBufferState rb = new ReadBufferState(buffer, offset, count, ares);

            rb.InitialCount += nread;
            base.BeginRead(ares._buffer, ares._offset, ares._count, OnRead, rb);
            return(ares);
        }
コード例 #2
0
        public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback cback, object state)
        {
            if (disposed)
            {
                throw new ObjectDisposedException(GetType().ToString());
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            int num = buffer.Length;

            if (offset < 0 || offset > num)
            {
                throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
            }
            if (count < 0 || offset > num - count)
            {
                throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer");
            }
            HttpStreamAsyncResult httpStreamAsyncResult = new HttpStreamAsyncResult();

            httpStreamAsyncResult.Callback = cback;
            httpStreamAsyncResult.State    = state;
            if (no_more_data)
            {
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            int num2 = decoder.Read(buffer, offset, count);

            offset += num2;
            count  -= num2;
            if (count == 0)
            {
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            if (!decoder.WantMore)
            {
                no_more_data = (num2 == 0);
                httpStreamAsyncResult.Count = num2;
                httpStreamAsyncResult.Complete();
                return(httpStreamAsyncResult);
            }
            httpStreamAsyncResult.Buffer = new byte[8192];
            httpStreamAsyncResult.Offset = 0;
            httpStreamAsyncResult.Count  = 8192;
            ReadBufferState readBufferState = new ReadBufferState(buffer, offset, count, httpStreamAsyncResult);

            readBufferState.InitialCount += num2;
            base.BeginRead(httpStreamAsyncResult.Buffer, httpStreamAsyncResult.Offset, httpStreamAsyncResult.Count, (AsyncCallback)OnRead, (object)readBufferState);
            return(httpStreamAsyncResult);
        }
コード例 #3
0
ファイル: WebConnection.cs プロジェクト: Hengle/JellyTerain
        private int EnsureRead(byte[] buffer, int offset, int size)
        {
            byte[] array = null;
            int    i;

            for (i = 0; i == 0; i += chunkStream.Read(buffer, offset + i, size - i))
            {
                if (!chunkStream.WantMore)
                {
                    break;
                }
                int num = chunkStream.ChunkLeft;
                if (num <= 0)
                {
                    num = 1024;
                }
                else if (num > 16384)
                {
                    num = 16384;
                }
                if (array == null || array.Length < num)
                {
                    array = new byte[num];
                }
                int num2 = nstream.Read(array, 0, num);
                if (num2 <= 0)
                {
                    return(0);
                }
                chunkStream.Write(array, 0, num2);
            }
            return(i);
        }
コード例 #4
0
        // To be called on chunkedRead when we can read no data from the ChunkStream yet
        int EnsureRead(byte [] buffer, int offset, int size)
        {
            byte [] morebytes = null;
            int     nbytes    = 0;

            while (nbytes == 0 && chunkStream.WantMore)
            {
                int localsize = chunkStream.ChunkLeft;
                if (localsize <= 0)                 // not read chunk size yet
                {
                    localsize = 1024;
                }
                else if (localsize > 16384)
                {
                    localsize = 16384;
                }

                if (morebytes == null || morebytes.Length < localsize)
                {
                    morebytes = new byte [localsize];
                }

                int nread = nstream.Read(morebytes, 0, localsize);
                if (nread <= 0)
                {
                    return(0);                    // Error
                }
                chunkStream.Write(morebytes, 0, nread);
                nbytes += chunkStream.Read(buffer, offset + nbytes, size - nbytes);
            }

            return(nbytes);
        }
コード例 #5
0
        protected override IAsyncResult BeginReadCore(byte[] buffer, int offset, int size, AsyncCallback cback, object state)
        {
            HttpStreamAsyncResult ares = new HttpStreamAsyncResult(this);

            ares._callback = cback;
            ares._state    = state;
            if (_no_more_data || size == 0 || _closed)
            {
                ares.Complete();
                return(ares);
            }
            int nread = _decoder.Read(buffer, offset, size);

            offset += nread;
            size   -= nread;
            if (size == 0)
            {
                // got all we wanted, no need to bother the decoder yet
                ares._count = nread;
                ares.Complete();
                return(ares);
            }
            if (!_decoder.WantMore)
            {
                _no_more_data = nread == 0;
                ares._count   = nread;
                ares.Complete();
                return(ares);
            }
            ares._buffer = new byte[8192];
            ares._offset = 0;
            ares._count  = 8192;
            ReadBufferState rb = new ReadBufferState(buffer, offset, size, ares);

            rb.InitialCount += nread;
            base.BeginReadCore(ares._buffer, ares._offset, ares._count, OnRead, rb);
            return(ares);
        }