Exemplo n.º 1
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