BufferCopy() static private method

static private BufferCopy ( byte source, int srcOffset, byte dest, int destOffset, int count ) : void
source byte
srcOffset int
dest byte
destOffset int
count int
return void
        protected byte[] ReadToByte(byte b, ValidateByteDelegate validator)
        {
            byte[] dest = null;
            if (this._dataCount == 0)
            {
                this.BufferMoreData();
            }
            int  num       = this._dataOffset + this._dataCount;
            int  srcOffset = this._dataOffset;
            int  index     = srcOffset;
            bool flag      = false;

            while (!flag)
            {
                bool flag2 = index == num;
                flag = !flag2 && (this._dataBuffer[index] == b);
                if (((validator != null) && !flag2) && (!flag && !validator(this._dataBuffer[index])))
                {
                    throw new RemotingException(CoreChannel.GetResourceString("Remoting_Http_InvalidDataReceived"));
                }
                if (flag2 || flag)
                {
                    int count = index - srcOffset;
                    if (dest == null)
                    {
                        dest = new byte[count];
                        StreamHelper.BufferCopy(this._dataBuffer, srcOffset, dest, 0, count);
                    }
                    else
                    {
                        int    length  = dest.Length;
                        byte[] buffer2 = new byte[length + count];
                        StreamHelper.BufferCopy(dest, 0, buffer2, 0, length);
                        StreamHelper.BufferCopy(this._dataBuffer, srcOffset, buffer2, length, count);
                        dest = buffer2;
                    }
                    this._dataOffset += count;
                    this._dataCount  -= count;
                    if (flag2)
                    {
                        this.BufferMoreData();
                        num       = this._dataOffset + this._dataCount;
                        srcOffset = this._dataOffset;
                        index     = srcOffset;
                    }
                    else if (flag)
                    {
                        this._dataOffset++;
                        this._dataCount--;
                    }
                }
                else
                {
                    index++;
                }
            }
            return(dest);
        }
Example #2
0
        } // ReadAndMatchFourBytes

        public int Read(byte[] buffer, int offset, int count)
        {
            int totalBytesRead = 0;

            byte[] dataBuffer = this.DataBuffer;

            // see if we have buffered data
            if (_dataCount > 0)
            {
                // copy minimum of buffered data size and bytes left to read
                int readCount = Math.Min(_dataCount, count);
                StreamHelper.BufferCopy(dataBuffer, _dataOffset, buffer, offset, readCount);
                _dataCount     -= readCount;
                _dataOffset    += readCount;
                count          -= readCount;
                offset         += readCount;
                totalBytesRead += readCount;
            }

            // keep reading (whoever is calling this will make sure that they
            //   don't try to read too much).
            while (count > 0)
            {
                if (count < 256)
                {
                    // if count is less than 256 bytes, I will buffer more data
                    // because it's not worth making a socket request for less.
                    BufferMoreData(dataBuffer);

                    // copy minimum of buffered data size and bytes left to read
                    int readCount = Math.Min(_dataCount, count);
                    StreamHelper.BufferCopy(dataBuffer, _dataOffset, buffer, offset, readCount);
                    _dataCount     -= readCount;
                    _dataOffset    += readCount;
                    count          -= readCount;
                    offset         += readCount;
                    totalBytesRead += readCount;
                }
                else
                {
                    // just go directly to the socket

                    // the internal buffer is guaranteed to be empty at this point, so just
                    //   read directly into the array given

                    int readCount = ReadFromSocket(buffer, offset, count);
                    count          -= readCount;
                    offset         += readCount;
                    totalBytesRead += readCount;
                }
            }

            return(totalBytesRead);
        } // Read
        public int Read(byte[] buffer, int offset, int count)
        {
            int num = 0;

            if (this._dataCount > 0)
            {
                int num2 = Math.Min(this._dataCount, count);
                StreamHelper.BufferCopy(this._dataBuffer, this._dataOffset, buffer, offset, num2);
                this._dataCount  -= num2;
                this._dataOffset += num2;
                count            -= num2;
                offset           += num2;
                num += num2;
            }
            while (count > 0)
            {
                if (count < 0x100)
                {
                    this.BufferMoreData();
                    int num3 = Math.Min(this._dataCount, count);
                    StreamHelper.BufferCopy(this._dataBuffer, this._dataOffset, buffer, offset, num3);
                    this._dataCount  -= num3;
                    this._dataOffset += num3;
                    count            -= num3;
                    offset           += num3;
                    num += num3;
                }
                else
                {
                    int num4 = this.ReadFromSocket(buffer, offset, count);
                    count  -= num4;
                    offset += num4;
                    num    += num4;
                }
            }
            return(num);
        }
Example #4
0
        } /// ReadToByte

        protected byte[] ReadToByte(byte b, ValidateByteDelegate validator)
        {
            byte[] readBytes = null;

            // start at current position and return byte array consisting of bytes
            //   up to where we found the byte.
            if (_dataCount == 0)
            {
                BufferMoreData();
            }

            int dataEnd    = _dataOffset + _dataCount; // one byte past last valid byte
            int startIndex = _dataOffset;              // current position
            int endIndex   = startIndex;               // current index

            bool foundByte = false;
            bool bufferEnd;

            while (!foundByte)
            {
                InternalRemotingServices.RemotingAssert(endIndex <= dataEnd, "endIndex shouldn't pass dataEnd");
                bufferEnd = endIndex == dataEnd;
                foundByte = !bufferEnd && (_dataBuffer[endIndex] == b);

                // validate character if necessary
                if ((validator != null) && !bufferEnd && !foundByte)
                {
                    if (!validator(_dataBuffer[endIndex]))
                    {
                        throw new RemotingException(
                                  CoreChannel.GetResourceString(
                                      "Remoting_Http_InvalidDataReceived"));
                    }
                }

                // we're at the end of the currently buffered data or we've found our byte
                if (bufferEnd || foundByte)
                {
                    // store processed byte in the readBytes array
                    int count = endIndex - startIndex;
                    if (readBytes == null)
                    {
                        readBytes = new byte[count];
                        StreamHelper.BufferCopy(_dataBuffer, startIndex, readBytes, 0, count);
                    }
                    else
                    {
                        int    oldSize  = readBytes.Length;
                        byte[] newBytes = new byte[oldSize + count];
                        StreamHelper.BufferCopy(readBytes, 0, newBytes, 0, oldSize);
                        StreamHelper.BufferCopy(_dataBuffer, startIndex, newBytes, oldSize, count);
                        readBytes = newBytes;
                    }

                    // update data counters
                    _dataOffset += count;
                    _dataCount  -= count;

                    if (bufferEnd)
                    {
                        // we still haven't found the byte, so buffer more data
                        //   and keep looking.
                        BufferMoreData();

                        // reset indices
                        dataEnd    = _dataOffset + _dataCount; // last valid byte
                        startIndex = _dataOffset;              // current position
                        endIndex   = startIndex;               // current index
                    }
                    else
                    if (foundByte)
                    {
                        // skip over the byte that we were looking for
                        _dataOffset += 1;
                        _dataCount  -= 1;
                    }
                }
                else
                {
                    // still haven't found character or end of buffer, so advance position
                    endIndex++;
                }
            }

            return(readBytes);
        } // ReadToByte