Use extension members for ThrowHelpers

This commit is contained in:
Marius Thesing
2025-04-18 13:44:44 +02:00
parent c0b525fce1
commit a0a67e010f
64 changed files with 287 additions and 333 deletions
@@ -2,8 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using Renci.SshNet.Common;
namespace Renci.SshNet.Abstractions
{
internal static class ThreadAbstraction
@@ -18,7 +16,7 @@ namespace Renci.SshNet.Abstractions
/// </returns>
public static Task ExecuteThreadLongRunning(Action action)
{
ThrowHelper.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(action);
return Task.Factory.StartNew(action,
CancellationToken.None,
@@ -32,7 +30,7 @@ namespace Renci.SshNet.Abstractions
/// <param name="action">The action to execute.</param>
public static void ExecuteThread(Action action)
{
ThrowHelper.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(action);
_ = ThreadPool.QueueUserWorkItem(o => action());
}
@@ -0,0 +1,94 @@
#nullable enable
#if !NET
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace System
{
internal static class ThrowExtensions
{
extension(ObjectDisposedException)
{
public static void ThrowIf(bool condition, object instance)
{
if (condition)
{
Throw(instance);
static void Throw(object? instance)
{
throw new ObjectDisposedException(instance?.GetType().FullName);
}
}
}
}
extension(ArgumentNullException)
{
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (argument is null)
{
ThrowArgumentNullException(paramName);
}
[DoesNotReturn]
static void ThrowArgumentNullException(string? paramName)
{
throw new ArgumentNullException(paramName);
}
}
}
extension(ArgumentException)
{
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (string.IsNullOrWhiteSpace(argument))
{
Throw(argument, paramName);
[DoesNotReturn]
static void Throw(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
}
}
}
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
if (string.IsNullOrEmpty(argument))
{
Throw(argument, paramName);
[DoesNotReturn]
static void Throw(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException("The value cannot be an empty string.", paramName);
}
}
}
}
extension(ArgumentOutOfRangeException)
{
public static void ThrowIfNegative(long value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
{
if (value < 0)
{
Throw(value, paramName);
[DoesNotReturn]
static void Throw(long value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, "Value must be non-negative.");
}
}
}
}
}
}
#endif
+1 -3
View File
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -36,7 +34,7 @@ namespace Renci.SshNet
/// <exception cref="ArgumentException"><paramref name="username"/> is whitespace or <see langword="null"/>.</exception>
protected AuthenticationMethod(string username)
{
ThrowHelper.ThrowIfNullOrWhiteSpace(username);
ArgumentException.ThrowIfNullOrWhiteSpace(username);
Username = username;
}
+3 -3
View File
@@ -186,8 +186,8 @@ namespace Renci.SshNet
/// </remarks>
private protected BaseClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
{
ThrowHelper.ThrowIfNull(connectionInfo);
ThrowHelper.ThrowIfNull(serviceFactory);
ArgumentNullException.ThrowIfNull(connectionInfo);
ArgumentNullException.ThrowIfNull(serviceFactory);
_connectionInfo = connectionInfo;
_ownsConnectionInfo = ownsConnectionInfo;
@@ -467,7 +467,7 @@ namespace Renci.SshNet
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected void CheckDisposed()
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
}
/// <summary>
+2 -2
View File
@@ -52,8 +52,8 @@ namespace Renci.SshNet
/// <exception cref="SshAuthenticationException">Failed to authenticate the client.</exception>
public void Authenticate(IConnectionInfoInternal connectionInfo, ISession session)
{
ThrowHelper.ThrowIfNull(connectionInfo);
ThrowHelper.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(connectionInfo);
ArgumentNullException.ThrowIfNull(session);
session.RegisterMessage("SSH_MSG_USERAUTH_FAILURE");
session.RegisterMessage("SSH_MSG_USERAUTH_SUCCESS");
@@ -16,7 +16,7 @@ namespace Renci.SshNet.Common
public ChannelDataEventArgs(uint channelNumber, ArraySegment<byte> data)
: base(channelNumber)
{
ThrowHelper.ThrowIfNull(data.Array);
ArgumentNullException.ThrowIfNull(data.Array);
Data = data;
}
@@ -106,7 +106,7 @@ namespace Renci.SshNet.Common
#endif
ValidateBufferArguments(buffer, offset, count);
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
if (count == 0)
{
@@ -16,7 +16,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/>.</exception>
public ChannelRequestEventArgs(RequestInfo info)
{
ThrowHelper.ThrowIfNull(info);
ArgumentNullException.ThrowIfNull(info);
Info = info;
}
+5 -5
View File
@@ -193,7 +193,7 @@ namespace Renci.SshNet.Common
/// </remarks>
public static byte[] Take(this byte[] value, int offset, int count)
{
ThrowHelper.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(value);
if (count == 0)
{
@@ -225,7 +225,7 @@ namespace Renci.SshNet.Common
/// </remarks>
public static byte[] Take(this byte[] value, int count)
{
ThrowHelper.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(value);
if (count == 0)
{
@@ -244,8 +244,8 @@ namespace Renci.SshNet.Common
public static bool IsEqualTo(this byte[] left, byte[] right)
{
ThrowHelper.ThrowIfNull(left);
ThrowHelper.ThrowIfNull(right);
ArgumentNullException.ThrowIfNull(left);
ArgumentNullException.ThrowIfNull(right);
return left.AsSpan().SequenceEqual(right);
}
@@ -259,7 +259,7 @@ namespace Renci.SshNet.Common
/// </returns>
public static byte[] TrimLeadingZeros(this byte[] value)
{
ThrowHelper.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(value);
for (var i = 0; i < value.Length; i++)
{
+1 -1
View File
@@ -97,7 +97,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="host"/> is <see langword="null"/>.</exception>
public HostKeyEventArgs(KeyHostAlgorithm host)
{
ThrowHelper.ThrowIfNull(host);
ArgumentNullException.ThrowIfNull(host);
CanTrust = true;
HostKey = host.KeyData.GetBytes();
+2 -2
View File
@@ -14,8 +14,8 @@ namespace Renci.SshNet.Common
public static string Create(byte[] data, int indentLevel)
{
ThrowHelper.ThrowIfNull(data);
ThrowHelper.ThrowIfNegative(indentLevel);
ArgumentNullException.ThrowIfNull(data);
ArgumentOutOfRangeException.ThrowIfNegative(indentLevel);
const int lineWidth = 16;
+1 -1
View File
@@ -123,7 +123,7 @@ namespace Renci.SshNet.Common
{
Debug.Assert(Monitor.IsEntered(_sync));
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);
ObjectDisposedException.ThrowIf(_disposed, this);
_buffer.EnsureAvailableSpace(buffer.Length);
@@ -17,7 +17,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is not within <see cref="IPEndPoint.MinPort" /> and <see cref="IPEndPoint.MaxPort" />.</exception>
internal PortForwardEventArgs(string host, uint port)
{
ThrowHelper.ThrowIfNull(host);
ArgumentNullException.ThrowIfNull(host);
port.ValidatePort();
OriginatorHost = host;
+3 -3
View File
@@ -38,7 +38,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentException"><paramref name="path"/> is empty ("").</exception>
public static PosixPath CreateAbsoluteOrRelativeFilePath(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
var posixPath = new PosixPath();
@@ -92,7 +92,7 @@ namespace Renci.SshNet.Common
/// </remarks>
public static string GetFileName(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
var pathEnd = path.LastIndexOf('/');
if (pathEnd == -1)
@@ -119,7 +119,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
public static string GetDirectoryName(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
var pathEnd = path.LastIndexOf('/');
if (pathEnd == -1)
+2 -2
View File
@@ -91,7 +91,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
public void Load(byte[] data)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
LoadInternal(data, 0, data.Length);
}
@@ -105,7 +105,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
public void Load(byte[] data, int offset, int count)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
LoadInternal(data, offset, count);
}
+4 -4
View File
@@ -112,7 +112,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
public void Write(byte[] data)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
Write(data, 0, data.Length);
}
@@ -126,8 +126,8 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
public void Write(string s, Encoding encoding)
{
ThrowHelper.ThrowIfNull(s);
ThrowHelper.ThrowIfNull(encoding);
ArgumentNullException.ThrowIfNull(s);
ArgumentNullException.ThrowIfNull(encoding);
#if NET
ReadOnlySpan<char> value = s;
@@ -192,7 +192,7 @@ namespace Renci.SshNet.Common
/// <exception cref="ArgumentNullException"><paramref name="buffer"/> is <see langword="null"/>.</exception>
public void WriteBinary(byte[] buffer)
{
ThrowHelper.ThrowIfNull(buffer);
ArgumentNullException.ThrowIfNull(buffer);
WriteBinary(buffer, 0, buffer.Length);
}
+1 -23
View File
@@ -1,6 +1,6 @@
#pragma warning disable
#if !NET
// Copied verbatim from https://github.com/dotnet/runtime/blob/261611930d6b436d7c4395450356b624d903d9bf/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs
// Copied verbatim from https://github.com/dotnet/runtime/blob/7b2de1e5ed6368c536ee646346e6fd81939e6fe6/src/libraries/Common/src/System/Threading/Tasks/TaskToAsyncResult.cs
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
@@ -35,15 +35,7 @@ namespace System.Threading.Tasks
/// </remarks>
public static IAsyncResult Begin(Task task, AsyncCallback? callback, object? state)
{
#if NET
ArgumentNullException.ThrowIfNull(task);
#else
if (task is null)
{
throw new ArgumentNullException(nameof(task));
}
#endif
return new TaskAsyncResult(task, state, callback);
}
@@ -72,14 +64,7 @@ namespace System.Threading.Tasks
/// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not produced by a call to <see cref="Begin"/>.</exception>
public static Task Unwrap(IAsyncResult asyncResult)
{
#if NET
ArgumentNullException.ThrowIfNull(asyncResult);
#else
if (asyncResult is null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
#endif
if ((asyncResult as TaskAsyncResult)?._task is not Task task)
{
@@ -101,14 +86,7 @@ namespace System.Threading.Tasks
/// </exception>
public static Task<TResult> Unwrap<TResult>(IAsyncResult asyncResult)
{
#if NET
ArgumentNullException.ThrowIfNull(asyncResult);
#else
if (asyncResult is null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
#endif
if ((asyncResult as TaskAsyncResult)?._task is not Task<TResult> task)
{
+4 -96
View File
@@ -1,93 +1,19 @@
#nullable enable
#if !NET
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Renci.SshNet.Common
{
internal static class ThrowHelper
{
public static void ThrowObjectDisposedIf(bool condition, object instance)
{
#if NET
ObjectDisposedException.ThrowIf(condition, instance);
#else
if (condition)
{
Throw(instance);
static void Throw(object? instance)
{
throw new ObjectDisposedException(instance?.GetType().FullName);
}
}
#endif
}
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET
ArgumentNullException.ThrowIfNull(argument, paramName);
#else
if (argument is null)
{
Throw(paramName);
[DoesNotReturn]
static void Throw(string? paramName)
{
throw new ArgumentNullException(paramName);
}
}
#endif
}
public static void ThrowIfNullOrWhiteSpace([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET
ArgumentException.ThrowIfNullOrWhiteSpace(argument, paramName);
#else
if (string.IsNullOrWhiteSpace(argument))
{
Throw(argument, paramName);
[DoesNotReturn]
static void Throw(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException("The value cannot be an empty string or composed entirely of whitespace.", paramName);
}
}
#endif
}
public static void ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
#if NET
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
#else
if (string.IsNullOrEmpty(argument))
{
Throw(argument, paramName);
[DoesNotReturn]
static void Throw(string? argument, string? paramName)
{
ThrowIfNull(argument, paramName);
throw new ArgumentException("The value cannot be an empty string.", paramName);
}
}
#endif
}
#if !NET
// A rough copy of
// https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L960C13-L974C10
// for lower targets.
public static void ValidateBufferArguments(byte[] buffer, int offset, int count)
{
ThrowIfNull(buffer);
ThrowIfNegative(offset);
ArgumentNullException.ThrowIfNull(buffer);
ArgumentOutOfRangeException.ThrowIfNegative(offset);
if ((uint)count > buffer.Length - offset)
{
@@ -102,24 +28,6 @@ namespace Renci.SshNet.Common
}
}
}
#endif
public static void ThrowIfNegative(long value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
{
#if NET
ArgumentOutOfRangeException.ThrowIfNegative(value, paramName);
#else
if (value < 0)
{
Throw(value, paramName);
[DoesNotReturn]
static void Throw(long value, string? paramName)
{
throw new ArgumentOutOfRangeException(paramName, value, "Value must be non-negative.");
}
}
#endif
}
}
}
#endif
+1 -1
View File
@@ -18,7 +18,7 @@ namespace Renci.SshNet.Connection
protected ConnectorBase(ISocketFactory socketFactory, ILoggerFactory loggerFactory)
{
ThrowHelper.ThrowIfNull(socketFactory);
ArgumentNullException.ThrowIfNull(socketFactory);
SocketFactory = socketFactory;
_logger = loggerFactory.CreateLogger(GetType());
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Connection
{
/// <summary>
@@ -33,8 +31,8 @@ namespace Renci.SshNet.Connection
/// <exception cref="ArgumentNullException"><paramref name="softwareVersion"/> is <see langword="null"/>.</exception>
public SshIdentification(string protocolVersion, string softwareVersion, string comments)
{
ThrowHelper.ThrowIfNull(protocolVersion);
ThrowHelper.ThrowIfNull(softwareVersion);
ArgumentNullException.ThrowIfNull(protocolVersion);
ArgumentNullException.ThrowIfNull(softwareVersion);
ProtocolVersion = protocolVersion;
SoftwareVersion = softwareVersion;
+5 -5
View File
@@ -325,17 +325,17 @@ namespace Renci.SshNet
/// <exception cref="ArgumentException">No <paramref name="authenticationMethods"/> specified.</exception>
public ConnectionInfo(string host, int port, string username, ProxyTypes proxyType, string proxyHost, int proxyPort, string proxyUsername, string proxyPassword, params AuthenticationMethod[] authenticationMethods)
{
ThrowHelper.ThrowIfNull(host);
ArgumentNullException.ThrowIfNull(host);
port.ValidatePort();
ThrowHelper.ThrowIfNullOrWhiteSpace(username);
ArgumentException.ThrowIfNullOrWhiteSpace(username);
if (proxyType != ProxyTypes.None)
{
ThrowHelper.ThrowIfNull(proxyHost);
ArgumentNullException.ThrowIfNull(proxyHost);
proxyPort.ValidatePort();
}
ThrowHelper.ThrowIfNull(authenticationMethods);
ArgumentNullException.ThrowIfNull(authenticationMethods);
if (authenticationMethods.Length == 0)
{
@@ -459,7 +459,7 @@ namespace Renci.SshNet
/// <exception cref="SshAuthenticationException">No suitable authentication method found to complete authentication, or permission denied.</exception>
internal void Authenticate(ISession session, IServiceFactory serviceFactory)
{
ThrowHelper.ThrowIfNull(serviceFactory);
ArgumentNullException.ThrowIfNull(serviceFactory);
IsAuthenticated = false;
var clientAuthentication = serviceFactory.CreateClientAuthentication();
+4 -6
View File
@@ -1,8 +1,6 @@
using System;
using System.Text.RegularExpressions;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -28,8 +26,8 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="expect"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
public ExpectAction(Regex expect, Action<string> action)
{
ThrowHelper.ThrowIfNull(expect);
ThrowHelper.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(expect);
ArgumentNullException.ThrowIfNull(action);
Expect = expect;
Action = action;
@@ -43,8 +41,8 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="expect"/> or <paramref name="action"/> is <see langword="null"/>.</exception>
public ExpectAction(string expect, Action<string> action)
{
ThrowHelper.ThrowIfNull(expect);
ThrowHelper.ThrowIfNull(action);
ArgumentNullException.ThrowIfNull(expect);
ArgumentNullException.ThrowIfNull(action);
Expect = new Regex(Regex.Escape(expect));
Action = action;
+1 -1
View File
@@ -130,7 +130,7 @@ namespace Renci.SshNet
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
}
/// <summary>
+3 -3
View File
@@ -91,8 +91,8 @@ namespace Renci.SshNet
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="IPEndPoint.MaxPort" />.</exception>
public ForwardedPortLocal(string boundHost, uint boundPort, string host, uint port)
{
ThrowHelper.ThrowIfNull(boundHost);
ThrowHelper.ThrowIfNull(host);
ArgumentNullException.ThrowIfNull(boundHost);
ArgumentNullException.ThrowIfNull(host);
boundPort.ValidatePort();
port.ValidatePort();
@@ -158,7 +158,7 @@ namespace Renci.SshNet
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
}
/// <summary>
+3 -3
View File
@@ -88,8 +88,8 @@ namespace Renci.SshNet
/// <exception cref="ArgumentOutOfRangeException"><paramref name="port" /> is greater than <see cref="IPEndPoint.MaxPort" />.</exception>
public ForwardedPortRemote(IPAddress boundHostAddress, uint boundPort, IPAddress hostAddress, uint port)
{
ThrowHelper.ThrowIfNull(boundHostAddress);
ThrowHelper.ThrowIfNull(hostAddress);
ArgumentNullException.ThrowIfNull(boundHostAddress);
ArgumentNullException.ThrowIfNull(hostAddress);
boundPort.ValidatePort();
port.ValidatePort();
@@ -227,7 +227,7 @@ namespace Renci.SshNet
/// <exception cref="ObjectDisposedException">The current instance is disposed.</exception>
protected override void CheckDisposed()
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
}
private void Session_ChannelOpening(object sender, MessageEventArgs<ChannelOpenMessage> e)
+1 -3
View File
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -22,7 +20,7 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="message"/> is <see langword="null"/>.</exception>
public MessageEventArgs(T message)
{
ThrowHelper.ThrowIfNull(message);
ArgumentNullException.ThrowIfNull(message);
Message = message;
}
@@ -1,4 +1,4 @@
using Renci.SshNet.Common;
using System;
namespace Renci.SshNet.Messages.Connection
{
@@ -89,7 +89,7 @@ namespace Renci.SshNet.Messages.Connection
public ChannelDataMessage(uint localChannelNumber, byte[] data)
: base(localChannelNumber)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
Data = data;
Offset = 0;
@@ -106,7 +106,7 @@ namespace Renci.SshNet.Messages.Connection
public ChannelDataMessage(uint localChannelNumber, byte[] data, int offset, int size)
: base(localChannelNumber)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
Data = data;
Offset = offset;
@@ -1,8 +1,6 @@
using System;
using System.Globalization;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Connection
{
/// <summary>
@@ -106,7 +104,7 @@ namespace Renci.SshNet.Messages.Connection
/// <exception cref="ArgumentNullException"><paramref name="info"/> is <see langword="null"/>.</exception>
public ChannelOpenMessage(uint channelNumber, uint initialWindowSize, uint maximumPacketSize, ChannelOpenInfo info)
{
ThrowHelper.ThrowIfNull(info);
ArgumentNullException.ThrowIfNull(info);
ChannelType = Ascii.GetBytes(info.ChannelType);
LocalChannelNumber = channelNumber;
@@ -1,8 +1,6 @@
using System;
using System.Text;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Connection
{
/// <summary>
@@ -81,8 +79,8 @@ namespace Renci.SshNet.Messages.Connection
public ExecRequestInfo(string command, Encoding encoding)
: this()
{
ThrowHelper.ThrowIfNull(command);
ThrowHelper.ThrowIfNull(encoding);
ArgumentNullException.ThrowIfNull(command);
ArgumentNullException.ThrowIfNull(encoding);
_command = encoding.GetBytes(command);
Encoding = encoding;
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Messages.Transport
{
/// <summary>
@@ -47,7 +45,7 @@ namespace Renci.SshNet.Messages.Transport
/// <param name="data">The data.</param>
public IgnoreMessage(byte[] data)
{
ThrowHelper.ThrowIfNull(data);
ArgumentNullException.ThrowIfNull(data);
Data = data;
}
+1 -2
View File
@@ -1,7 +1,6 @@
using System;
using System.Threading;
using Renci.SshNet.Common;
using Renci.SshNet.Messages;
using Renci.SshNet.Messages.Authentication;
@@ -44,7 +43,7 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null"/>.</exception>
public override AuthenticationResult Authenticate(Session session)
{
ThrowHelper.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(session);
session.UserAuthenticationSuccessReceived += Session_UserAuthenticationSuccessReceived;
session.UserAuthenticationFailureReceived += Session_UserAuthenticationFailureReceived;
@@ -9,7 +9,9 @@ using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#if !NET
using Renci.SshNet.Common;
#endif
namespace Renci.SshNet
{
@@ -486,8 +488,8 @@ namespace Renci.SshNet
public virtual void CopyTo(T[] array, int arrayIndex)
{
ThrowHelper.ThrowIfNull(array);
ThrowHelper.ThrowIfNegative(arrayIndex);
ArgumentNullException.ThrowIfNull(array);
ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);
if (array.Length - arrayIndex < Count)
{
@@ -69,7 +69,7 @@ namespace Renci.SshNet
public PasswordAuthenticationMethod(string username, byte[] password)
: base(username)
{
ThrowHelper.ThrowIfNull(password);
ArgumentNullException.ThrowIfNull(password);
_password = password;
_requestMessage = new RequestMessagePassword(ServiceName.Connection, Username, _password);
@@ -85,7 +85,7 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="session" /> is <see langword="null"/>.</exception>
public override AuthenticationResult Authenticate(Session session)
{
ThrowHelper.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(session);
_session = session;
@@ -42,7 +42,7 @@ namespace Renci.SshNet
public PrivateKeyAuthenticationMethod(string username, params IPrivateKeySource[] keyFiles)
: base(username)
{
ThrowHelper.ThrowIfNull(keyFiles);
ArgumentNullException.ThrowIfNull(keyFiles);
KeyFiles = new Collection<IPrivateKeySource>(keyFiles);
}
+5 -5
View File
@@ -72,11 +72,11 @@ namespace Renci.SshNet
switch (_version)
{
case "3":
ThrowHelper.ThrowIfNullOrEmpty(_argon2Type);
ThrowHelper.ThrowIfNullOrEmpty(_argon2Iterations);
ThrowHelper.ThrowIfNullOrEmpty(_argon2Memory);
ThrowHelper.ThrowIfNullOrEmpty(_argon2Parallelism);
ThrowHelper.ThrowIfNullOrEmpty(_argon2Salt);
ArgumentException.ThrowIfNullOrEmpty(_argon2Type);
ArgumentException.ThrowIfNullOrEmpty(_argon2Iterations);
ArgumentException.ThrowIfNullOrEmpty(_argon2Memory);
ArgumentException.ThrowIfNullOrEmpty(_argon2Parallelism);
ArgumentException.ThrowIfNullOrEmpty(_argon2Salt);
var keyData = Argon2(
_argon2Type,
+3 -3
View File
@@ -172,7 +172,7 @@ namespace Renci.SshNet
/// <param name="key">The key.</param>
public PrivateKeyFile(Key key)
{
ThrowHelper.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(key);
_key = key;
_hostAlgorithms.Add(new KeyHostAlgorithm(key.ToString(), key));
@@ -223,7 +223,7 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="fileName"/> is <see langword="null"/>.</exception>
public PrivateKeyFile(string fileName, string? passPhrase, string? certificateFileName)
{
ThrowHelper.ThrowIfNull(fileName);
ArgumentNullException.ThrowIfNull(fileName);
using (var keyFile = File.OpenRead(fileName))
{
@@ -263,7 +263,7 @@ namespace Renci.SshNet
/// <param name="certificate">A certificate which certifies the private key.</param>
public PrivateKeyFile(Stream privateKey, string? passPhrase, Stream? certificate)
{
ThrowHelper.ThrowIfNull(privateKey);
ArgumentNullException.ThrowIfNull(privateKey);
Open(privateKey, passPhrase);
@@ -1,8 +1,6 @@
using System;
using System.Text;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -52,7 +50,7 @@ namespace Renci.SshNet
/// </example>
public string Transform(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
var transformed = new StringBuilder(path.Length);
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -23,7 +21,7 @@ namespace Renci.SshNet
/// </remarks>
public string Transform(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
return path;
}
@@ -1,8 +1,6 @@
using System;
using System.Text;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -82,7 +80,7 @@ namespace Renci.SshNet
/// </example>
public string Transform(string path)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
// result is at least value and (likely) leading/trailing single-quotes
var sb = new StringBuilder(path.Length + 2);
+10 -10
View File
@@ -116,7 +116,7 @@ namespace Renci.SshNet
}
set
{
ThrowHelper.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(value);
_remotePathTransformation = value;
}
@@ -288,7 +288,7 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client is not connected.</exception>
public void Upload(FileInfo fileInfo, string path)
{
ThrowHelper.ThrowIfNull(fileInfo);
ArgumentNullException.ThrowIfNull(fileInfo);
if (Session is null)
{
@@ -335,8 +335,8 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client is not connected.</exception>
public void Upload(DirectoryInfo directoryInfo, string path)
{
ThrowHelper.ThrowIfNull(directoryInfo);
ThrowHelper.ThrowIfNullOrEmpty(path);
ArgumentNullException.ThrowIfNull(directoryInfo);
ArgumentException.ThrowIfNullOrEmpty(path);
if (Session is null)
{
@@ -378,8 +378,8 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client is not connected.</exception>
public void Download(string filename, FileInfo fileInfo)
{
ThrowHelper.ThrowIfNullOrEmpty(filename);
ThrowHelper.ThrowIfNull(fileInfo);
ArgumentException.ThrowIfNullOrEmpty(filename);
ArgumentNullException.ThrowIfNull(fileInfo);
if (Session is null)
{
@@ -418,8 +418,8 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client is not connected.</exception>
public void Download(string directoryName, DirectoryInfo directoryInfo)
{
ThrowHelper.ThrowIfNullOrEmpty(directoryName);
ThrowHelper.ThrowIfNull(directoryInfo);
ArgumentException.ThrowIfNullOrEmpty(directoryName);
ArgumentNullException.ThrowIfNull(directoryInfo);
if (Session is null)
{
@@ -458,8 +458,8 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client is not connected.</exception>
public void Download(string filename, Stream destination)
{
ThrowHelper.ThrowIfNullOrWhiteSpace(filename);
ThrowHelper.ThrowIfNull(destination);
ArgumentException.ThrowIfNullOrWhiteSpace(filename);
ArgumentNullException.ThrowIfNull(destination);
if (Session is null)
{
@@ -25,7 +25,7 @@ namespace Renci.SshNet.Security.Cryptography.Ciphers
if (cipherMode != System.Security.Cryptography.CipherMode.ECB)
{
ThrowHelper.ThrowIfNull(iv);
ArgumentNullException.ThrowIfNull(iv);
aes.IV = iv.Take(16);
}
@@ -21,7 +21,7 @@ namespace Renci.SshNet.Security.Cryptography
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
public ED25519DigitalSignature(ED25519Key key)
{
ThrowHelper.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(key);
_key = key;
}
@@ -78,7 +78,7 @@ namespace Renci.SshNet.Security
/// <param name="publicKeyData">The encoded public key data.</param>
public ED25519Key(SshKeyData publicKeyData)
{
ThrowHelper.ThrowIfNull(publicKeyData);
ArgumentNullException.ThrowIfNull(publicKeyData);
if (publicKeyData.Name != "ssh-ed25519" || publicKeyData.Keys.Length != 1)
{
@@ -18,7 +18,7 @@ namespace Renci.SshNet.Security.Cryptography
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
public EcdsaDigitalSignature(EcdsaKey key)
{
ThrowHelper.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(key);
_key = key;
}
@@ -188,7 +188,7 @@ namespace Renci.SshNet.Security
/// <param name="publicKeyData">The encoded public key data.</param>
public EcdsaKey(SshKeyData publicKeyData)
{
ThrowHelper.ThrowIfNull(publicKeyData);
ArgumentNullException.ThrowIfNull(publicKeyData);
if (!publicKeyData.Name.StartsWith("ecdsa-sha2-", StringComparison.Ordinal) || publicKeyData.Keys.Length != 2)
{
@@ -139,7 +139,7 @@ namespace Renci.SshNet.Security
/// <param name="publicKeyData">The encoded public key data.</param>
public RsaKey(SshKeyData publicKeyData)
{
ThrowHelper.ThrowIfNull(publicKeyData);
ArgumentNullException.ThrowIfNull(publicKeyData);
if (publicKeyData.Name != "ssh-rsa" || publicKeyData.Keys.Length != 2)
{
@@ -159,7 +159,7 @@ namespace Renci.SshNet.Security
/// <param name="privateKeyData">DER encoded private key data.</param>
public RsaKey(byte[] privateKeyData)
{
ThrowHelper.ThrowIfNull(privateKeyData);
ArgumentNullException.ThrowIfNull(privateKeyData);
var keyReader = new AsnReader(privateKeyData, AsnEncodingRules.DER);
var sequenceReader = keyReader.ReadSequence();
@@ -1,7 +1,5 @@
using System;
using Renci.SshNet.Common;
namespace Renci.SshNet.Security.Cryptography
{
/// <summary>
@@ -21,7 +19,7 @@ namespace Renci.SshNet.Security.Cryptography
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
protected SymmetricCipher(byte[] key)
{
ThrowHelper.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(key);
Key = key;
}
@@ -7,7 +7,6 @@ using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
namespace Renci.SshNet.Security
@@ -53,9 +52,9 @@ namespace Renci.SshNet.Security
DHParameters parameters,
HashAlgorithmName hashAlgorithm)
{
ThrowHelper.ThrowIfNull(name);
ThrowHelper.ThrowIfNull(parameters);
ThrowHelper.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
ArgumentNullException.ThrowIfNull(name);
ArgumentNullException.ThrowIfNull(parameters);
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
Name = name;
_dhParameters = parameters;
@@ -7,7 +7,6 @@ using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
namespace Renci.SshNet.Security
@@ -83,8 +82,8 @@ namespace Renci.SshNet.Security
uint preferredGroupSize,
uint maximumGroupSize)
{
ThrowHelper.ThrowIfNull(name);
ThrowHelper.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
ArgumentNullException.ThrowIfNull(name);
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
if (preferredGroupSize < minimumGroupSize || preferredGroupSize > maximumGroupSize)
{
+4 -4
View File
@@ -87,8 +87,8 @@ namespace Renci.SshNet
/// <inheritdoc/>
public IKeyExchange CreateKeyExchange(IDictionary<string, Func<IKeyExchange>> clientAlgorithms, string[] serverAlgorithms)
{
ThrowHelper.ThrowIfNull(clientAlgorithms);
ThrowHelper.ThrowIfNull(serverAlgorithms);
ArgumentNullException.ThrowIfNull(clientAlgorithms);
ArgumentNullException.ThrowIfNull(serverAlgorithms);
// find an algorithm that is supported by both client and server
var keyExchangeAlgorithmFactory = (from c in clientAlgorithms
@@ -215,8 +215,8 @@ namespace Renci.SshNet
/// <exception cref="NotSupportedException">The <see cref="IConnectionInfo.ProxyType"/> value of <paramref name="connectionInfo"/> is not supported.</exception>
public IConnector CreateConnector(IConnectionInfo connectionInfo, ISocketFactory socketFactory)
{
ThrowHelper.ThrowIfNull(connectionInfo);
ThrowHelper.ThrowIfNull(socketFactory);
ArgumentNullException.ThrowIfNull(connectionInfo);
ArgumentNullException.ThrowIfNull(socketFactory);
var loggerFactory = connectionInfo.LoggerFactory ?? SshNetLoggingConfiguration.LoggerFactory;
+5 -5
View File
@@ -557,9 +557,9 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="socketFactory"/> is <see langword="null"/>.</exception>
internal Session(ConnectionInfo connectionInfo, IServiceFactory serviceFactory, ISocketFactory socketFactory)
{
ThrowHelper.ThrowIfNull(connectionInfo);
ThrowHelper.ThrowIfNull(serviceFactory);
ThrowHelper.ThrowIfNull(socketFactory);
ArgumentNullException.ThrowIfNull(connectionInfo);
ArgumentNullException.ThrowIfNull(serviceFactory);
ArgumentNullException.ThrowIfNull(socketFactory);
ConnectionInfo = connectionInfo;
SessionLoggerFactory = connectionInfo.LoggerFactory ?? SshNetLoggingConfiguration.LoggerFactory;
@@ -937,7 +937,7 @@ namespace Renci.SshNet
/// </returns>
private WaitResult TryWait(WaitHandle waitHandle, TimeSpan timeout, out Exception exception)
{
ThrowHelper.ThrowIfNull(waitHandle);
ArgumentNullException.ThrowIfNull(waitHandle);
var waitHandles = new[]
{
@@ -999,7 +999,7 @@ namespace Renci.SshNet
/// <exception cref="SocketException">A socket error was signaled while receiving messages from the server.</exception>
internal void WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
{
ThrowHelper.ThrowIfNull(waitHandle);
ArgumentNullException.ThrowIfNull(waitHandle);
var waitHandles = new[]
{
@@ -1,7 +1,6 @@
using System;
using System.Text;
using Renci.SshNet.Common;
using Renci.SshNet.Sftp.Responses;
namespace Renci.SshNet.Sftp.Requests
@@ -44,7 +43,7 @@ namespace Renci.SshNet.Sftp.Requests
public SftpRealPathRequest(uint protocolVersion, uint requestId, string path, Encoding encoding, Action<SftpNameResponse> nameAction, Action<SftpStatusResponse> statusAction)
: base(protocolVersion, requestId, statusAction)
{
ThrowHelper.ThrowIfNull(nameAction);
ArgumentNullException.ThrowIfNull(nameAction);
Encoding = encoding;
Path = path;
+3 -3
View File
@@ -33,8 +33,8 @@ namespace Renci.SshNet.Sftp
throw new SshConnectionException("Client not connected.");
}
ThrowHelper.ThrowIfNull(attributes);
ThrowHelper.ThrowIfNull(fullName);
ArgumentNullException.ThrowIfNull(attributes);
ArgumentNullException.ThrowIfNull(fullName);
_sftpSession = sftpSession;
Attributes = attributes;
@@ -485,7 +485,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="destFileName"/> is <see langword="null"/>.</exception>
public void MoveTo(string destFileName)
{
ThrowHelper.ThrowIfNull(destFileName);
ArgumentNullException.ThrowIfNull(destFileName);
_sftpSession.RequestRename(FullName, destFileName);
+1 -1
View File
@@ -78,7 +78,7 @@ namespace Renci.SshNet.Sftp
public byte[] Read()
{
ThrowHelper.ThrowObjectDisposedIf(_disposingOrDisposed, this);
ObjectDisposedException.ThrowIf(_disposingOrDisposed, this);
if (_exception is not null)
{
+3 -3
View File
@@ -224,7 +224,7 @@ namespace Renci.SshNet.Sftp
{
Debug.Assert(isAsync || cancellationToken == default);
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (bufferSize <= 0)
{
@@ -828,7 +828,7 @@ namespace Renci.SshNet.Sftp
/// </remarks>
public override void SetLength(long value)
{
ThrowHelper.ThrowIfNegative(value);
ArgumentOutOfRangeException.ThrowIfNegative(value);
// Lock down the file stream while we do this.
lock (_lock)
@@ -1179,7 +1179,7 @@ namespace Renci.SshNet.Sftp
private void CheckSessionIsOpen()
{
ThrowHelper.ThrowObjectDisposedIf(_session is null, this);
ObjectDisposedException.ThrowIf(_session is null, this);
if (!_session.IsOpen)
{
+6 -6
View File
@@ -542,7 +542,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public byte[] EndOpen(SftpOpenAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
@@ -658,7 +658,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public void EndClose(SftpCloseAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
@@ -733,7 +733,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public byte[] EndRead(SftpReadAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
@@ -1056,7 +1056,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public SftpFileAttributes EndLStat(SFtpStatAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
@@ -1665,7 +1665,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public string EndRealPath(SftpRealPathAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
@@ -1762,7 +1762,7 @@ namespace Renci.SshNet.Sftp
/// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is <see langword="null"/>.</exception>
public SftpFileAttributes EndStat(SFtpStatAsyncResult asyncResult)
{
ThrowHelper.ThrowIfNull(asyncResult);
ArgumentNullException.ThrowIfNull(asyncResult);
if (asyncResult.EndInvokeCalled)
{
+44 -44
View File
@@ -298,7 +298,7 @@ namespace Renci.SshNet
public void ChangeDirectory(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -323,7 +323,7 @@ namespace Renci.SshNet
public Task ChangeDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -364,7 +364,7 @@ namespace Renci.SshNet
public void CreateDirectory(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -390,7 +390,7 @@ namespace Renci.SshNet
public async Task CreateDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -415,7 +415,7 @@ namespace Renci.SshNet
public void DeleteDirectory(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -431,7 +431,7 @@ namespace Renci.SshNet
public async Task DeleteDirectoryAsync(string path, CancellationToken cancellationToken = default)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -458,7 +458,7 @@ namespace Renci.SshNet
public void DeleteFile(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -474,7 +474,7 @@ namespace Renci.SshNet
public async Task DeleteFileAsync(string path, CancellationToken cancellationToken)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -516,8 +516,8 @@ namespace Renci.SshNet
public void RenameFile(string oldPath, string newPath, bool isPosix)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(oldPath);
ThrowHelper.ThrowIfNull(newPath);
ArgumentNullException.ThrowIfNull(oldPath);
ArgumentNullException.ThrowIfNull(newPath);
if (_sftpSession is null)
{
@@ -553,8 +553,8 @@ namespace Renci.SshNet
public async Task RenameFileAsync(string oldPath, string newPath, CancellationToken cancellationToken)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(oldPath);
ThrowHelper.ThrowIfNull(newPath);
ArgumentNullException.ThrowIfNull(oldPath);
ArgumentNullException.ThrowIfNull(newPath);
if (_sftpSession is null)
{
@@ -581,8 +581,8 @@ namespace Renci.SshNet
public void SymbolicLink(string path, string linkPath)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ThrowHelper.ThrowIfNullOrWhiteSpace(linkPath);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(linkPath);
if (_sftpSession is null)
{
@@ -633,7 +633,7 @@ namespace Renci.SshNet
public async IAsyncEnumerable<ISftpFile> ListDirectoryAsync(string path, [EnumeratorCancellation] CancellationToken cancellationToken)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -738,7 +738,7 @@ namespace Renci.SshNet
public ISftpFile Get(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -768,7 +768,7 @@ namespace Renci.SshNet
public async Task<ISftpFile> GetAsync(string path, CancellationToken cancellationToken)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -799,7 +799,7 @@ namespace Renci.SshNet
public bool Exists(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -855,7 +855,7 @@ namespace Renci.SshNet
public async Task<bool> ExistsAsync(string path, CancellationToken cancellationToken = default)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -977,8 +977,8 @@ namespace Renci.SshNet
public IAsyncResult BeginDownloadFile(string path, Stream output, AsyncCallback? asyncCallback, object? state, Action<ulong>? downloadCallback = null)
{
CheckDisposed();
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ThrowHelper.ThrowIfNull(output);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(output);
var asyncResult = new SftpDownloadAsyncResult(asyncCallback, state);
@@ -1028,8 +1028,8 @@ namespace Renci.SshNet
/// <inheritdoc/>
public void UploadFile(Stream input, string path, bool canOverride, Action<ulong>? uploadCallback = null)
{
ThrowHelper.ThrowIfNull(input);
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(input);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
CheckDisposed();
var flags = Flags.Write | Flags.Truncate;
@@ -1056,8 +1056,8 @@ namespace Renci.SshNet
/// <inheritdoc />
public Task UploadFileAsync(Stream input, string path, CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowIfNull(input);
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(input);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
CheckDisposed();
return InternalUploadFile(
@@ -1182,8 +1182,8 @@ namespace Renci.SshNet
/// </remarks>
public IAsyncResult BeginUploadFile(Stream input, string path, bool canOverride, AsyncCallback? asyncCallback, object? state, Action<ulong>? uploadCallback = null)
{
ThrowHelper.ThrowIfNull(input);
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(input);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
CheckDisposed();
var flags = Flags.Write | Flags.Truncate;
@@ -1258,7 +1258,7 @@ namespace Renci.SshNet
public SftpFileSystemInformation GetStatus(string path)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -1285,7 +1285,7 @@ namespace Renci.SshNet
public async Task<SftpFileSystemInformation> GetStatusAsync(string path, CancellationToken cancellationToken)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -1315,7 +1315,7 @@ namespace Renci.SshNet
public void AppendAllLines(string path, IEnumerable<string> contents)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(contents);
ArgumentNullException.ThrowIfNull(contents);
using (var stream = AppendText(path))
{
@@ -1339,7 +1339,7 @@ namespace Renci.SshNet
public void AppendAllLines(string path, IEnumerable<string> contents, Encoding encoding)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(contents);
ArgumentNullException.ThrowIfNull(contents);
using (var stream = AppendText(path, encoding))
{
@@ -1422,7 +1422,7 @@ namespace Renci.SshNet
public StreamWriter AppendText(string path, Encoding encoding)
{
CheckDisposed();
ThrowHelper.ThrowIfNull(encoding);
ArgumentNullException.ThrowIfNull(encoding);
return new StreamWriter(Open(path, FileMode.Append, FileAccess.Write), encoding);
}
@@ -1795,7 +1795,7 @@ namespace Renci.SshNet
public IEnumerable<string> ReadLines(string path, Encoding encoding)
{
// We allow this usage exception to throw eagerly...
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
// ... but other exceptions will throw lazily i.e. inside the state machine created
// by yield. We could choose to open the file eagerly as well in order to throw
@@ -1868,7 +1868,7 @@ namespace Renci.SshNet
/// <inheritdoc/>
public void WriteAllBytes(string path, byte[] bytes)
{
ThrowHelper.ThrowIfNull(bytes);
ArgumentNullException.ThrowIfNull(bytes);
UploadFile(new MemoryStream(bytes), path);
}
@@ -2014,8 +2014,8 @@ namespace Renci.SshNet
/// <exception cref="SshException">If a problem occurs while copying the file.</exception>
public IEnumerable<FileInfo> SynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern)
{
ThrowHelper.ThrowIfNull(sourcePath);
ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);
ArgumentNullException.ThrowIfNull(sourcePath);
ArgumentException.ThrowIfNullOrWhiteSpace(destinationPath);
return InternalSynchronizeDirectories(sourcePath, destinationPath, searchPattern, asyncResult: null);
}
@@ -2036,9 +2036,9 @@ namespace Renci.SshNet
/// <exception cref="SshException">If a problem occurs while copying the file.</exception>
public IAsyncResult BeginSynchronizeDirectories(string sourcePath, string destinationPath, string searchPattern, AsyncCallback? asyncCallback, object? state)
{
ThrowHelper.ThrowIfNull(sourcePath);
ThrowHelper.ThrowIfNullOrWhiteSpace(destinationPath);
ThrowHelper.ThrowIfNull(searchPattern);
ArgumentNullException.ThrowIfNull(sourcePath);
ArgumentException.ThrowIfNullOrWhiteSpace(destinationPath);
ArgumentNullException.ThrowIfNull(searchPattern);
var asyncResult = new SftpSynchronizeDirectoriesAsyncResult(asyncCallback, state);
@@ -2185,7 +2185,7 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client not connected.</exception>
private List<ISftpFile> InternalListDirectory(string path, SftpListDirectoryAsyncResult? asyncResult, Action<int>? listCallback)
{
ThrowHelper.ThrowIfNull(path);
ArgumentNullException.ThrowIfNull(path);
if (_sftpSession is null)
{
@@ -2249,8 +2249,8 @@ namespace Renci.SshNet
/// <exception cref="SshConnectionException">Client not connected.</exception>
private void InternalDownloadFile(string path, Stream output, SftpDownloadAsyncResult? asyncResult, Action<ulong>? downloadCallback)
{
ThrowHelper.ThrowIfNull(output);
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(output);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
@@ -2297,8 +2297,8 @@ namespace Renci.SshNet
private async Task InternalDownloadFileAsync(string path, Stream output, CancellationToken cancellationToken)
{
ThrowHelper.ThrowIfNull(output);
ThrowHelper.ThrowIfNullOrWhiteSpace(path);
ArgumentNullException.ThrowIfNull(output);
ArgumentException.ThrowIfNullOrWhiteSpace(path);
if (_sftpSession is null)
{
+3 -3
View File
@@ -215,7 +215,7 @@ namespace Renci.SshNet
/// <inheritdoc/>
public override void Flush()
{
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);
ObjectDisposedException.ThrowIf(_disposed, this);
if (_writeBuffer.ActiveLength > 0)
{
@@ -295,7 +295,7 @@ namespace Renci.SshNet
/// <exception cref="ObjectDisposedException">The stream is closed.</exception>
public void ChangeWindowSize(uint columns, uint rows, uint width, uint height)
{
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);
ObjectDisposedException.ThrowIf(_disposed, this);
_channel.SendWindowChangeRequest(columns, rows, width, height);
}
@@ -864,7 +864,7 @@ namespace Renci.SshNet
private void Write(ReadOnlySpan<byte> buffer)
#endif
{
ThrowHelper.ThrowObjectDisposedIf(_disposed, this);
ObjectDisposedException.ThrowIf(_disposed, this);
while (!buffer.IsEmpty)
{
+2 -2
View File
@@ -155,7 +155,7 @@ namespace Renci.SshNet
/// <inheritdoc />
public void AddForwardedPort(ForwardedPort port)
{
ThrowHelper.ThrowIfNull(port);
ArgumentNullException.ThrowIfNull(port);
EnsureSessionIsOpen();
@@ -166,7 +166,7 @@ namespace Renci.SshNet
/// <inheritdoc />
public void RemoveForwardedPort(ForwardedPort port)
{
ThrowHelper.ThrowIfNull(port);
ArgumentNullException.ThrowIfNull(port);
// Stop port forwarding before removing it
port.Stop();
+5 -5
View File
@@ -214,9 +214,9 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException">Either <paramref name="session"/>, <paramref name="commandText"/> is <see langword="null"/>.</exception>
internal SshCommand(ISession session, string commandText, Encoding encoding)
{
ThrowHelper.ThrowIfNull(session);
ThrowHelper.ThrowIfNull(commandText);
ThrowHelper.ThrowIfNull(encoding);
ArgumentNullException.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(commandText);
ArgumentNullException.ThrowIfNull(encoding);
_session = session;
CommandText = commandText;
@@ -244,7 +244,7 @@ namespace Renci.SshNet
#pragma warning disable CA1849 // Call async methods when in an async method; PipeStream.DisposeAsync would complete synchronously anyway.
public Task ExecuteAsync(CancellationToken cancellationToken = default)
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
if (cancellationToken.IsCancellationRequested)
{
@@ -385,7 +385,7 @@ namespace Renci.SshNet
/// <exception cref="SshOperationTimeoutException">Operation has timed out.</exception>
public IAsyncResult BeginExecute(string commandText, AsyncCallback? callback, object? state)
{
ThrowHelper.ThrowIfNull(commandText);
ArgumentNullException.ThrowIfNull(commandText);
CommandText = commandText;
+2 -2
View File
@@ -184,7 +184,7 @@ namespace Renci.SshNet
public void EnableAndActivateMessage(string messageName)
{
ThrowHelper.ThrowIfNull(messageName);
ArgumentNullException.ThrowIfNull(messageName);
lock (_lock)
{
@@ -208,7 +208,7 @@ namespace Renci.SshNet
public void DisableAndDeactivateMessage(string messageName)
{
ThrowHelper.ThrowIfNull(messageName);
ArgumentNullException.ThrowIfNull(messageName);
lock (_lock)
{
@@ -1,9 +1,9 @@
#nullable enable
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Renci.SshNet.Common;
namespace Renci.SshNet
{
/// <summary>
@@ -19,7 +19,7 @@ namespace Renci.SshNet
/// <param name="loggerFactory">The logger factory.</param>
public static void InitializeLogging(ILoggerFactory loggerFactory)
{
ThrowHelper.ThrowIfNull(loggerFactory);
ArgumentNullException.ThrowIfNull(loggerFactory);
LoggerFactory = loggerFactory;
}
+5 -5
View File
@@ -55,7 +55,7 @@ namespace Renci.SshNet
{
get
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
return _channel;
}
@@ -89,8 +89,8 @@ namespace Renci.SshNet
/// <exception cref="ArgumentNullException"><paramref name="session" /> or <paramref name="subsystemName" /> is <see langword="null"/>.</exception>
protected SubsystemSession(ISession session, string subsystemName, int operationTimeout)
{
ThrowHelper.ThrowIfNull(session);
ThrowHelper.ThrowIfNull(subsystemName);
ArgumentNullException.ThrowIfNull(session);
ArgumentNullException.ThrowIfNull(subsystemName);
_session = session;
_subsystemName = subsystemName;
@@ -106,7 +106,7 @@ namespace Renci.SshNet
/// <exception cref="SshException">The channel session could not be opened, or the subsystem could not be executed.</exception>
public void Connect()
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
if (IsOpen)
{
@@ -166,7 +166,7 @@ namespace Renci.SshNet
/// <param name="data">The data to be sent.</param>
public void SendData(byte[] data)
{
ThrowHelper.ThrowObjectDisposedIf(_isDisposed, this);
ObjectDisposedException.ThrowIf(_isDisposed, this);
EnsureSessionIsOpen();
_channel.SendData(data);
@@ -22,7 +22,7 @@ namespace Renci.SshNet.IntegrationTests.Common
public Socket Connect(IPEndPoint endPoint)
{
ThrowHelper.ThrowIfNull(endPoint);
ArgumentNullException.ThrowIfNull(endPoint);
var addressBytes = GetAddressBytes(endPoint);
return Connect(addressBytes, endPoint.Port);
@@ -30,7 +30,7 @@ namespace Renci.SshNet.IntegrationTests.Common
public Socket Connect(string host, int port)
{
ThrowHelper.ThrowIfNull(host);
ArgumentNullException.ThrowIfNull(host);
if (host.Length > byte.MaxValue)
{