Renci.SshNet.Common.SshDataStream.ReadString C# (CSharp) Method

ReadString() public method

Reads the next string data type from the SSH data stream.
public ReadString ( Encoding encoding ) : string
encoding System.Text.Encoding
return string
        public string ReadString(Encoding encoding)
        {
            var length = ReadUInt32();

            if (length > int.MaxValue)
            {
                throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", int.MaxValue));
            }

            var bytes = ReadBytes((int) length);
            return encoding.GetString(bytes, 0, bytes.Length);
        }

Usage Example

コード例 #1
0
        public void GetBytes()
        {
            var target = new PseudoTerminalRequestInfo(_environmentVariable, _columns, _rows, _width, _height, _terminalModeValues);

            var bytes = target.GetBytes();

            var expectedBytesLength = 1; // WantReply
            expectedBytesLength += 4; // EnvironmentVariable length
            expectedBytesLength += _environmentVariableBytes.Length; // EnvironmentVariable
            expectedBytesLength += 4; // Columns
            expectedBytesLength += 4; // Rows
            expectedBytesLength += 4; // PixelWidth
            expectedBytesLength += 4; // PixelHeight
            expectedBytesLength += 4; // Length of "encoded terminal modes"
            expectedBytesLength += _terminalModeValues.Count*(1 + 4) + 1; // encoded terminal modes

            Assert.AreEqual(expectedBytesLength, bytes.Length);

            var sshDataStream = new SshDataStream(bytes);

            Assert.AreEqual(1, sshDataStream.ReadByte()); // WantReply
            Assert.AreEqual(_environmentVariable, sshDataStream.ReadString(Encoding.UTF8));
            Assert.AreEqual(_columns, sshDataStream.ReadUInt32());
            Assert.AreEqual(_rows, sshDataStream.ReadUInt32());
            Assert.AreEqual(_width, sshDataStream.ReadUInt32());
            Assert.AreEqual(_height, sshDataStream.ReadUInt32());
            Assert.AreEqual((uint) (_terminalModeValues.Count * (1 + 4) + 1), sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.CS8, sshDataStream.ReadByte());
            Assert.AreEqual(_terminalModeValues[TerminalModes.CS8], sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.ECHO, sshDataStream.ReadByte());
            Assert.AreEqual(_terminalModeValues[TerminalModes.ECHO], sshDataStream.ReadUInt32());
            Assert.AreEqual((int) TerminalModes.TTY_OP_END, sshDataStream.ReadByte());

            Assert.IsTrue(sshDataStream.IsEndOfData);
        }
All Usage Examples Of Renci.SshNet.Common.SshDataStream::ReadString