public StringDecoder(int sizeQuota)
 {
     this.sizeQuota    = sizeQuota;
     this.sizeDecoder  = new IntDecoder();
     this.currentState = StringDecoder.State.ReadingSize;
     this.Reset();
 }
        public int Decode(byte[] buffer, int offset, int size)
        {
            int num;

            DecoderHelper.ValidateSize(size);
            switch (this.currentState)
            {
            case StringDecoder.State.ReadingSize:
            {
                num = this.sizeDecoder.Decode(buffer, offset, size);
                if (!this.sizeDecoder.IsValueDecoded)
                {
                    break;
                }
                this.encodedSize = this.sizeDecoder.Value;
                if (this.encodedSize > this.sizeQuota)
                {
                    Exception exception = this.OnSizeQuotaExceeded(this.encodedSize);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
                }
                if (this.encodedBytes == null || (int)this.encodedBytes.Length < this.encodedSize)
                {
                    this.encodedBytes = DiagnosticUtility.Utility.AllocateByteArray(this.encodedSize);
                    this.@value       = null;
                }
                this.currentState = StringDecoder.State.ReadingBytes;
                this.bytesNeeded  = this.encodedSize;
                break;
            }

            case StringDecoder.State.ReadingBytes:
            {
                if (this.@value == null || this.valueLengthInBytes != this.encodedSize || this.bytesNeeded != this.encodedSize || size < this.encodedSize || !StringDecoder.CompareBuffers(this.encodedBytes, buffer, offset))
                {
                    num = this.bytesNeeded;
                    if (size < this.bytesNeeded)
                    {
                        num = size;
                    }
                    Buffer.BlockCopy(buffer, offset, this.encodedBytes, this.encodedSize - this.bytesNeeded, num);
                    StringDecoder stringDecoder = this;
                    stringDecoder.bytesNeeded = stringDecoder.bytesNeeded - num;
                    if (this.bytesNeeded != 0)
                    {
                        break;
                    }
                    this.@value             = Encoding.UTF8.GetString(this.encodedBytes, 0, this.encodedSize);
                    this.valueLengthInBytes = this.encodedSize;
                    this.OnComplete(this.@value);
                    break;
                }
                else
                {
                    num = this.bytesNeeded;
                    this.OnComplete(this.@value);
                    break;
                }
            }

            default:
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidDataException(Microsoft.ServiceBus.SR.GetString(Resources.InvalidDecoderStateMachine, new object[0])));
            }
            }
            return(num);
        }
 public void Reset()
 {
     this.currentState = StringDecoder.State.ReadingSize;
     this.sizeDecoder.Reset();
 }
 protected virtual void OnComplete(string value)
 {
     this.currentState = StringDecoder.State.Done;
 }