Ejemplo n.º 1
0
        public override int Read(byte [] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            else if (offset < 0 || offset >= buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            else if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            else if (offset + count > buffer.Length)
            {
                throw new ArgumentException("sum of offset and count is larger than the buffer length");
            }

            CheckDisposed();

            while (!haveData || (bufferLength == 0 && multi.HandlesRemaining > 0))
            {
                multi.AutoPerformWithSelect();
            }

            count = Math.Min(bufferLength, count);
            if (count == 0)
            {
                return(0);
            }

            Array.Copy(this.buffer, 0, buffer, offset, count);

            if (bufferWritePosition - count > 0)
            {
                Array.Copy(this.buffer, count, this.buffer, 0, bufferWritePosition - count);
            }

            bufferWritePosition -= count;
            bufferLength        -= count;
            position            += count;

            return(count);
        }