GetString() public static method

public static GetString ( byte data ) : string
data byte
return string
示例#1
0
        protected string ReceiveMessage()
        {
            var  buffer = new MemoryStream();
            int  @byte;
            bool possibleDelimiterIsComing;
            var  possibleDelimiterCount = 0;

            while ((@byte = stream.ReadByte()) != -1)
            {
                buffer.WriteByte((byte)@byte);

                if (@byte == this.delimiterBytes[possibleDelimiterCount])
                {
                    possibleDelimiterIsComing = true;
                    possibleDelimiterCount++;
                }
                else
                {
                    possibleDelimiterIsComing = false;
                    possibleDelimiterCount    = 0;
                }

                if (possibleDelimiterIsComing && this.delimiterBytes.Length == possibleDelimiterCount)
                {
                    // complete message received, return removing the delimiters
                    return(StreamEncoding.GetString(
                               buffer.GetBuffer(),
                               0,
                               (int)(buffer.Length - this.delimiterBytes.Length)));
                }
            }

            // return incomplete message
            return(StreamEncoding.GetString(buffer.GetBuffer()));
        }
        private string ReceiveMessage(int length)
        {
            var messageBytes       = new byte[length];
            var currentBufferIndex = 0;
            var bytesRead          = -1;

            while (bytesRead != 0 && currentBufferIndex < messageBytes.Length)
            {
                bytesRead = this.stream.Read(
                    messageBytes,
                    currentBufferIndex,
                    messageBytes.Length - currentBufferIndex);

                currentBufferIndex += bytesRead;
            }

            return(StreamEncoding.GetString(messageBytes));
        }