StreamHelpers.CopyValidation.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.IO
  5. {
  6. /// <summary>Provides methods to help in the implementation of Stream-derived types.</summary>
  7. internal static partial class StreamHelpers
  8. {
  9. /// <summary>Validate the arguments to CopyTo, as would Stream.CopyTo.</summary>
  10. public static void ValidateCopyToArgs(Stream source, Stream destination, int bufferSize)
  11. {
  12. if (destination == null)
  13. {
  14. throw new ArgumentNullException(nameof(destination));
  15. }
  16. if (bufferSize <= 0)
  17. {
  18. throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, SR.ArgumentOutOfRange_NeedPosNum);
  19. }
  20. bool sourceCanRead = source.CanRead;
  21. if (!sourceCanRead && !source.CanWrite)
  22. {
  23. throw new ObjectDisposedException(null, SR.ObjectDisposed_StreamClosed);
  24. }
  25. bool destinationCanWrite = destination.CanWrite;
  26. if (!destinationCanWrite && !destination.CanRead)
  27. {
  28. throw new ObjectDisposedException(nameof(destination), SR.ObjectDisposed_StreamClosed);
  29. }
  30. if (!sourceCanRead)
  31. {
  32. throw new NotSupportedException(SR.NotSupported_UnreadableStream);
  33. }
  34. if (!destinationCanWrite)
  35. {
  36. throw new NotSupportedException(SR.NotSupported_UnwritableStream);
  37. }
  38. }
  39. }
  40. }