async Task <int> EnsureReadAsync(byte[] buffer, int offset, int size, CancellationToken cancellationToken)
        {
            byte[] morebytes = null;
            int    nbytes    = 0;

            while (nbytes == 0 && ChunkStream.WantMore && !cancellationToken.IsCancellationRequested)
            {
                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 = await InnerStream.ReadAsync(morebytes, 0, localsize, cancellationToken).ConfigureAwait(false);

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

            return(nbytes);
        }
예제 #2
0
        // To be called on chunkedRead when we can read no data from the MonoChunkStream 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);
        }
예제 #3
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 len = buffer.Length;

            if (offset < 0 || offset > len)
            {
                throw new ArgumentOutOfRangeException("offset exceeds the size of buffer");
            }

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

            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);
        }