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

SshDataStream

public class SshDataStream : MemoryStream
Specialized MemoryStream for reading and writing data SSH data.
using System; using System.Buffers; using System.Buffers.Binary; using System.Globalization; using System.IO; using System.Numerics; 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) { } private new int Read(Span<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); int num = Read(array, 0, buffer.Length); MemoryExtensions.AsSpan(array, 0, num).CopyTo(buffer); ArrayPool<byte>.Shared.Return(array, false); return num; } private new void Write(ReadOnlySpan<byte> buffer) { byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length); buffer.CopyTo(array); Write(array, 0, buffer.Length); ArrayPool<byte>.Shared.Return(array, false); } 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 = Extensions.ToByteArray(data, false, true); WriteBinary(array, 0, array.Length); } public void Write(byte[] data) { ThrowHelper.ThrowIfNull(data, "data"); Write(data, 0, data.Length); } public void Write(string s, Encoding encoding) { ThrowHelper.ThrowIfNull(encoding, "encoding"); byte[] bytes = encoding.GetBytes(s); WriteBinary(bytes, 0, bytes.Length); } 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) { ThrowHelper.ThrowIfNull(buffer, "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() { byte[] array = ReadBinary(); Array.Reverse((Array)array); return new BigInteger(array); } 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)); } } }