Forráskód Böngészése

Add a few spans on net6.0 or greater (#1138)

Rob Hague 1 éve
szülő
commit
e998d875e0

+ 4 - 0
src/Renci.SshNet/Common/Extensions.cs

@@ -221,6 +221,9 @@ namespace Renci.SshNet.Common
                 return true;
             }
 
+#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
+            return left.AsSpan().SequenceEqual(right);
+#else
             if (left.Length != right.Length)
             {
                 return false;
@@ -235,6 +238,7 @@ namespace Renci.SshNet.Common
             }
 
             return true;
+#endif
         }
 
         /// <summary>

+ 2 - 14
src/Renci.SshNet/Common/SshData.cs

@@ -155,19 +155,7 @@ namespace Renci.SshNet.Common
         /// <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>
@@ -209,7 +197,7 @@ namespace Renci.SshNet.Common
         /// <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>

+ 31 - 2
src/Renci.SshNet/Common/SshDataStream.cs

@@ -62,8 +62,14 @@ namespace Renci.SshNet.Common
         /// <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>
@@ -72,8 +78,14 @@ namespace Renci.SshNet.Common
         /// <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>
@@ -180,6 +192,24 @@ namespace Renci.SshNet.Common
             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>
@@ -264,11 +294,10 @@ namespace Renci.SshNet.Common
         /// 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));