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

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

            if (num2 >= 0)
            {
                return(num2);
            }
            if ((this.matchBuffer == null) || (this.matchBuffer.Length < (this.delimitter.Length - read)))
            {
                this.matchBuffer = new byte[this.delimitter.Length - read];
            }
            int num3 = this.stream.ReadBlock(this.matchBuffer, 0, this.delimitter.Length - read);

            if (this.MatchRemainder(read, num3))
            {
                this.currentStream = null;
                return(0);
            }
            this.stream.Push(this.matchBuffer, 0, num3);
            int index = 1;

            while (index < read)
            {
                if (buffer[index] == this.delimitter[0])
                {
                    break;
                }
                index++;
            }
            if (index < read)
            {
                this.stream.Push(buffer, offset + index, read - index);
            }
            return(index);
        }
 public Stream GetNextStream(byte[] delimitter)
 {
     if (this.currentStream != null)
     {
         this.currentStream.Close();
         this.currentStream = null;
     }
     if (!this.canGetNextStream)
     {
         return(null);
     }
     this.delimitter       = delimitter;
     this.canGetNextStream = delimitter != null;
     this.currentStream    = new DelimittedReadStream(this);
     return(this.currentStream);
 }
 public Stream GetNextStream(byte[] delimitter)
 {
     if (this.currentStream != null)
     {
         this.currentStream.Close();
         this.currentStream = null;
     }
     if (!this.canGetNextStream)
     {
         return null;
     }
     this.delimitter = delimitter;
     this.canGetNextStream = delimitter != null;
     this.currentStream = new DelimittedReadStream(this);
     return this.currentStream;
 }
 private void Close(DelimittedReadStream caller)
 {
     if (this.currentStream == caller)
     {
         if (this.delimitter == null)
         {
             this.stream.Close();
         }
         else
         {
             if (this.scratch == null)
             {
                 this.scratch = new byte[0x400];
             }
             while (this.Read(caller, this.scratch, 0, this.scratch.Length) != 0)
             {
             }
         }
         this.currentStream = null;
     }
 }
 private void Close(DelimittedReadStream caller)
 {
     if (this.currentStream == caller)
     {
         if (this.delimitter == null)
         {
             this.stream.Close();
         }
         else
         {
             if (this.scratch == null)
             {
                 this.scratch = new byte[0x400];
             }
             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)
            {
                int index = offset;
                int end   = offset + read;
                while (index < end)
                {
                    if (buffer[index] == this.delimitter[0])
                    {
                        switch (this.MatchDelimitter(buffer, index, end))
                        {
                        case MatchState.True:
                        {
                            int num3 = index - offset;
                            index += this.delimitter.Length;
                            this.stream.Push(buffer, index, end - index);
                            this.currentStream = null;
                            return(num3);
                        }

                        case MatchState.InsufficientData:
                        {
                            int num4 = index - offset;
                            if (num4 <= 0)
                            {
                                return(-1);
                            }
                            this.stream.Push(buffer, index, end - index);
                            return(num4);
                        }
                        }
                    }
                    index++;
                }
            }
            return(read);
        }
Пример #7
0
        int Read(DelimittedReadStream caller, byte[] buffer, int offset, int count)
        {
            if (this.currentStream != caller)
                return 0;

            int read = this.stream.Read(buffer, offset, count);
            if (read == 0)
            {
                this.canGetNextStream = false;
                this.currentStream = null;
                return read;
            }

            // If delimitter is null, read until the underlying stream returns 0 bytes
            if (this.delimitter == null)
                return read;

            // Scans the read data for the delimitter. If found, the number of bytes read are adjusted
            // to account for the number of bytes up to but not including the delimitter.
            int actual = ProcessRead(buffer, offset, read);

            if (actual < 0)
            {
                if (this.matchBuffer == null || this.matchBuffer.Length < this.delimitter.Length - read)
                    this.matchBuffer = new byte[this.delimitter.Length - read];

                int matched = this.stream.ReadBlock(this.matchBuffer, 0, this.delimitter.Length - read);

                if (MatchRemainder(read, matched))
                {
                    this.currentStream = null;
                    actual = 0;
                }
                else
                {
                    this.stream.Push(this.matchBuffer, 0, matched);

                    int i = 1;
                    for (; i < read; i++)
                    {
                        if (buffer[i] == this.delimitter[0])
                            break;
                    }

                    if (i < read)
                        this.stream.Push(buffer, offset + i, read - i);

                    actual = i;
                }
            }

            return actual;
        }
Пример #8
0
        int ProcessRead(byte[] buffer, int offset, int read)
        {
            // nothing to process if 0 bytes were read
            if (read == 0)
                return read;

            for (int ptr = offset, end = offset + read; ptr < end; ptr++)
            {
                if (buffer[ptr] == delimitter[0])
                {
                    switch (MatchDelimitter(buffer, ptr, end))
                    {
                        case MatchState.True:
                            {
                                int actual = ptr - offset;
                                ptr += this.delimitter.Length;
                                this.stream.Push(buffer, ptr, end - ptr);
                                this.currentStream = null;
                                return actual;
                            }
                        case MatchState.False:
                            break;
                        case MatchState.InsufficientData:
                            {
                                int actual = ptr - offset;
                                if (actual > 0)
                                {
                                    this.stream.Push(buffer, ptr, end - ptr);
                                    return actual;
                                }
                                else
                                {
                                    return -1;
                                }
                            }
                    }
                }
            }
            return read;
        }
Пример #9
0
        // Gets the next logical stream delimitted by the given sequence.
        public Stream GetNextStream(byte[] delimitter)
        {
            if (currentStream != null)
            {
                currentStream.Close();
                currentStream = null;
            }

            if (!canGetNextStream)
                return null;

            this.delimitter = delimitter;

            canGetNextStream = delimitter != null;

            currentStream = new DelimittedReadStream(this);

            return currentStream;
        }
Пример #10
0
        // Closes the current stream.  If the current stream is not the same as the caller, nothing is done.
        void Close(DelimittedReadStream caller)
        {
            if (currentStream == caller)
            {
                if (delimitter == null)
                {
                    stream.Close();
                }
                else
                {
                    if (scratch == null)
                    {
                        scratch = new byte[1024];
                    }
                    while (0 != Read(caller, this.scratch, 0, this.scratch.Length));
                }

                currentStream = null;
            }
        }
 private int Read(DelimittedReadStream caller, byte[] buffer, int offset, int count)
 {
     if (this.currentStream != caller)
     {
         return 0;
     }
     int read = this.stream.Read(buffer, offset, count);
     if (read == 0)
     {
         this.canGetNextStream = false;
         this.currentStream = null;
         return read;
     }
     if (this.delimitter == null)
     {
         return read;
     }
     int num2 = this.ProcessRead(buffer, offset, read);
     if (num2 >= 0)
     {
         return num2;
     }
     if ((this.matchBuffer == null) || (this.matchBuffer.Length < (this.delimitter.Length - read)))
     {
         this.matchBuffer = new byte[this.delimitter.Length - read];
     }
     int num3 = this.stream.ReadBlock(this.matchBuffer, 0, this.delimitter.Length - read);
     if (this.MatchRemainder(read, num3))
     {
         this.currentStream = null;
         return 0;
     }
     this.stream.Push(this.matchBuffer, 0, num3);
     int index = 1;
     while (index < read)
     {
         if (buffer[index] == this.delimitter[0])
         {
             break;
         }
         index++;
     }
     if (index < read)
     {
         this.stream.Push(buffer, offset + index, read - index);
     }
     return index;
 }
 private int ProcessRead(byte[] buffer, int offset, int read)
 {
     if (read != 0)
     {
         int index = offset;
         int end = offset + read;
         while (index < end)
         {
             if (buffer[index] == this.delimitter[0])
             {
                 switch (this.MatchDelimitter(buffer, index, end))
                 {
                     case MatchState.True:
                     {
                         int num3 = index - offset;
                         index += this.delimitter.Length;
                         this.stream.Push(buffer, index, end - index);
                         this.currentStream = null;
                         return num3;
                     }
                     case MatchState.InsufficientData:
                     {
                         int num4 = index - offset;
                         if (num4 <= 0)
                         {
                             return -1;
                         }
                         this.stream.Push(buffer, index, end - index);
                         return num4;
                     }
                 }
             }
             index++;
         }
     }
     return read;
 }