Skip to content

Commit fa68180

Browse files
committed
Add a few spans on net6.0 or greater
1 parent 34b5123 commit fa68180

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

src/Renci.SshNet/Common/Extensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
221221
return true;
222222
}
223223

224+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
225+
return left.AsSpan().SequenceEqual(right);
226+
#else
224227
if (left.Length != right.Length)
225228
{
226229
return false;
@@ -235,6 +238,7 @@ public static bool IsEqualTo(this byte[] left, byte[] right)
235238
}
236239

237240
return true;
241+
#endif
238242
}
239243

240244
/// <summary>

src/Renci.SshNet/Common/SshData.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,7 @@ protected byte[] ReadBytes()
155155
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the number of bytes available to be read.</exception>
156156
protected byte[] ReadBytes(int length)
157157
{
158-
var data = new byte[length];
159-
var bytesRead = _stream.Read(data, 0, length);
160-
161-
#if NET8_0_OR_GREATER
162-
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, bytesRead);
163-
#else
164-
if (bytesRead < length)
165-
{
166-
throw new ArgumentOutOfRangeException(nameof(length));
167-
}
168-
#endif
169-
170-
return data;
158+
return _stream.ReadBytes(length);
171159
}
172160

173161
/// <summary>
@@ -209,7 +197,7 @@ protected bool ReadBoolean()
209197
/// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
210198
protected ushort ReadUInt16()
211199
{
212-
return Pack.BigEndianToUInt16(ReadBytes(2));
200+
return _stream.ReadUInt16();
213201
}
214202

215203
/// <summary>

src/Renci.SshNet/Common/SshDataStream.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,14 @@ public bool IsEndOfData
6262
/// <param name="value"><see cref="uint"/> data to write.</param>
6363
public void Write(uint value)
6464
{
65+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
66+
Span<byte> bytes = stackalloc byte[4];
67+
System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(bytes, value);
68+
Write(bytes);
69+
#else
6570
var bytes = Pack.UInt32ToBigEndian(value);
6671
Write(bytes, 0, bytes.Length);
72+
#endif
6773
}
6874

6975
/// <summary>
@@ -72,8 +78,14 @@ public void Write(uint value)
7278
/// <param name="value"><see cref="ulong"/> data to write.</param>
7379
public void Write(ulong value)
7480
{
81+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
82+
Span<byte> bytes = stackalloc byte[8];
83+
System.Buffers.Binary.BinaryPrimitives.WriteUInt64BigEndian(bytes, value);
84+
Write(bytes);
85+
#else
7586
var bytes = Pack.UInt64ToBigEndian(value);
7687
Write(bytes, 0, bytes.Length);
88+
#endif
7789
}
7890

7991
/// <summary>
@@ -180,6 +192,24 @@ public BigInteger ReadBigInt()
180192
return new BigInteger(data.Reverse());
181193
}
182194

195+
/// <summary>
196+
/// Reads the next <see cref="ushort"/> data type from the SSH data stream.
197+
/// </summary>
198+
/// <returns>
199+
/// The <see cref="ushort"/> read from the SSH data stream.
200+
/// </returns>
201+
public ushort ReadUInt16()
202+
{
203+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
204+
Span<byte> bytes = stackalloc byte[2];
205+
ReadBytes(bytes);
206+
return System.Buffers.Binary.BinaryPrimitives.ReadUInt16BigEndian(bytes);
207+
#else
208+
var data = ReadBytes(2);
209+
return Pack.BigEndianToUInt16(data);
210+
#endif
211+
}
212+
183213
/// <summary>
184214
/// Reads the next <see cref="uint"/> data type from the SSH data stream.
185215
/// </summary>
@@ -264,11 +294,10 @@ public override byte[] ToArray()
264294
/// An array of bytes that was read from the internal buffer.
265295
/// </returns>
266296
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is greater than the internal buffer size.</exception>
267-
private byte[] ReadBytes(int length)
297+
internal byte[] ReadBytes(int length)
268298
{
269299
var data = new byte[length];
270300
var bytesRead = Read(data, 0, length);
271-
272301
if (bytesRead < length)
273302
{
274303
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));

0 commit comments

Comments
 (0)