private int Read(DelimittedStreamReader.DelimittedReadStream caller, byte[] buffer, int offset, int count)
        {
            if (this.currentStream != caller)
            {
                return(0);
            }
            int num = this.stream.Read(buffer, offset, count);

            if (num == 0)
            {
                this.canGetNextStream = false;
                this.currentStream    = null;
                return(num);
            }
            if (this.delimitter == null)
            {
                return(num);
            }
            int num2 = this.ProcessRead(buffer, offset, num);

            if (num2 < 0)
            {
                if (this.matchBuffer == null || this.matchBuffer.Length < this.delimitter.Length - num)
                {
                    this.matchBuffer = new byte[this.delimitter.Length - num];
                }
                int count2 = this.stream.ReadBlock(this.matchBuffer, 0, this.delimitter.Length - num);
                if (this.MatchRemainder(num, count2))
                {
                    this.currentStream = null;
                    num2 = 0;
                }
                else
                {
                    this.stream.Push(this.matchBuffer, 0, count2);
                    int num3 = 1;
                    while (num3 < num && buffer[num3] != this.delimitter[0])
                    {
                        num3++;
                    }
                    if (num3 < num)
                    {
                        this.stream.Push(buffer, offset + num3, num - num3);
                    }
                    num2 = num3;
                }
            }
            return(num2);
        }
 public Stream GetNextStream(byte[] delimitterValue, int?contentLength)
 {
     if (this.currentStream != null)
     {
         this.currentStream.Dispose();// Close();
         this.currentStream = null;
     }
     if (!this.canGetNextStream)
     {
         return(null);
     }
     this.delimitter       = delimitterValue;
     this.canGetNextStream = (delimitterValue != null);
     this.currentStream    = new DelimittedStreamReader.DelimittedReadStream(this, contentLength);
     return(this.currentStream);
 }
 private void Close(DelimittedStreamReader.DelimittedReadStream caller)
 {
     if (this.currentStream == caller)
     {
         if (this.delimitter == null)
         {
             this.stream.Close();
         }
         else
         {
             if (this.scratch == null)
             {
                 this.scratch = new byte[1024];
             }
             while (this.Read(caller, this.scratch, 0, this.scratch.Length) != 0)
             {
             }
         }
         this.currentStream = null;
     }
 }
        private int ProcessRead(byte[] buffer, int offset, int read)
        {
            if (read == 0)
            {
                return(read);
            }
            int i   = offset;
            int num = offset + read;

            while (i < num)
            {
                if (buffer[i] == this.delimitter[0])
                {
                    switch (this.MatchDelimitter(buffer, i, num))
                    {
                    case DelimittedStreamReader.MatchState.True:
                    {
                        int result = i - offset;
                        i += this.delimitter.Length;
                        this.stream.Push(buffer, i, num - i);
                        this.currentStream = null;
                        return(result);
                    }

                    case DelimittedStreamReader.MatchState.InsufficientData:
                    {
                        int num2 = i - offset;
                        if (num2 > 0)
                        {
                            this.stream.Push(buffer, i, num - i);
                            return(num2);
                        }
                        return(-1);
                    }
                    }
                }
                i++;
            }
            return(read);
        }