Ejemplo n.º 1
0
        internal static int ReadBytesFromSocketBuffer(byte[] buffer, int offset, int count, SocketBuffer socketBuffer)
        {
            int byteToRead = (int)socketBuffer.UnreadBytes > count ? count : (int)socketBuffer.UnreadBytes;

            Buffer.BlockCopy(socketBuffer.Buffer, (int)socketBuffer.ReadIndex, buffer, offset, byteToRead);
            socketBuffer.BytesRead(byteToRead);
            return(byteToRead);
        }
Ejemplo n.º 2
0
        internal static byte[] ReadBytesFromSocketBuffer(int count, SocketBuffer socketBuffer)
        {
            if (count > socketBuffer.UnreadBytes)
            {
                throw new Exception("Did not find enough data to read.");
            }
            var retBytes = new byte[count];

            Buffer.BlockCopy(socketBuffer.Buffer, (int)socketBuffer.ReadIndex, retBytes, 0, count);
            socketBuffer.BytesRead(count);
            return(retBytes);
        }
Ejemplo n.º 3
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
            case SeekOrigin.Begin:
            {
                if (offset < 0 || _commandLength < offset)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }

                _position        = offset;
                _currBufferIndex = 0;

                for (int i = 0; i < _buffers.Count; i++)
                {
                    if (i == 0)
                    {
                        _buffers[i].BytesRead(_commandStart - (int)_buffers[i].ReadIndex);
                    }
                    else
                    {
                        _buffers[i].BytesRead(0 - _buffers[i].ReadIndex);
                    }
                }

                while (offset > 0)
                {
                    SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                    if (currentBuffer.UnreadBytes < offset)
                    {
                        offset -= currentBuffer.UnreadBytes;
                        currentBuffer.BytesRead(currentBuffer.UnreadBytes);
                        _currBufferIndex++;
                    }
                    else
                    {
                        currentBuffer.BytesRead(offset);
                    }
                }
                break;
            }

            case SeekOrigin.Current:
            {
                long tempPosition = unchecked (_position + offset);
                if (tempPosition < 0 || tempPosition > _commandLength)
                {
                    throw new IOException("IO.IO_SeekBeforeBegin");
                }

                _position = tempPosition;

                if (offset < 0)
                {
                    while (offset < 0)
                    {
                        SocketBuffer currentBuffer = _buffers[_currBufferIndex];
                        currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);

                        if (currentBuffer.ReadIndex + offset < 0)
                        {
                            offset += currentBuffer.ReadIndex;
                            currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);
                            _currBufferIndex--;
                        }
                        else
                        {
                            currentBuffer.BytesRead(offset);
                        }
                    }
                }
                else
                {
                    while (offset > 0)
                    {
                        SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                        if (currentBuffer.UnreadBytes < offset)
                        {
                            offset -= currentBuffer.UnreadBytes;
                            currentBuffer.BytesRead(currentBuffer.UnreadBytes);
                            _currBufferIndex++;
                        }
                        else
                        {
                            currentBuffer.BytesRead(offset);
                        }
                    }
                }

                break;
            }

            case SeekOrigin.End:
            {
                if (offset > 0 || _commandLength < -1 * offset)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }

                _currBufferIndex = _buffers.Count - 1;
                _position        = _commandLength + offset;

                for (int i = _buffers.Count - 1; i >= 0; i--)
                {
                    if (i == _buffers.Count - 1)
                    {
                        _buffers[i].BytesRead(_commandEnd - _buffers[i].ReadIndex);
                    }
                    else
                    {
                        _buffers[i].BytesRead(_buffers[i].UnreadBytes);
                    }
                }

                while (offset < 0)
                {
                    SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                    if (currentBuffer.ReadIndex + offset < 0)
                    {
                        offset += currentBuffer.ReadIndex;
                        currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);
                        _currBufferIndex--;
                    }
                    else
                    {
                        currentBuffer.BytesRead(offset);
                    }
                }

                break;
            }

            default: throw new ArgumentException("Argument_InvalidSeekOrigin");
            }

            return(_position);
        }