Skip to content

Commit

Permalink
Add a few spans on net6.0 or greater
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob-Hague committed Dec 21, 2023
1 parent 34b5123 commit fa68180
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/Renci.SshNet/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
return true;
}

#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
return left.AsSpan().SequenceEqual(right);
#else
if (left.Length != right.Length)
{
return false;
Expand All @@ -235,6 +238,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
}

return true;
#endif
}

/// <summary>
Expand Down
16 changes: 2 additions & 14 deletions src/Renci.SshNet/Common/SshData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,7 @@ protected byte[] ReadBytes()
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the number of bytes available to be read.</exception>
protected byte[] ReadBytes(int length)
{
var data = new byte[length];
var bytesRead = _stream.Read(data, 0, length);

#if NET8_0_OR_GREATER
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, bytesRead);
#else
if (bytesRead < length)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
#endif

return data;
return _stream.ReadBytes(length);
}

/// <summary>
Expand Down Expand Up @@ -209,7 +197,7 @@ protected bool ReadBoolean()
/// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
protected ushort ReadUInt16()
{
return Pack.BigEndianToUInt16(ReadBytes(2));
return _stream.ReadUInt16();
}

/// <summary>
Expand Down
33 changes: 31 additions & 2 deletions src/Renci.SshNet/Common/SshDataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ public bool IsEndOfData
/// <param name="value"><see cref="uint"/> data to write.</param>
public void Write(uint value)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[4];
System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bytes, value);
Write(bytes);
#else
var bytes = Pack.UInt32ToBigEndian(value);
Write(bytes, 0, bytes.Length);
#endif
}

/// <summary>
Expand All @@ -72,8 +78,14 @@ public void Write(uint value)
/// <param name="value"><see cref="ulong"/> data to write.</param>
public void Write(ulong value)
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[8];
System.Buffers.Binary.BinaryPrimitives.WriteUInt64BigEndian(bytes, value);
Write(bytes);
#else
var bytes = Pack.UInt64ToBigEndian(value);
Write(bytes, 0, bytes.Length);
#endif
}

/// <summary>
Expand Down Expand Up @@ -180,6 +192,24 @@ public BigInteger ReadBigInt()
return new BigInteger(data.Reverse());
}

/// <summary>
/// Reads the next <see cref="ushort"/> data type from the SSH data stream.
/// </summary>
/// <returns>
/// The <see cref="ushort"/> read from the SSH data stream.
/// </returns>
public ushort ReadUInt16()
{
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
Span<byte> bytes = stackalloc byte[2];
ReadBytes(bytes);
return System.Buffers.Binary.BinaryPrimitives.ReadUInt16BigEndian(bytes);
#else
var data = ReadBytes(2);
return Pack.BigEndianToUInt16(data);
#endif
}

/// <summary>
/// Reads the next <see cref="uint"/> data type from the SSH data stream.
/// </summary>
Expand Down Expand Up @@ -264,11 +294,10 @@ public override byte[] ToArray()
/// An array of bytes that was read from the internal buffer.
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
private byte[] ReadBytes(int length)
internal byte[] ReadBytes(int length)
{
var data = new byte[length];
var bytesRead = Read(data, 0, length);

if (bytesRead < length)
{
throw new ArgumentOutOfRangeException(nameof(length), string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", length, bytesRead));
Expand Down

0 comments on commit fa68180

Please sign in to comment.