add extension methods for checks

This commit is contained in:
Adam Hathcock
2026-04-23 10:11:54 +01:00
parent be4b6cdd7f
commit 2bae46e28a
21 changed files with 141 additions and 102 deletions

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SharpCompress;
internal static class StreamValidationExtensions
{
internal static Stream RequireReadable(this Stream stream)
{
stream.NotNull(nameof(stream));
if (!stream.CanRead)
{
throw new ArgumentException("Stream must be readable", nameof(stream));
}
return stream;
}
internal static Stream RequireSeekable(this Stream stream)
{
stream.NotNull(nameof(stream));
if (!stream.CanSeek)
{
throw new ArgumentException("Stream must be seekable", nameof(stream));
}
return stream;
}
internal static IReadOnlyList<Stream> RequireSeekable(this IReadOnlyList<Stream> streams)
{
streams.NotNull(nameof(streams));
foreach (var stream in streams)
{
stream.RequireSeekable();
}
return streams;
}
internal static IReadOnlyList<Stream> RequireReadable(this IEnumerable<Stream> streams)
{
streams.NotNull(nameof(streams));
var streamArray = streams as IReadOnlyList<Stream> ?? streams.ToArray();
foreach (var stream in streamArray)
{
stream.RequireReadable();
}
return streamArray;
}
}