コード例 #1
0
ファイル: StreamExtensions.cs プロジェクト: nuxleus/nmqtt
        /// <summary>
        /// Reads an MQTT string from the underlying stream.
        /// </summary>
        /// <param name="stream">The stream to read the string from.</param>
        /// <returns>The Mqtt String.</returns>
        public static string ReadMqttString(this Stream stringStream)
        {
            // read and check the length
            var lengthBytes = new byte[2];
            int bytesRead = stringStream.Read(lengthBytes, 0, 2);
            if (bytesRead < 2)
            {
                throw new ArgumentException(
                    "The stream did not have enough bytes to describe the length of the string",
                    "stringStream");
            }

            System.Text.Encoding enc = new MqttEncoding();
            short stringLength = (short)enc.GetCharCount(lengthBytes);

            // read the bytes from the string, validate we have enough etc.
            var stringBytes = new byte[stringLength];
            bytesRead = stringStream.Read(stringBytes, 0, stringLength);
            if (bytesRead < stringLength)
            {
                throw new ArgumentException("stream",
                    String.Format("The stream did not have enough bytes to match the defined string length {0}", stringLength));
            }

            return enc.GetString(stringBytes);
        }
コード例 #2
0
ファイル: StreamExtensions.cs プロジェクト: fm0597/nmqtt
        /// <summary>
        ///     Reads an MQTT string from the underlying stream.
        /// </summary>
        /// <param name="stringStream">The stream to read the string from.</param>
        /// <returns>The Mqtt String.</returns>
        public static string ReadMqttString(this Stream stringStream)
        {
            // read and check the length
            var lengthBytes = new byte[2];
            var bytesRead = stringStream.Read(lengthBytes, 0, 2);
            if (bytesRead < 2)
            {
                throw new ArgumentException(
                    "The stream did not have enough bytes to describe the length of the string",
                    "stringStream");
            }

            System.Text.Encoding enc = new MqttEncoding();
            var stringLength = (ushort)enc.GetCharCount(lengthBytes);

            // read the bytes from the string, validate we have enough etc.
            var stringBytes = new byte[stringLength];
            var readBuffer = new byte[1 << 10]; // 1KB read buffer
            var totalRead = 0;

            // Keep reading until we have all. Intentionally synchronous
            while (totalRead < stringLength) {
                var remainingBytes = stringLength - totalRead;
                var nextReadSize = remainingBytes > readBuffer.Length ? readBuffer.Length : remainingBytes;
                bytesRead = stringStream.Read(readBuffer, 0, nextReadSize);
                Array.Copy(readBuffer, 0, stringBytes, totalRead, bytesRead);
                totalRead += bytesRead;
            }

            return enc.GetString(stringBytes);
        }