<PackageReference Include="SSH.NET" Version="2024.1.0" />

SshDataStream

public class SshDataStream : MemoryStream
Specialized MemoryStream for reading and writing data SSH data.
using System; using System.Buffers.Binary; using System.Globalization; using System.IO; using System.Text; namespace Renci.SshNet.Common { public class SshDataStream : MemoryStream { public bool IsEndOfData => Position >= Length; public SshDataStream(int capacity) : base(capacity) { } public SshDataStream(byte[] buffer) : base(buffer) { } public SshDataStream(byte[] buffer, int offset, int count) : base(buffer, offset, count) { } public unsafe void Write(uint value) { Span<byte> span = new Span<byte>(stackalloc byte[4], 4); BinaryPrimitives.WriteUInt32BigEndian(span, value); Write(span); } public unsafe void Write(ulong value) { Span<byte> span = new Span<byte>(stackalloc byte[8], 8); BinaryPrimitives.WriteUInt64BigEndian(span, value); Write(span); } public void Write(BigInteger data) { byte[] array = data.ToByteArray().Reverse(); WriteBinary(array, 0, array.Length); } public void Write(byte[] data) { if (data == null) throw new ArgumentNullException("data"); Write(data, 0, data.Length); } public unsafe void Write(string s, Encoding encoding) { if (encoding == null) throw new ArgumentNullException("encoding"); ReadOnlySpan<char> chars = s; int byteCount = encoding.GetByteCount(chars); Span<byte> span; if (byteCount <= 256) { int num = byteCount; span = new Span<byte>(stackalloc byte[(int)(uint)num], num); } else span = new byte[byteCount]; Span<byte> span2 = span; encoding.GetBytes(chars, span2); Write((uint)byteCount); Write(span2); } public byte[] ReadBinary() { uint num = ReadUInt32(); if (num > 2147483647) throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Data longer than {0} is not supported.", 2147483647)); return ReadBytes((int)num); } public void WriteBinary(byte[] buffer) { if (buffer == null) throw new ArgumentNullException("buffer"); WriteBinary(buffer, 0, buffer.Length); } public void WriteBinary(byte[] buffer, int offset, int count) { Write((uint)count); Write(buffer, offset, count); } public BigInteger ReadBigInt() { uint length = ReadUInt32(); return new BigInteger(ReadBytes((int)length).Reverse()); } public unsafe ushort ReadUInt16() { Span<byte> span = new Span<byte>(stackalloc byte[2], 2); ReadBytes(span); return BinaryPrimitives.ReadUInt16BigEndian(span); } public unsafe uint ReadUInt32() { Span<byte> span = new Span<byte>(stackalloc byte[4], 4); ReadBytes(span); return BinaryPrimitives.ReadUInt32BigEndian(span); } public unsafe ulong ReadUInt64() { Span<byte> span = new Span<byte>(stackalloc byte[8], 8); ReadBytes(span); return BinaryPrimitives.ReadUInt64BigEndian(span); } public string ReadString(Encoding encoding = null) { if (encoding == null) encoding = Encoding.UTF8; uint num = ReadUInt32(); if (num > 2147483647) throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Strings longer than {0} is not supported.", 2147483647)); byte[] array = ReadBytes((int)num); return encoding.GetString(array, 0, array.Length); } public override byte[] ToArray() { if (Capacity == Length) return GetBuffer(); return base.ToArray(); } internal byte[] ReadBytes(int length) { byte[] array = new byte[length]; int num = Read(array, 0, length); if (num < length) throw new ArgumentOutOfRangeException("length", string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", length, num)); return array; } private void ReadBytes(Span<byte> buffer) { int num = Read(buffer); if (num < buffer.Length) throw new ArgumentOutOfRangeException("buffer", string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", buffer.Length, num)); } } }